将 bbPress WordPress 插件论坛迁移到 Discourse

我最近成功使用 Discourse 内置的迁移脚本完成了 bbPress 数据库迁移。现在,我将以分步教程的形式分享这一过程。

注意:本教程适用于 bbPress 插件,而非 bbPress 的旧版独立版本。

可以导入哪些数据?

  • 用户(包括匿名用户)
  • 分类
  • 主题
  • 帖子
  • 私信(通过 BuddyPress)
  • 附件
  • 固定链接

在开始迁移之前,请在您的机器上(或在虚拟机内)设置开发环境,并在那里运行导入操作,而不是在 Docker 容器内运行。当我尝试在 Docker 容器内运行时,遇到了 peer authentication failed(对等认证失败)问题。因此,我强烈建议您使用开发机器。请参阅 macOSUbuntu / Debian 的开发安装指南。

Discourse 需要 Ruby 3.4 或更高版本。您可以使用以下命令检查您的 Ruby 版本:

ruby -v

接下来,我们需要安装 libmysqlclient-dev 依赖项,以便能够使用 mysql2 gem。

sudo apt-get install libmysqlclient-dev

安装成功后,进入您的 Discourse 开发安装路径(通常为 ~/discourse)。

cd ~/discourse

配置数据库连接

bbPress 导入脚本会从环境变量中读取所有数据库连接设置。您无需编辑脚本文件。支持以下环境变量:

变量 默认值 描述
BBPRESS_HOST localhost MySQL 数据库主机
BBPRESS_USER root MySQL 用户名
BBPRESS_PW (空) MySQL 密码
BBPRESS_DB bbpress MySQL 数据库名称
BBPRESS_PREFIX wp_ WordPress 表前缀
BBPRESS_ATTACHMENTS_DIR /path/to/attachments bbPress 附件目录路径

如果您正在迁移本地主机(localhost)上的数据库,通常只需设置数据库名称:

IMPORT=1 bundle && IMPORT=1 BBPRESS_DB="my_bbpress" bundle exec ruby script/import_scripts/bbpress.rb

如果您正在迁移外部服务器上的数据库,则还需要设置主机、用户名和密码:

IMPORT=1 bundle && IMPORT=1 BBPRESS_HOST="REMOTE_HOST_NAME" BBPRESS_USER="DB_USERNAME" BBPRESS_PW="MY_SECURE_PASSWORD" BBPRESS_DB="DB_NAME" bundle exec ruby script/import_scripts/bbpress.rb

恭喜! 您的数据库已成功从 bbPress 迁移到 Discourse :clap: :wave: :boom:

现在,请从管理页面 /admin/backups 备份数据,并将其导入到您的线上 Discourse 网站中。


将 bbPress 论坛迁移到 Discourse 后,如果您仍计划将 WordPress 网站作为主站点使用,并希望将其与 Discourse 连接,请安装 Discourse 官方 WordPress 插件

15 个赞

Just wanted to thank you for providing this step-by-step guide. We migrated our site from bbpress to Discourse with minimal headache thanks to you. Since we’re running multisite wordpress there were a few tweaks to make to the importer, but other than that it went very smoothly. Thanks!

3 个赞

If you can share your tweaks then it will be helpful to other multisite owners.

1 个赞

Admittedly I didn’t take very good notes, and have since killed the VM it
was on (sorry!) but the basic gist is that if your bbpress install is not
on the primary site of the Multisite setup you’ll need to a) set the
environmental variable for BBPRESS_PREFIX to include your site’s ID number
(e.g. wp_6_ ) and then edit the migration script to use wp_users rather
than #{BBPRESS_PREFIX} for the Users sql. This is because on Multisite
installations the wp_users table is shared across sites and then each site
has its own tables for posts, postmeta, etc.

5 个赞

thanks for the details :thumbsup:

I recently posted on how to import bbpress into discourse

2 个赞

Great job on the tutorial! But it’s confusing to have two different guides on this. Do they serve different purposes? Otherwise we should figure out how to merge them.

That guide is about how to import bbpress inside of a development environment, mine is how to import bbpress using the docker container. It’s just 2 different ways to go. I’d recommend importing via docker container since it doesn’t ask for the additional step of setting up a development environment, which can be cumbersome.

Hello I am stuck trying to import successfully after all users are done importing.

As soon it begins to import anonymous users it cancels with an error of “Invalid Email… and Validation failed: Username can’t be blank (ActiveRecord::RecordInvalid)”.

I added the error below, does anyone encountered this before or have any ideas on what I should do?

Thanks,

