ITSquad/Mastodon/Maintenance

From Pirate Party Belgium
Jump to navigation Jump to search

This page aims to describe procedures to maintain the Pirate Party's Mastodon instance.

How to upgrade?

A script called update.sh located in /home/mastodon will reproduce the steps below. Note: this script stops at step 14 and does not go further.

Before running this script or the following commands, please check the instruction for the current release. Sometimes, aditionnal actions are needed (other than migrate database and compile assets)

  1. git fetch # update the git branch
  2. git stash # prevent changes made to the files to be overwritten (mainly, the docker-compose.yml file)
  3. git checkout vX.X.X # jump to the version where we want to update
  4. git stash pop # restore your changes
  5. sed s/v[:digit:].[:digit:].[:digit:]/vX.X.X/g docker-compose.yml # and change vX.X.X by the new version
  6. docker-compose pull # pull from the docker-hub https://hub.docker.com/r/gargron/mastodon/
  7. docker-compose stop # stop the containers
  8. docker start mastodon_db_1 # start the db container
  9. docker exec -t mastodon_db_1 pg_dumpall -c -U postgres > ../dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql # backup the database
  10. tail ../your_dump_file.sql # check if the backup worked, with your_dump_file.sql being the dumpfile you have created in the previous step.
  11. docker-compose down # shut down the containers.
    You'll maybe get "ERROR: An HTTP request took too long to complete" and other errors. Don't mind this and just wait 'till it's done.
  12. docker-compose run --rm web rails db:migrate # upgrade the database
  13. docker-compose run --rm web rails assets:precompile # complie the assets
  14. docker-compose up -d # start the mastodon instance (create new volumes)
  15. docker-compose logs -ft web # (optional) if you want to monitor the progress. Once this is done you ctrl+c
  16. docker volume prune # remove all unused volumes

What to do when something went wrong?

Don't panic. You can restore the database backup as follows:

  1. docker-compose stop
  2. docker container start mastodon_db_1
  3. docker exec -t mastodon_db_1 dropdb postgres -U postgres # remove the db !!!!!!!
  4. docker exec -t mastodon_db_1 createdb postgres -U postgres # create a fresh and new db
  5. cat ../your_dump_file.sql | docker exec -i mastodon_db_1 psql -U postgres # restore the database, with "your_dump_file" being a database backup
  6. docker-compose down
  7. docker-compose up -d

You can also go to a previous version of Mastodon with:

  1. git checkout vX.X.X
  2. sed s/v[:digit:].[:digit:].[:digit:]/vX.X.X/g docker-compose.yml
  3. docker-compose pull

How to backup?

Backups of the database are not enough. We need to backup images, user feeds, etc.

We can simply copy the whole Mastodon's directory. For instance:

tar cvf /home/mastodon/backup/mastodon.tar /home/mastodon/mastodon/

Then, we backup the database:

docker exec -t mastodon_db_1 pg_dumpall -c -U postgres > /home/mastodon/backup/db/dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql

It can be interesting to execute the following command before doing a backup:

docker-compose run --rm web rails mastodon:media:remove_remote

This will remove local cache of media older than NUM_DAYS (=7 by default, but we can change this value). Note that this command is executed every day.

Currently, we do not provide automatic backups!