Migrate Wiki.js to Another Server

2 min read September 18, 2024 426 words

My migration setup

  • Original server runs WikiJS through docker. One container contains the PostgresDB and the other contains the node server
  • New server will be running WikiJS natively

Exporting the database on old server

From the old server, run the following command to dump the database to a file.

pg_dump wiki -U wiki -F c > wikibackup.dump

I'm using podman to host my containers. My command is a little different. I'll run pgdump on the container running the node server

podman exec 5eab35c74818 pg_dump wiki -U wikijs -F c > wikibackup.dump

Now move the dump file to your new server. I like using python's http server.

  # Run on the old server
  python -m http.server

  # Download the file on the new server
  wget https://OLD-SERVER-IP/wikibackup.dump

Importing the database

Start with installing postgres

sudo apt install postgresql

Now edit the following lines in /etc/postgresql/NUMBER/main/pg_hba.conf

# CHANGE THIS LINE TO TRUST
#local   all             postgres                                peer
local   all             postgres                                trust

# CHANGE THIS LINE MD5
#local   all             all                                    peer 
local   all             all                                     md5

Restart the postgres service

systemctl restart postgresql
systemctl enable postgresql

Now log into the postgres user account and create the database. This will be what we later merge our dump into. Fyi, if your docker container had a DB different than "wikijs", make sure to create that user instead.

sudo -i -u postgres
psql
CREATE USER wikijs WITH PASSWORD 'your_password';
CREATE DATABASE wiki;
GRANT ALL PRIVILEGES ON DATABASE wiki TO wikijs;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO wikijs;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO wikijs;
ALTER USER wikijs WITH SUPERUSER;
ALTER USER wikijs WITH CREATEDB;
ALTER USER wikijs WITH CREATEROLE;
ALTER USER wikijs WITH REPLICATION;
ALTER USER wikijs WITH BYPASSRLS;
\q
exit

With the database created, restore the database dump you transferred to the new server earlier.

pg_restore wikibackup.dump.1 -d wiki -U wikijs

Install the Wiki server, then edit the config.yml file to include the username and password created in the previous section.

  # Installing Node
  apt install curl
  curl -sL https://deb.nodesource.com/setup_lts.x | bash -
  apt install -y nodejs

  # Installing the application
  wget https://github.com/Requarks/wiki/releases/latest/download/wiki-js.tar.gz
  mkdir /var/wiki
  tar xzf wiki-js.tar.gz -C /var/wiki
  cd /var/wiki
  cp config.sample.yml config.yml
  vim config.yml

At this point you can test the server by running node server in the wiki folder. Create a systemd unit afterward to start the service on boot

cat <<OEM >/etc/systemd/system/wiki.service
[Unit]
Description=Wiki.js
After=network.target

[Service]
Type=simple
ExecStart=/usr/bin/node server
Restart=always
# Consider creating a dedicated user for Wiki.js here:
User=nobody
Environment=NODE_ENV=production
WorkingDirectory=/var/wiki

[Install]
WantedBy=multi-user.target
OEM

systemctl daemon-reload
systemctl enable --now wiki