importing anonymous users...
Invalid email '' for ''. Using '6be92499a6f885cb271d94bffd5e667b@email.invalid'
Error on record: {:id=>"", :email=>"", :name=>"", :website=>nil}
Traceback (most recent call last):
	23: from script/import_scripts/bbpress.rb:512:in `<main>'
	22: from /home/lutechi/discourse/script/import_scripts/base.rb:49:in `perform'
	21: from script/import_scripts/bbpress.rb:31:in `execute'
	20: from script/import_scripts/bbpress.rb:153:in `import_anonymous_users'
	19: from /home/lutechi/discourse/script/import_scripts/base.rb:249:in `create_users'
	18: from /home/lutechi/discourse/script/import_scripts/base.rb:249:in `each'
	17: from /home/lutechi/discourse/script/import_scripts/base.rb:261:in `block in create_users'
	16: from /home/lutechi/discourse/script/import_scripts/base.rb:337:in `create_user'
	15: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:212:in `transaction'
	14: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `transaction'
	13: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:236:in `within_new_transaction'
	12: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/2.6.0/monitor.rb:230:in `mon_synchronize'
	11: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:239:in `block in within_new_transaction'
	10: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:267:in `block in transaction'
	 9: from /home/lutechi/discourse/script/import_scripts/base.rb:338:in `block in create_user'
	 8: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/suppressor.rb:48:in `save!'
	 7: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:315:in `save!'
	 6: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:385:in `with_transaction_returning_status'
	 5: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:212:in `transaction'
	 4: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:265:in `transaction'
	 3: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:387:in `block in with_transaction_returning_status'
	 2: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/transactions.rb:315:in `block in save!'
	 1: from /home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/validations.rb:52:in `save!'
/home/lutechi/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/validations.rb:80:in `raise_validation_error': Validation failed: Username can't be blank (ActiveRecord::RecordInvalid)

Thanks for this useful tutorial - worked for me although I have a couple of small issues:

  1. The install process seemed to install postgres 11 and Discourse hosted version support postgres 10, so that’s giving them a bit of a headache!

  2. My imported users don’t seem to be able to log into Discourse with their WordPress credentials - I tried it with my own user account, both with the username and the email address, but neither work. I can log into WordPress fine and the user account is present in Discourse, with the associated forum threads. Something changed with passwords/auth maybe?

Are you using SSO from WordPress?

This requires the migrate password plugin. Did you install that?

3 个赞

Hi Michael,

No, that is the first reference I’ve found to the plugin, so thank you for highlighting it - maybe I missed a step somewhere! Thanks for sharing I will take a closer look tomorrow when my eyes aren’t quite so tired!

Ruth

3 个赞

Hi :wave:

Can user uploaded pictures also be migrated?
The plugin for the user uploads was GD bbPress Attachments

Cheers :beers:

The script appears to import attachments.

You need to set a BB_PRESS_ATTACHMENTS_DIR ENV variable with the path to the attachments.

1 个赞

The uploads of the bbPress plugin appear to be stored in the /uploads folder of the WordPress installation. The GD bbPress Attachments was used for this. Im not sure which kind of path I should put in the BB_PRESS_ATTACHMENTS_DIR variable. Could you help me here a bit? Should I put the full URL path to the uploads e.g. http://www.example.com/httpdocs/wp-content/uploads/2018/02/picture.png or what else?

I saw that the import script didn’t finish successfully since the import_private_messages step failed. Private messages were not activated in the forum. I removed that step form the script and it fails with the message: table wp_bb_attachments doesn't exist which is true. I can’t find any table for attachments in the database tho. How could that attachments have worked in the first place? :thinking:

The attachments need to be in a directory on the server where you run the import.

If you don’t see the attachment table that the script is looking for It may be that you have used some different means of handling attachments than the script expects. My guess is that they are either in a different table that you haven’t found or that the relative filename is in the posts themselves.

It sounds like you need to another function to import them. If you have a budget I can help. Discourse Migration — Literate Computing, LLC has information about imports. You don’t need full service, it seems, so your cost would be less than suggested there.

Since Discourse always runs in a docker container should this directory be linked into the import container?

Correct. If you’re running the import from a container then you need to put those images somewhere that the can be seen from the container and use that path.

1 个赞

One last question. Since you mentioned your import service I checked the site and did some more digging into my database and found where the attachments are stored. One sentence on your page made me worry a bit:

For example, some forums can attach files to a post without mentioning them in the body of the message, the import script identifies attachments by replacing references to them in the message body; when this happens, those un-linked attachments are omitted.

I found that my attachments where stored in the wp_postsmeta table as _wp_attached_file. The post itself which had this attachment linked however does not have a mention of this in the post body. It seems the only link here is the post_id and its meta_id. Does this mean that this is an “un-linked attachment” and will be omitted?