Almost every server has a backup job. Far fewer have a backup that someone has restored. The difference shows up on the worst possible day: the job ran every night, but it skipped the database, the password is lost, or nobody knows how long a full restore takes.

This is the routine we set up with restic, an open-source backup program. It encrypts everything before it leaves the server, stores files only once even if they appear in many backups, and works with S3-compatible storage, SFTP and its own REST server.

The Repository and Its Password

Keep the settings in a file only root can read:

sudo install -d -m 700 /etc/restic
sudo nano /etc/restic/env
sudo chmod 600 /etc/restic/env
RESTIC_REPOSITORY=s3:https://s3.example.com/backups/web-01
RESTIC_PASSWORD_FILE=/etc/restic/password
AWS_ACCESS_KEY_ID=...
AWS_SECRET_ACCESS_KEY=...

Generate the password, create the repository, and store a copy of the password outside the server, in your password manager. Without it, the backups cannot be decrypted by anyone, including you.

sudo sh -c 'openssl rand -base64 32 > /etc/restic/password && chmod 600 /etc/restic/password'
sudo sh -c 'set -a; . /etc/restic/env; restic init'

What to Back Up

Files that change only when someone edits them can be copied as they are: /etc, application code and configuration, uploaded files.

A running database cannot. Copying its files while it writes produces a backup that may not start. Back up a dump instead. restic 0.17 and newer can run the dump itself and fail the backup if the dump fails:

restic backup --stdin-from-command --stdin-filename app.dump -- \
  runuser -u postgres -- pg_dump -Fc app

Run It Every Night

A systemd service runs the backup, then removes old snapshots by a retention rule:

# /etc/systemd/system/restic-backup.service
[Unit]
Description=restic backup

[Service]
Type=oneshot
EnvironmentFile=/etc/restic/env
Nice=10
IOSchedulingClass=idle
ExecStart=/usr/bin/restic backup --exclude-caches /etc /srv/app
ExecStart=/usr/bin/restic backup --stdin-from-command --stdin-filename app.dump -- runuser -u postgres -- pg_dump -Fc app
ExecStart=/usr/bin/restic forget --keep-daily 7 --keep-weekly 5 --keep-monthly 12 --prune
# /etc/systemd/system/restic-backup.timer
[Unit]
Description=Nightly restic backup

[Timer]
OnCalendar=*-*-* 03:15:00
RandomizedDelaySec=15min
Persistent=true

[Install]
WantedBy=timers.target
sudo systemctl daemon-reload
sudo systemctl enable --now restic-backup.timer

Persistent=true runs a missed backup after the server was off at 03:15. The steps run in order and stop at the first failure, so old snapshots are never removed after a failed backup. Add OnFailure= with a unit that notifies you, because a backup that fails silently is the same as no backup.

Check the Data, Not Just the Job

restic check verifies the repository’s structure. With --read-data-subset, it also downloads part of the data and verifies it against its checksums. Reading 5% a week covers the whole repository over time without downloading everything:

sudo sh -c 'set -a; . /etc/restic/env; restic check --read-data-subset=5%'

The Restore Test

Once a month, restore into a scratch location and look at the result:

sudo sh -c 'set -a; . /etc/restic/env; restic restore latest --target /tmp/restore-test --include /etc/nginx'
sudo diff -r /etc/nginx /tmp/restore-test/etc/nginx

Restore the database into a separate, empty database and query it:

sudo -u postgres createdb restore_test
sudo sh -c 'set -a; . /etc/restic/env; restic dump latest /app.dump' | sudo -u postgres pg_restore --dbname restore_test
sudo -u postgres psql restore_test -c 'select count(*) from orders;'

Write down how long each step took. That number is how long the service will be down if the server is lost, and it is the answer to the first question anyone asks during an incident.

Protect the Backups From the Server

The server holds credentials that can write to the repository, so an attacker with root on the server can usually delete it too. Keep a second copy the server cannot remove: restic’s REST server started with --append-only accepts new backups and refuses deletions. Run forget and prune for that copy from a separate, trusted machine.

Together with the first copy, that makes three copies of the data on two kinds of storage, one of them away from the server: the old 3-2-1 rule, which still holds.