Restic Backup

Photo by benjamin lehman

After 5 years of using ThinkPad X1 Carbon I’ve decided to switch to M2 Air. It took me a while to get comfortable with the thought that I will not have native docker and that my ThinkPad is old. Don’t get me wrong, there’s nothing wrong with ThinkPad. Also my side project has made me much more ramen money than M2 Air retail price, so I’ve been convinced to give it a go.

Now the essence of the post: Restic is a backup tool that I use to backup my documents on the laptops. It’s a great solution for Linux (I’ve wrapped restic into the systemd service) and I found out it also works on MacOS. This post is a message to my future self in case I will need to set up restic on my Mac again.

First, you need to install restic on your system:

brew install restic

That was an easy one. Now let’s set the credentials to the repository so that we could get the latest backup copy locally:

#!/usr/bin/env bash
# this file: ~/src/restic/env.sh, useful for interactive experiments
export RESTIC_REPOSITORY="azure:backups:/"
export RESTIC_EXCLUDE_FILE="/Users/xobb/src/restic/backup_exclude"
export RESTIC_PASSWORD_FILE="/Users/xobb/src/restic/pw.txt"
export AZURE_ACCOUNT_NAME="your-account-name"
export AZURE_ACCOUNT_KEY="your-account-key"
export BACKUP_PATHS="/Users/xobb/Documents"  # or any other paths you want to backup

Now it’s time to see what’s in our repository:

> cd ~/src/restic
> source env.sh  # Source the environment variables for restic
> restic snapshots

ID        Time                 Host           Tags           Paths
----------------------------------------------------------------------------------
c4e5e4b4  2021-11-14 13:11:37  thinkpad                      /home/xobb/Documents
21b37b79  2021-11-30 00:00:05  thinkpad       systemd.timer  /home/xobb/Documents
...
----------------------------------------------------------------------------------

The snapshots are successfully fetched. Let’s try restoring the snapshot:

> restic restore c4e5e4b4 --target ~/ResticBackups
# and let's check the folder
> ls -la ~/ResticBackups

Great, we’ve restored one of the backups. Next one is creating a backup of our Documents folder:

> restic backup ~/Documents

And now let’s put it all together:

#!/usr/bin/env sh
# this file: ~/src/restic/backup.sh
export RESTIC_REPOSITORY="azure:backups:/"
export RESTIC_EXCLUDE_FILE="/Users/xobb/src/restic/backup_exclude"  # this is the list of files and paths you want to exclude from the backup
export RESTIC_PASSWORD_FILE="/Users/xobb/src/restic/pw.txt" # this is where you put the password to the repository
export AZURE_ACCOUNT_NAME="your-account-name"
export AZURE_ACCOUNT_KEY="your-account-key"
export BACKUP_PATHS="/Users/xobb/Documents"  # or any other paths you want to backup

# Make backup and show the notification on the result
/opt/homebrew/bin/restic backup --exclude-file $RESTIC_EXCLUDE_FILE $BACKUP_PATHS && \
    osascript -e 'display notification "Backup Complete" with title "Restic"' || \
    osascript -e 'display notification "Backup Failed" with title "Restic"'

# forget the old backups and prune the repository, modify to match your policy
/opt/homebrew/bin/restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune

As we have this in one script, we can do a cronjob that will run the backup on schedule. I’ve decided to run the backup at the end of my business day at 6:05pm. Your schedule might be different:

crontab -l
5 18 * * *       $HOME/src/restic/backup.sh

Now don’t forget to make your folder private from prying eyes:

chmod 0700 -R ~/src/restic

I will be updating the post in case I find anything else that might be useful.