As you properly know, NodeBB supports two DB backends, Redis and MongoDB. Discourse importer script supports them both. In this tutorial, we will learn how to migrate NodeBB with MongoDB as DB backend. We will be using NodeBB Importer with mongo
adapter. If your NodeBB forum uses Redis as backend then please follow this tutorial which demonstrate the redis
adapter.
The plan
-
Preparing the development environment.
-
Export database from production environment.
-
Import the production DB to a Discourse instance.
-
Run the importer script.
What can be migrated
- Groups
- Attachments
- Categories
- Root Category => Root Category
- Sub Category & Sub-Sub-Category => Sub-Category
- Topics & Posts
- pinned topic => pinned topic
- locked topic => closed topic
- topic views
- upvoted_by
- styles, mentions, emoji, and attachments.
- Users (with the following attributes)
- profile background
- avatars
- banned status
- username
- name
- admin
- bio
- group
- website
- location
- joining at
Preparing Local Development Environment
As mentioned in our plan we first need to prepare our development environment. Follow one of these guides to install Discourse itself:
Please, consult this guide if you have any problems setting up Discourse server.
Then install MongoDB database server.
Ubuntu-18-04:
$ wget -qO - https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add -
$ echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
$ sudo apt-get update
$ sudo apt-get install -y mongodb-org
$ sudo service mongod status
For more details, refer to the official guide
Mac OS:
$ brew tap mongodb/brew
$ brew install mongodb-community@4.0
$ brew services start mongodb-community@4.0
$ brew services status mongodb-community@4.0
For more details, refer to the official guide
Windows 10:
Download the installer and install MongoDB as a Windows service:
https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-4.0.12-signed.msi
Run the cmd
with Administrative privileges to check if the mongo server is working properly:
"C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe"
For more details, refer to the official guide.
This environment will be our Discourse server.
Exporting Production Database Dump:
Shut down your NodeBB forum (production server).
$ cd /path_to_nodebb
$ ./nodebb stop
Shut down your database:
$ sudo service mongodb stop
Backup your DB:
$ mongodump --out ~/my_backup_path/
The output of the previous command will be something like this:
2019-08-16T15:17:09.845+0300 done dumping admin.system.users (1 document)
2019-08-16T15:17:09.846+0300 done dumping admin.system.version (2 documents)
2019-08-16T15:17:09.849+0300 done dumping nodebb.sessions (10 documents)
2019-08-16T15:17:09.850+0300 done dumping nodebb.socket.io (215 documents)
2019-08-16T15:17:09.854+0300 done dumping nodebb.objects (1488 documents)
Note that the backup is actually a directory, not just a file.
You can check the size of your DB by running
use myDatabase
thendb.stats().dataSize;
insidemongo
CLI. The returned value will be in bytes.
Backup your forum assets:
$ cd /path_to_nodebb_root_directory/
$ tar -czf ./uploads.tar.gz ./public/uploads
After you have your DB and assets forum you should copy them to Discourse server.
Importing The Database
Now that we have our database we can import it in our local MongoDB instance:
$ mongorestore ~/path_of_my_backup_directory/
For more options, refer to official guide.
Next, you need to extract the uploads.tar.gz so the importer can import the assets:
$ tar xvzf uploads.tar.gz
Running The Importer Script
Now that our database and assets in place we are ready to run our importer script. Before that, we need to edit the NodeBB importer script to fit our needs.
This is the path of your NodeBB uploads folder:
ATTACHMENT_DIR = '/absolute_path/uploads'
Next, we need to tell the importer to use the mongo
adapter instead of redis
adapter:
adapter = NodeBB::Mongo
@client = adapter.new('mongodb://127.0.0.1:27017/nodebb')
# adapter = NodeBB::Redis
# @client = adapter.new(
# host: "localhost",
# port: "6379",
# db: 14
# )
Run the importer with clean Discourse and mongo
gem support:
$ cd ~/discourse
$ echo "gem 'mongo'" >> Gemfile
$ bundle install
$ bundle exec rake db:drop db:create db:migrate
$ bundle exec ruby script/import_scripts/nodebb/nodebb.rb
The importer will connect to the MongoDB instance and migrates everything to Discourse.
After the importer finishes, start Discourse:
$ bundle exec rails server
Start Sidekiq to process the migrated data:
$ bundle exec sidekiq
You can monitor the progress at http://localhost:3000/sidekiq/queues
.
Perform a Discourse backup and upload it to your Discourse production server by following this tutorial.
If you have any questions about the process I am happy to help.
Happy migration