Delta backups with restic to Google Drive
A daily restic backup of a server to Google Drive through rclone: only changed chunks upload, old snapshots expire on a schedule, and the job yields to everything else.
I back up my server every night to Google Drive. The job uploads only what changed since the last run, and it keeps a bounded history. I built it from restic, rclone and a systemd timer. This post describes how it works and the mistakes I would warn you about.
Why restic
Restic splits files into chunks by content. It stores each chunk once, encrypted, and every snapshot is a list of chunks. A file that did not change costs almost nothing on the next run. A large file that changed in one place uploads only the new chunks. Each snapshot still looks like a full backup when I restore it.
Restic decides which files to read by comparing their metadata with the parent snapshot. So a nightly run on a mostly unchanged disk finishes quickly.
Restic does not speak the Google Drive API. Rclone does, and restic can use rclone as a backend. A repository path looks like rclone:my-remote:folder. I set up the Drive remote once with rclone config. Drive asks for an OAuth login at that step. After that the backup runs without me.
The script
#!/usr/bin/env bash
set -eo pipefail
export RESTIC_REPOSITORY="rclone:my-remote:backups"
export RESTIC_PASSWORD_FILE="$HOME/.config/restic/password"
EXCL="$HOME/.config/restic/excludes.txt"
OLD="$HOME/.cache/old-session-files.txt"
find "$HOME/.agent-sessions" -type f -mtime +30 > "$OLD"
restic cat config >/dev/null 2>&1 || restic init --repository-version 2
restic backup "$HOME" /srv/projects \
--tag nightly --host my-host --one-file-system \
--compression max \
--exclude-larger-than 100M \
--exclude-file "$EXCL" \
--exclude-file "$OLD"
restic forget --tag nightly --host my-host \
--keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
The names are placeholders. Here is what each part does.
The repository string and the password file stay outside the script body. The password file has mode 600. Restic cannot read an encrypted repository without the password. If I lose the password, I lose the backup. Keep a second copy of it on another machine.
The line with restic cat config tests whether the repository exists. If it does not, restic init creates it. I pass --repository-version 2 because compression needs version 2.
The flag --one-file-system keeps restic inside the disk it starts on. It skips mounted network drives and virtual folders. The flag --exclude-larger-than 100M drops any file above that size. Large build artifacts and media files are the usual cause. A backup of source and notes rarely needs them.
Restic has no filter for the age of a file. I wanted to drop old session logs, so a find command lists files older than 30 days into a file. Restic reads that list as an exclude file. The exact paths are the patterns.
Symlinks are not followed
Restic stores a symlink as a symlink. It does not back up the data on the other side. My projects folder in the home directory is a link to another disk. A backup of the home folder alone holds only the link. The fix is the second path in the command: I list the real directory next to the home directory. Check for this in your own setup. The backup of a link looks fine in the snapshot list and contains no data.
Tags, host and forget
I add --tag nightly --host my-host to every backup. The forget command uses the same two filters. It then touches only the snapshots of this job. If another machine or another job writes to the same repository, its snapshots stay alone.
The policy keeps the last 7 daily snapshots, 4 weekly and 6 monthly. The flag --prune removes the chunks that no remaining snapshot uses. Pruning rewrites data in the repository, and it cannot be undone. When I change the policy, I run the command with --dry-run first and read the list of snapshots it would remove.
The schedule
A systemd timer starts the job at night. The service is a oneshot unit.
# backup.service
[Service]
Type=oneshot
ExecStart=%h/bin/backup.sh
Nice=10
IOSchedulingClass=idle
# backup.timer
[Timer]
OnCalendar=*-*-* 03:30:00
Persistent=true
Two settings matter most. With Persistent=true, systemd runs a missed backup when the machine comes back from downtime. With Nice=10 and IOSchedulingClass=idle, the backup gives way to everything else on the host. Parallel coding agents run on the same server, and a backup must never slow them down.
Check the backup
A backup that I never restored is a guess. These commands take a short time and show the state:
restic snapshots --tag nightly
restic check
restic restore latest --target "$HOME/.cache/restore-test" --include "/path/to/one/file"
The first lists the snapshots. The second checks the structure of the repository. The third restores one file into a scratch folder. Compare that file with the original.