archive2016

rsync backups with daily snapshots (--link-dest)

The rsync options I use to back up a small server, why a mirror is not enough, and a --link-dest script that keeps dated snapshots and survives a failed run.

4 min read#ops#rsync#backup#linux

My home server is backed up with rsync over SSH. rsync is installed almost everywhere and only copies what changed. I set this up a year ago. This is the second version of the snapshot script, changed after a failed run, and tested with a real restore.

The options

rsync -aHAX --numeric-ids --delete -e ssh \
  /srv/data/ backup@192.168.1.20:/backups/pi1/data/
  • -a is archive mode: recursive, and it keeps permissions, times, symlinks and owners.
  • -H keeps hard links, -A ACLs and -X extended attributes. Leave out -A and -X if the target file system does not support them.
  • --numeric-ids keeps user IDs as numbers. Without it rsync maps users by name, which is wrong when the backup host has different users.
  • --delete removes files on the target that are gone on the source, so the copy stays exact.

The trailing slash. rsync -a /srv/data/ target/ copies the contents of data into target. rsync -a /srv/data target/ copies the folder itself, so you get target/data/.... Mixed up together with --delete, this can delete a whole folder on the target. I put a slash on both sides, in every script.

--dry-run first. Every time I change a backup script, I run it once with --dry-run -v and read the list of files it would delete.

A mirror is not enough

With --delete, a file you delete by mistake is deleted on the backup at the next run, and a corrupted file is copied over the good one. A backup needs old versions.

--link-dest=DIR gives them cheaply. It tells rsync: if a file is the same as in DIR, do not copy it, make a hard link to it. A hard link is a second name for the same data on disk. So every daily folder looks like a full copy, and unchanged files use no extra space. I can open the folder from last Tuesday and see every file as it was then.

The script

#!/bin/bash
set -euo pipefail

SRC="pi@raspberrypi.local:/home/pi/"
DEST="/backup/raspberrypi"
TODAY=$(date +%Y-%m-%d)

mkdir -p "$DEST"

rsync -a --delete \
  --link-dest="$DEST/latest" \
  --exclude='.cache/' \
  "$SRC" "$DEST/$TODAY.partial"

rm -rf "$DEST/$TODAY"          # a second run on the same day replaces the first one
mv "$DEST/$TODAY.partial" "$DEST/$TODAY"
touch "$DEST/$TODAY"           # see "Removing old snapshots" below
ln -sfn "$DEST/$TODAY" "$DEST/latest"

It writes to a .partial folder and renames it only at the end. My first script wrote straight into the dated folder. Then one run failed in the middle and left a half-finished folder with a normal date name, which looked like a good backup. Now a failed run leaves only a .partial folder, and latest still points to the last good snapshot.

The rm -rf line matters when the script runs twice on one day. Without it, mv moves the new .partial folder into the existing dated folder instead of replacing it.

On the first run there is no latest yet. rsync prints a warning and makes a full copy, which is fine.

The script runs on the backup server and pulls from the Pi. Then a Pi that gets broken into cannot delete the old snapshots. The SSH key has no passphrase and is used only for this, and in the Pi’s authorized_keys it is limited to the backup host with from="192.168.1.20". Cron starts it at night:

30 3 * * * /usr/local/bin/backup-raspberrypi.sh >> /var/log/backup-raspberrypi.log 2>&1

Removing old snapshots

Keep 30 days:

find /backup/raspberrypi -maxdepth 1 -type d -name '20*' -mtime +30 -exec rm -rf {} +

This only works because of the touch in the script. rsync -a copies the modification time of the source folder to the snapshot folder. If nothing was added to or removed from /home/pi/ in the last 30 days, a new snapshot would look 30 days old, and this find would delete every snapshot, including today’s. touch sets the folder time to the time of the backup.

Deleting an old folder is safe. The data of a file is only freed when its last hard link is gone.

Limits

  • The destination must support hard links. ext4 is fine; FAT and most network shares are not.
  • A large file that changes a little every day, like a database file or a VM image, is stored again in full every day. For databases I back up a dump instead.
  • -a keeps file owners only when the receiving side runs as root. Otherwise everything belongs to the backup user.

The restore test

A backup is only proven by a restore. I restored a full Pi home folder from a snapshot onto a new card, and it worked. I repeat that test from time to time.