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

需要从一个 Discourse 实例迁移一些内容到另一个实例吗?

有一个命令行工具可以导出主题、一组主题或整个板块,还有一个工具可以在另一端导入它们。

主题导出/导入

  • 一个或多个主题及其回复
  • 在主题中发帖的用户

板块导出/导入

  • 板块及其所有子板块
  • 其安全设置
  • 安全设置中提到的自定义组
    • :warning: 如果任何组启用了“成员申请”,将会崩溃。请在导出之前关闭此功能
  • 板块和子板块中的所有主题和帖子
  • 所有发帖用户,并将他们分配到相应的组

开始:识别 ID

首先,识别你要导出的 =Export= 的 ID。你可以在用于访问它的 URL 末尾找到它。

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

  • https://meta.discourse.org/t/how-to-contribute-to-discourse/53797/12
    (在这种情况下,你正处于主题中间——取第一个数字)

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

板块或主题 ID

多个主题

你可以通过用空格分隔 ID 来选择多个主题;这在上面的字段中有效(我认为这对板块无效):

  • 53797 200943 124096

运行导出脚本

该工具可以在容器内以 root 用户身份运行:

cd /var/discourse
./launcher enter app

运行此命令:

discourse export_=Export= =ID=

输出应该类似如下:

开始导出 =Export=…

新用户都来自哪里?
导出已保存到 =Export=-export-2021-08-27-015437.json

完成

移动导出的文件

导出的 .json 文件需要从执行导出的应用程序容器内部移动到接收实例的容器内部。这需要几个步骤。

复制到“外部”

如果你还没有退出容器,请退出

exit

你现在应该看到如下提示:

root@yourserver:/var/discourse

使用 docker 将文件从容器中复制出来(我认为不能移动它)

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

移动到另一个实例

这超出了本指南的范围,但这可能对你有帮助:

复制到接收实例的容器中

以 root 身份 SSH 到你的接收实例并进入 Discourse 目录:

cd /var/discourse

再次使用 docker 将文件复制到另一个容器中

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

导入内容

进入接收实例的容器:

./launcher enter app

运行导入脚本

discourse import_=Export= =FileName=

:cake: 完成!

你可能需要清理一些遗留问题,但希望一切都能顺利完成。

高级技巧

缺少 Ruby Gem

在进行复杂的板块导入时,我的实例缺少 ‘Listen’ Ruby 库。
这可以通过以下方式解决:(使用容器内的 Discourse 用户)

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

这破坏了 rails 控制台,之后需要重建

./launcher rebuild app

多站点

在多站点环境中,使用 RAILS_DB 环境变量。

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

请在 Contribute > Bug 板块中报告错误,并在 Contribute > Feature 板块中请求功能增强。


相关主题


最后由 @SaraDev2022-07-13T00:00:00Z 审核

45 个赞
UI: Topic and Category Export/Import
CLI command to migrate a single category
Moving topics between sites?
Category/Topic export fails with error "database "discourse_development" does not exist"
Easiest way to export a single topic?
Saving a Conversation
How to export user data from a category on a hosted site
Import CSV User Activity from one Discourse to Another
How use API to convert topic in discourse1.com to discourse2.com
Can sub-forums of the same instance split up and move to new independent servers (or discourse instances)?
Help migrating category from one instance to another without admin on the source instance
I wonder how to add hundreds of users into a group? Is there any api?
How to download Comments and Topics
Adding default sets of topics, categories and users to a new Discourse
Can we combine 2 separate discourse sites?
Tracking and resolving a schema drift cause
Merge two Discourse forums into one
Moving topics from a forum on one website to a forum on another website
Transfer topics between forums?
Forum Aquisition
Transfering inbox group messages to another instance?
Transfering inbox group messages to another instance?
Imported categories are not in parent category
Copy categories / topics
Downloading Content
Selective backups skipping settings and config
Test driving admin/moderation features
Add backup to existing Discourse instance
Merging two Discourse forums
Migrating vBulletin onto existing and live Discourse instance
Migrate topics from one discourse instance to another?
A system where people have to start at lesson 1, and cannot see the other lessons?
How to divide my community into 2 parts
Migrate user custom fields to another instance
Error Importing Categories from a Discourse Instance to Another

我将再次尝试一下。

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

1 个赞

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

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

1 个赞

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

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

或者你的意思是这个?

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

或者完全是别的东西?

1 个赞

我认为其中任何一个都应该可以工作。但是,您可以将这些图像获取到新站点,它们应该会生成相同的 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,这样链接就可以使用了。你可以手动尝试几个看看。

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

2 个赞

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

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

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

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

1 个赞

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

1 个赞

我一直想解决上传文件迁移的问题,已经惦记很久了,现在借助 LLM 的帮助终于搞定了。分享我的笔记给关注这个话题的朋友们!Jay 在上面的帖子中包含了启动这项任务所需的关键诀窍,非常感谢他。

我最初尝试的方法完全符合 Jay 的预测。如果你创建一个包含源站点图片 URL 的列表,并将其粘贴到新站点的帖子中,这些图片就会被下载。要实现这一点,必须启用 Download remote images to local 选项。

我的 LLM 为此生成了一个脚本,我在这里分享出来,供后来者参考。

摘要

在 Rails 控制台中运行以下代码:

topic = Topic.find(=TOPIC_ID=)
posts = Post.where(topic_id: topic.id)
urls = []

posts.each do |p|
  UploadReference.where(target: p).each do |ref|
    urls << "=SOURCE_URL=#{ref.upload.url}"
  end
end

puts urls.uniq.join("\n")
puts "\nTotal: #{urls.uniq.count} upload(s)"

:bulb: 想迁移整个分类吗? 将前两行替换为:

topic_ids = Topic.where(category_id: =TOPIC_ID=).pluck(:id)
posts = Post.where(topic_id: topic_ids)

在这种情况下,=TOPIC_ID= 是你的分类 ID,可以在 Admin → Categories 中该分类 URL 的末尾找到。

然而,对我来说这还不够,因为我还有 PDF 文件,而 PDF 文件不会自动下载到本地。

所以我让 LLM 为我编写了一个用于迁移上传文件的 Python 脚本。它运行得相当不错,所以我愿意分享它。如果你想尝试或提供反馈,这里是仓库地址:tobias/discourse-upload-migrator - Digitally Sovereign Forgejo: Digitally Sovereign, for the win!

我还让 LLM 为我编写了一份关于迁移主题或分类的更完整的操作手册,它已经发布在我的 discourse 上:https://digitallysovereign.org/t/move-topics-or-categories-between-discourse-instances/742?u=tobias

1 个赞