← Knowledge Base
Server administrationFrom my setup3 min read · updated 2026-11-27

Encrypted nightly backups for a small server with restic

How I back up a small Ubuntu server with restic: nightly encrypted snapshots, retention, a weekly integrity check, a restore test and an off-server copy on my PC.

backupresticubuntucrondocker

A server you cannot restore is not backed up. This is the setup I use for a small Ubuntu 24.04 VM that runs Docker, Caddy and a static website. It has three layers, so that no single failure loses everything.

Layer What Protects against
1. Nightly encrypted snapshots restic repository on the server deleted files, bad edits, a broken config
2. Off-server copy the same encrypted repository copied to my PC losing the server or its disk
3. Cloud boot-volume backup a snapshot of the whole disk, taken manually before risky changes a VM that no longer boots

What goes into the backup

Only things that matter and that cannot simply be re-downloaded:

  • the project folder with docker-compose.yml, the Caddyfile and the generated website
  • the Caddy certificate volume (so a rebuilt server reuses the existing Let's Encrypt certificate)
  • /etc/ssh/sshd_config.d, /etc/fail2ban, /etc/iptables, /etc/docker, /etc/fstab, /etc/cron.d
  • the backup script itself and authorized_keys
  • a small "system facts" folder: package list, running containers, enabled services, live firewall rules, effective sshd settings

Large folders that can be rebuilt (in my case a gallery of converted pictures) are excluded with --exclude.

Set it up

Install restic, create a password file readable only by root, and let the script initialise the repository on first run:

sudo apt install restic
sudo sh -c 'umask 077; openssl rand -base64 33 > /root/.restic-password'
sudo install -m 750 restic-nightly-backup.sh /usr/local/sbin/backup-server.sh

Then schedule it. Cron on the server runs in UTC:

# /etc/cron.d/server-backup
20 3 * * * root /usr/local/sbin/backup-server.sh >> /var/log/server-backup.log 2>&1

The script takes a lock (flock) so two runs can never overlap, writes the time of the last success to /var/lib/backup-status/last_success, and treats restic's exit code 3 (some files unreadable) as a warning instead of a failure.

Retention and checks

On Sundays the script also prunes old snapshots and verifies a part of the stored data:

restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
restic check --read-data-subset=10%

Checking 10% each week means the whole repository is read over a couple of months without a long run every time.

Always test a restore

Backups that were never restored are a hope, not a backup. After the first run I restored the latest snapshot into a scratch folder and compared it with the live files:

export RESTIC_REPOSITORY=/home/ubuntu/backups/restic-repo RESTIC_PASSWORD_FILE=/root/.restic-password
restic snapshots
restic restore latest --target /tmp/restore-test
diff /tmp/restore-test/home/ubuntu/stack/Caddyfile /home/ubuntu/stack/Caddyfile && echo identical
restic check
rm -rf /tmp/restore-test

Files that must come back byte-identical: the Caddyfile, the compose file, the SSH hardening drop-in and the firewall rules. Also confirm that the certificate files in the Caddy volume were restored.

The off-server copy

A restic repository is just a folder of encrypted files, so copying it is enough. From the PC:

scp -r -P <ssh-port> ubuntu@<server>:/home/ubuntu/backups/restic-repo .\backups\server\

I copy to a new folder first, check that the config file exists, and only then swap it in, keeping the previous copy. Make the repository readable by the ubuntu user (but not writable) so the copy does not need root.

The password is the weak point

The repository cannot be opened without its password. Keep a copy somewhere that is not the server, for example a password manager. If both the server and the file on your PC are lost, the backups are unreadable.

Pitfalls I hit

  • Copying with a trailing backslash in a PowerShell path escapes the closing quote; pass the destination without it.
  • Docker volumes live under /var/lib/docker/volumes/<project>_<volume>/_data, readable only by root, which is why the backup runs as root.
  • Do not forget the cloud-provider level backup of the boot volume for the "VM will not boot" case; restic alone cannot fix that.

Downloads