584 Words

💾 Desktop backups with restic

For servers I use Bareos and tape. For my own laptop that is far too much: there I use restic. It is one program, it encrypts everything, and it only stores what changed, so a daily backup takes seconds.


The 3-2-1 rule

A backup is only a backup if it survives the disaster:

RuleExample
3three copies of your datathe original + two backups
2on two different medialaptop SSD + USB disk
1one copy somewhere elsea server, a cloud bucket, or a disk at a friend’s house

And the hidden fourth rule: test your restores. A backup you have never restored is a hope, not a backup.


1. Install restic

sudo apt install restic      # Debian/Ubuntu
sudo dnf install restic      # Fedora
restic self-update           # optional: the newest version (when not installed by a package)

2. Create a repository

A repository is where restic stores the backups. It can be a folder (on a USB disk), a server over SFTP, or cloud storage.

# a password file, so you don't have to type it (keep a copy in your password manager!)
mkdir -p ~/.config/restic
openssl rand -base64 32 > ~/.config/restic/password
chmod 600 ~/.config/restic/password

# the repository on a USB disk
restic init --repo /run/media/$USER/BACKUP/restic --password-file ~/.config/restic/password

⚠️ Without the password your backups are lost for good: restic encrypts everything. Store it somewhere outside the backup.

Other places to keep a repository:

sftp:me@nas.local:/srv/restic          # any server you can SSH into
rest:https://backup.example.com/me     # restic's own rest-server
s3:s3.amazonaws.com/my-bucket/restic   # S3 or S3-compatible storage
b2:my-bucket:restic                    # Backblaze B2

To avoid typing the options every time, put them in an environment file, ~/.config/restic/env:

RESTIC_REPOSITORY=/run/media/benjamin/BACKUP/restic
RESTIC_PASSWORD_FILE=/home/benjamin/.config/restic/password

and load it with set -a; source ~/.config/restic/env; set +a.

3. Make a backup

restic backup ~ \
  --exclude-caches \
  --exclude ~/.cache --exclude ~/Downloads --exclude '**/node_modules'

The first run copies everything; the next runs only add what changed.

4. Look at your backups

restic snapshots                         # all snapshots
restic ls latest | less                  # the files in the newest one
restic diff 1a2b3c4d latest              # what changed between two snapshots

5. Restore

# one folder from the newest snapshot, into /tmp/restore
restic restore latest --target /tmp/restore --include ~/Documents

# or browse all snapshots like normal folders
mkdir -p /tmp/restic && restic mount /tmp/restic

6. Keep it tidy

# keep 7 daily, 4 weekly and 12 monthly snapshots, remove the rest
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune

# check that the repository is healthy (do this now and then)
restic check

7. Every day, automatically (systemd timer)

A systemd user timer runs the backup without root. Create ~/.config/systemd/user/restic-backup.service:

[Unit]
Description=restic backup of my home folder

[Service]
Type=oneshot
EnvironmentFile=%h/.config/restic/env
ExecStart=/usr/bin/restic backup %h --exclude-caches --exclude %h/.cache --exclude %h/Downloads
ExecStartPost=/usr/bin/restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 12 --prune

and ~/.config/systemd/user/restic-backup.timer:

[Unit]
Description=Daily restic backup

[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=30m

[Install]
WantedBy=timers.target

Turn it on:

systemctl --user daemon-reload
systemctl --user enable --now restic-backup.timer
systemctl --user list-timers               # when does it run next?
journalctl --user -u restic-backup -e      # what happened last time?

Persistent=true makes up for a missed run, for example when the laptop was off. More about services and timers in systemd basics.


restic or borg?

BorgBackup works much the same way and is also excellent. I chose restic because it is one binary and can write straight to cloud storage; borg needs borg on the server too, but is a bit faster and has more compression options. I use both: see Backups with BorgBackup.