Restic backups on your systemd-enabled Linux desktop distro

I’ve been using Restic backup on several servers for more than a year now. As the software turned out to be very reliable and fast I decided to give it a try on my laptop. Until now I had to trigger my backup mechanism by hand every day, but as you can image this is not a very reliable way to make backups.

Today I looked for a way to automate Restic backups on my laptop while keeping the mechanism easy and inconspicuous. In this post I’d like to introduce you to my personal backup solution.

Create backup script

File: ~/.local/bin/backup

#!/bin/bash

source /home/thomas/.config/restic/environment.sh

notify-send "Restic backup started."

echo "Creating incremental backup ..."
### Backup new stuff
restic backup \
        --verbose \
        --files-from /home/thomas/.config/restic/backup.files \
        --exclude-file /home/thomas/.config/restic/exclude.files

### Remove old stuff
echo "Deleting old backups ..."
restic forget \
        --keep-last 7 \
        --keep-daily 14 \
        --keep-weekly 4 \
        --keep-monthly 6

# restic -r $BACKARCH check
echo "Don't forget to run \"restic check\" from time to time"
echo "Backup finished."

notify-send "Restic backup finished."

Make the new shell script executable for the current user:

chmod u+x ~/.local/bin/backup

Create backup config

File: ~/.config/restic/environment.sh

#!/bin/bash
export RESTIC_REPOSITORY="sftp:thomas@rock64:/path/to/my/backup/repo"
export RESTIC_PASSWORD="mypasswordforbackuprepo"

environment.sh defines the location of the Restic backup archive. This configuration is for an SFTP location on a target named “rock64”. RESTIC_PASSWORD represents the password which was set up when you created the Restic archive.

File: ~/.config/restic/backup.files

/home/thomas

backup.files contains all paths to directories or files that i’d like to include into my backup. In general it’s a good idea to just name the user’s home directory. You can name multiple paths after another - in one line each.

File: ~/.config/restic/exclude.files

.local/share/Trash
.cache

exclude.files contains all paths to directories that should not be included in the backup. In my case this is cached files and my “trash” directory.

Create systemd services

Now create a new directory for user-specific systemd services (if it doesn’t exist yet):

mkdir -p ~/.config/systemd/user/

… and place a new service file in it:

File: ~/.config/systemd/user/restic-backup.service

[Unit]
Description=Restic backup service for Thomas

[Service]
ExecStart=/home/thomas/.local/bin/backup

[Install]
WantedBy=default.target

… as well as a timer file. The timer file is responsible for starting the service on certain time intervals. I chose to start a backup job 2 minutes after I’ve started my computer. After that the job is restarted every 24 hours.

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

[Unit]
Description=Timer for restic-backup

[Timer]
OnBootSec=2m
OnUnitInactiveSec=1d
Persistent=true

[Install]
WantedBy=basic.target

Reload the systemd service definitions and enable restic-backup:

systemctl --user daemon-reload
systemctl --user enable restic-backup.timer
systemctl --user enable restic-backup.service

… and you’re done! You can check if everything works by rebooting your PC. A backup should be started after ~ 2 minutes after startup. You can check your logs via:

journalctl --user -u restic-backup