Skip to content

Backups

A production installation needs regular backups. Three things must be saved: the PostgreSQL database, the file storage (uploaded documents) and the .env with the keys.

Where backups go

Keep the backups outside the directory with compose.yml. If you build the image yourself, Docker copies everything in that directory into the image, backups included. The commands on this page use /var/backups/lalabase. Create it once, readable only by the user who makes the backups:

sudo install -d -m 700 -o "$USER" /var/backups/lalabase

Dumps and archives are confidential: they hold all content in plain text, and open invitation links work with what is in them.

Backing up the database

Create a dump with pg_dump, directly in the DB container. Run it in the directory with compose.yml:

b=/var/backups/lalabase
docker compose exec -T db pg_dump -U lalabase lalabase > $b/lalabase.sql.tmp \
  && test -s $b/lalabase.sql.tmp \
  && mv $b/lalabase.sql.tmp $b/lalabase.sql

The database is called lalabase, as compose.yml creates it.

  • -T runs the command without a terminal, so the file holds only what pg_dump writes. Current versions of Docker Compose leave out the terminal by themselves when the output goes into a file; -T makes sure of it.
  • The shell creates the target file before pg_dump runs, even when it fails. That is why the dump goes into a temporary file first and replaces lalabase.sql only when the command succeeded and the file is not empty. A failed run keeps the last good backup.

Backing up file storage

Uploaded files live in the storage volume. tar packs them inside the container and writes the archive to the host, with the same temporary file as the dump:

b=/var/backups/lalabase
docker compose exec -T web tar -C /rails/storage -cf - . > $b/storage.tar.tmp \
  && test -s $b/storage.tar.tmp \
  && mv $b/storage.tar.tmp $b/storage.tar

docker compose cp is not a good fit here. Copied a second time into the same folder, it puts the new copy into a subfolder storage and leaves the old files at the top level. And folders copied back with it do not belong to the application's user, so new uploads into them fail.

Consistency

Back up the database and storage in the same time window, otherwise references to files may point nowhere.

Backing up .env

The .env holds the keys the data depends on. Without the three encryption values (ACTIVE_RECORD_ENCRYPTION_*), stored AI keys and repository tokens stay unreadable, even with a complete backup. Keep a copy of the .env in a safe place apart from the dumps, for example in a password manager, and update it whenever you change a value. Whoever has the .env and a dump can sign in to the running instance as any user, administrators included, and can read the encrypted values.

Regular backups

A small script on the host works well, called by cron. It saves the database and the files one after the other:

#!/bin/sh
set -eu
umask 077
cd /path/to/lalabase    # the directory with compose.yml and .env
b=/var/backups/lalabase
day=$(date +%F)

# save FILE COMMAND...: keeps FILE only if the command worked and wrote something
save() {
  file=$1; shift
  "$@" > "$file.tmp"
  test -s "$file.tmp"
  mv "$file.tmp" "$file"
}

save "$b/lalabase-$day.sql" docker compose exec -T db pg_dump -U lalabase lalabase
save "$b/storage-$day.tar" docker compose exec -T web tar -C /rails/storage -cf - .

set -e stops the script at the first error and leaves only a .tmp file behind. Cron reports the error by mail only if the host can send mail. Look into the backup folder now and then, copy the backups to separate storage, and delete old ones: every run saves all files again.

If someone uploads a file while tar reads the storage, tar may report file changed as we read it and end with an error. The run then keeps the last good archive; start it again, or wait for the next one.

Restoring

Restoring needs an empty database. After docker compose up the database is never empty: the web container prepares it on every start. Load a dump into it, and psql prints hundreds of errors and still reports success. So stop the application and the worker, recreate the database, load the dump and the files, then start them again:

b=/var/backups/lalabase
docker compose stop web worker \
  && docker compose exec -T db dropdb -U lalabase lalabase \
  && docker compose exec -T db createdb -U lalabase lalabase \
  && docker compose exec -T db psql -q -v ON_ERROR_STOP=1 -U lalabase lalabase < $b/lalabase.sql \
  && docker compose run --rm --no-deps -T web tar -C /rails/storage -xf - < $b/storage.tar \
  && docker compose start web worker

For a backup from the script, use its dated files instead of lalabase.sql and storage.tar.

  • The steps are joined with &&: if one fails, the rest does not run, and the application stays stopped. Do not start it by hand. Fix the cause and run the whole block again; it begins with a fresh database.
  • ON_ERROR_STOP=1 stops psql at the first error and makes it fail. Without it, psql goes on after an error and still reports success.
  • docker compose run starts a short-lived container with the storage volume while web is stopped. tar unpacks there as the application's user, so the application can write into the restored folders.
  • If files were ever copied back with docker compose cp, their folders belong to root, and tar stops with Cannot open. Give them back to the application's user first: docker compose run --rm --no-deps -T -u 0 web chown -R 1000:1000 /rails/storage
  • Restore with the release that made the backup, or a newer one. On its first start, a newer release updates an older database; an older release does not run on a newer one. Note the release next to your backups.
  • On a new server, put the .env from your backup in place before the first docker compose up.

dropdb deletes the running database for good. Run it only once you have the dump at hand.

Warning

Test your backups regularly with a trial restore. An untested backup is not a backup.

Moving to a new server or cloning a server

To move, load the database and the storage onto the new server as described above. A clone, for example a test instance with a copy of the data, is made the same way. What matters are the five values from the .env.

Keep the three encryption values (ACTIVE_RECORD_ENCRYPTION_*) exactly as they are. With other values, stored AI keys and repository tokens stay unreadable, and nothing brings them back.

SECRET_KEY_BASE may be generated anew. For a move, the simplest is to keep the old value; then nobody notices anything. A clone that keeps running next to the original gets its own: otherwise links from one instance's mails also work on the other.

A new SECRET_KEY_BASE has these effects:

  • Everyone has to sign in again.
  • Links from mails sent before no longer work: unsubscribing from notifications, resetting a password, unlocking an account. Whoever needs such a link requests a new one or changes the notification settings.
  • Images in texts and mentions of other notes show a placeholder, because their identifiers are signed with the key. The files are kept, even if somebody edits the text meanwhile.

A task brings images and mentions back; run it once after the first start. First as a dry run that writes nothing, then for real:

docker compose exec -e DRY_RUN=1 web bin/rails rich_text_embeds:resign_sgids
docker compose exec web bin/rails rich_text_embeds:resign_sgids

Without Docker: RAILS_ENV=production bin/rails rich_text_embeds:resign_sgids, with DRY_RUN=1 in front for the dry run.

The task does not need the old key. It re-signs an identifier only when the same target is stored next to the text: the image as an attachment of that text, the mentioned note as a reference. Whatever it cannot match, it lists. A note that mentions itself is one of those; set that mention again by hand. A second run changes nothing.

The task is available from the first release after 2026.10.0.