Cedar Shell

Backing up a small VPS with restic and a systemd timer

For years my backup plan for this server was a tarball I made when I remembered. That works until the week you need it. I finally moved to restic, and the setup was small enough that I should have done it much earlier.

What gets backed up

Not much: /etc, /var/www, my home directory, and a nightly dump of one SQLite file. Everything else can be reinstalled from a package list, which I also save.

dpkg --get-selections > /root/pkg-selections.txt
restic backup /etc /var/www /home /root/pkg-selections.txt \
  --exclude-caches --tag nightly

The timer

I used a systemd timer instead of cron because I wanted Persistent=true: if the machine is off at 03:30, the job runs when it comes back.

[Timer]
OnCalendar=*-*-* 03:30:00
RandomizedDelaySec=20m
Persistent=true

Pruning

Seven dailies, four weeklies, six monthlies. The repository settled at about 1.4 GB, and a nightly run takes under a minute because almost nothing changes.

restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

The part I skipped

I did not test a restore for the first two months. When I did, the restore itself was fine, but I found that my SQLite dump was being written after the backup ran, so every snapshot held the dump from the night before. One ExecStartPre= line fixed it. Test the restore.