How to migrate a dockerized Mastodon to a non-dockerized Mastodon instance

What is Mastodon?

Mastodon is a microblogging platform akin to others you may have seen, such as Twitter, but instead of being centralised it is a federated network which operates in a similar way to email.

Like email, you choose your server and whether it’s GMail, Outlook, iCloud, wherever you sign up you know you’ll be able to email everyone you need to so long as you know their address.

The word “instance” is often used on Mastodon as a synonym of server.

This means there’s no big uncaring company running it all, no shareholders, no central control, none of the targeted advertising we’re all fed up with, just a bunch of people sharing the things they want to share with each other.

(from https://blog.joinmastodon.org/2018/08/mastodon-quick-start-guide/)

What is this Guide?

This Guide will show you how to migrate an existing Mastodon dockerized instance to a new Server without docker. Please read the complete Guide carefully before you start to migrate and if possible test it before live migration.
You can also synchronize the files more than once to reduce the time required for live migration. The Installation on the new server without docker is documented here. but don’t run mastodon:setup.

Requirements:
  • root-access via ssh
  • Mastodon installed via Guide without mastodon:setup
On the old Server

I assume that Mastodon is running in the directory ~/mastodon/, otherwise please adjust in the commands. The container names also depend on the directory-name.

Step 1: Stop Mastodon

You have to stop the following containers manually, mastodon_streaming_1, mastodon_sidekiq_1 and mastodon_web_1:

docker stop mastodon_streaming_1 && docker stop mastodon_sidekiq_1 && docker stop mastodon_web_1
Step 2: Create Database dump

The following command will put the dump into the directory ~/mastodon/postgres/

docker exec mastodon_db_1 pg_dump -Fc -U postgres -d postgres -f mastodon.dump

Wait a moment and then check if dump was created:

ls -lah ~/mastodon/postgres/

Then stop the complete stack with:

docker-compose down
On the new Server
Step 3: Sync system-files

If you’re using S3, you can skip this step. Otherwise use rsync to copy the files to your new server, I assume you use mastodon as user.

rsync -avz --chown=mastodon:mastodon --progress old.server.tld:~/mastodon/public/system/ ~/mastodon/live/public/system/
Step 4: Copy and Edit Configuration

You copy the file .env.production to your new server:

scp old.server.tld:/~/mastodon/.env.production /home/mastodon/live/
chown mastodon:mastodon /home/mastodon/live/.env.production

now you have to adjust the settings for the database in .env.production

nano /home/mastodon/live/.env.production

and set DB_SETTINGS:

DB_HOST=/var/run/postgresql
DB_NAME=mastodon
DB_USER=mastodon
Step 5: Copy and Import database

Before importing the database, we have to copy the dump and create an empty Database on the new server:

scp old.server.tld:/~/mastodon/postgres/mastodon.dump /tmp/
chown mastodon:mastodon /tmp/mastodon.dump

change to user mastodon and create the empty DB:

su - mastodon
createdb -T template0 mastodon

Then import the dump:

pg_restore -U mastodon -n public --no-owner --role=mastodon -d mastodon /tmp/mastodon.dump
Step 6: Finish the migration

Before we can start the new server, we have to precompile the assets as user mastodon:

cd /home/mastodon/live/
RAILS_ENV=production bundle exec rails assets:precompile

then we rebuild the home timelines for our users:

RAILS_ENV=production ./bin/tootctl feeds build

This can take some time, when finished it is time to start the new server

Step 7: Start Mastodon

You have to change your prompt back to user root and start the services:

systemctl start mastodon-sidekiq mastodon-streaming mastodon-web

you can check the services with:

systemctl status mastodon-sidekiq mastodon-streaming mastodon-web

Do not forget to update your DNS settings to point to the new server and update or copy your Nginx/Apache2 configuration, re-run LetsEncrypt as necessary.

Enjoy your new Server and disable the old one.


If you want to run your own fully managed Instance hosted in Germany with 100% green Energy, look here

If you want to use mastodon without your own instance you may register here:

MastodonTech.de

or here:

dresden.network

If you want to contribute to the running costs of this Blog or the running Mastodon-Instances, then you can use the following link:

Support

Thx and feel free to contact me 🙂

———–

Sources:
https://docs.joinmastodon.org