将主题从一个Discourse实例迁移到另一个实例

Need to move some stuff from one instance of Discourse to another?

There is a command line tool to export a topic, a set of topics, or an entire category, and another to import them in at the other end.

Topic Export/Import

  • one or more topics and their replies
  • the users who posted in the topic

Category Export/Import

  • the category and all its subcategories
  • its security settings
  • custom groups mentioned in the security settings
    • :warning: if any of the groups have ‘membership requests’ enabled, it will crash. Turn this off before the export
  • all topics and posts in the category and subcategories
  • all users who posted, and assigns them to the groups

Begin: Identify the IDs

First, identify the ID(s) of the =Export= that you wish to export. You’ll find this at the end of the URL used to access it.

  • https://meta.discourse.org/t/how-to-contribute-to-discourse/53797

  • https://meta.discourse.org/t/how-to-contribute-to-discourse/53797/12
    (in this case you are in the middle of a topic - grab the first number)

  • https://meta.discourse.org/c/howto/faq/4

Category or Topic ID(s)

Multiple topics

You can select more than one topic by adding IDs separated by a space; this works in the field above (I don’t think that this works for categories though):

  • 53797 200943 124096

Run the Export Script

The tool can be run as a root user inside the container:

cd /var/discourse
./launcher enter app

Run this command:

discourse export_=Export= =ID=

The output should look something like this:

Starting export of =Export=…

Where are the new users all coming from?
Export saved to =Export=-export-2021-08-27-015437.json

Done

Move the Exported File

The exported .json file needs to be moved from the inside of the application container where you did the export to the inside of the container of the receiving instance. This takes a few steps.

Copy it to the ‘outside’

Exit the container if you haven’t already

exit

You should now have a prompt like this:

root@yourserver:/var/discourse

Use docker to copy the file out of the container (you can’t move it I don’t think)

docker cp app:/var/www/discourse/=FileName= .

Move it to the other instance

This is beyond the scope of this guide, but this might help you get there:

Copy it into the container in the receiving instance

SSH into your receiving instance as root and get to the Discourse directory:

cd /var/discourse

Use docker once again to copy the file into the other container

docker cp =FileName= app:/var/www/discourse

Import the Content

Enter the container in the receiving instance:

./launcher enter app

Run the import script

discourse import_=Export= =FileName=

:cake: Done!

You might need to tidy up some loose ends, but hopefully, it should all be done.

Advanced Tips

Missing Ruby Gem

When doing a complex category import, my instance was missing the ‘Listen’ Ruby.
This was solved by doing this: (using the Discourse user inside the container)

./launcher enter app
su discourse
bundle config --delete without
bundle config --delete with
bundle install
exit
exit

This broke the rails console, necessitating a rebuild afterwards

./launcher rebuild app

Multisite

In a multisite environment, use the RAILS_DB env variable.

RAILS_DB=xxxmoves discourse export_=Export= =ID=
RAILS_DB=dancerehab discourse discourse import_=Export= =FileName=

Please report bugs in the bug category, and request enhancements in the feature category.


Related Topics


Last Reviewed by @SaraDev on 2022-07-13T00:00:00Z

Last edited by @hugh 2024-05-29T06:25:11Z

Check documentPerform check on document:
43 个赞

我将再次尝试一下。

有没有一种简单的方法可以同时迁移相关的上传和主题?

不算有。一种大部分有效的方法是,如果你获取了所有这些上传文件,并将原始文件的 URL 粘贴到你正在迁移到的站点的任何帖子中,那么应该会发生的是,这些上传文件将被创建,具有相同的 SHA,并且它会“正常工作”。

所以你会做那个将帖子中的内容转换为 URL 的事情来获取那些 URL。(我总是记不住怎么做,所以每次都得弄清楚)。

嗯——我还是有点不明白。你是说这个会起作用吗?

  1. 从第一个网站抓取所有上传文件
    • 怎么抓?
  2. 将它们放入一个主题
  3. 它们将生成与旧网站上相同的相对链接,然后神奇地完成!

或者你的意思是这个?

  1. 从旧网站抓取你感兴趣的类别中的所有绝对 URL
  2. 将它们粘贴到主题中
  3. 它们都将以相同的 SHA 下载到新网站,并且现有链接将神奇地全部起作用

或者完全是别的东西?

我认为其中任何一个都应该可以工作。但是,您可以将这些图像获取到新站点,它们应该会生成相同的 sha,这样现有的主题链接就能神奇地生效。

也许你可以这样做:

cat_id=123
Topic.where(category_id: cat_id).each do| t|
  Post.where(topic_id: t.id) do |p|
      p.cooked.match(image url)....
         puts(the match)

我认为如果你这样做,并将这些链接粘贴到你网站上的任何帖子中,它应该会将所有这些图像下载到本地并生成相同的 sha,这样链接就可以使用了。你可以手动尝试几个看看。

现在是深夜。你的体验可能会有所不同。

1 个赞

您好,我不确定这个教程是否是我想要的,但在此提出疑问。

如果我在服务器上安装了 Discourse,但由于增长需要迁移到更强大的服务器,这些是步骤吗?

例如,备份选项会怎样?我不能这样做,在另一台服务器上安装 Discourse,然后从该选项恢复另一个实例的备份副本吗?

我期待您的澄清,因为我想知道在需要时如何迁移,并且不丢失我最近所做的工作。

不。要迁移到更大的服务器,只需进行简单的备份和恢复即可。您还可以使用 rsync 将 Discourse 站点迁移到另一台 VPS