Setting Up Backup Snapshots on Linux

For some time I’ve been looking for a backup solution for Linux that can periodically take snapshots of data, allowing me to go back in history of any file just like git. I finally found restic which fits these requirements. Here is how I set it up to take snapshots of particular directories, say every 15 minutes.

Installing restic

Though restic is available in repositories of almost all Linux distros, I recommend downloading the latest release directly from GitHub to avoid dealing with potentially outdated version. Helpfully, you can stay current with restic release with restic self-update.

Initializing restic

Restic has a notion of “repository” which is where it stores backup snapshots. You first need to create/initialize a repo before create any backups:

mkdir ~/restic-repo
rust init ~/restic-repo

Initialization steps requires a password for the repo to keep it encrypted. This password has to be provided in all future invocations of restic which refer to this newly created repo. To avoid specifying password directly on the command line, you can create a file like ~/.restic_passwd with your password as the only content. This file must have permissions set such that only you can read/write this file: chmod 600 ~/.restic_passwd.

Creating backup snapshots

Now you can backup any folder(s) to this repo with:

restic -r ~/restic-repo -p ~/.restic_passwd dir1 dir2 dir3

Of course, replace dir1 etc. with directories you actually want to backup. Once you have verified that this command works fine, let’s put it in a script file We will call this script from service files below.

~/bin/ be sure to specify directorie(s) you actually want to backup in script below



restic -r "$REPO" -p "$PASSWD" --verbose backup dir1 dir2 dir3
restic -r "$REPO" -p "$PASSWD" --verbose forget --keep-within 1m --prune

This script also prune/deletes snapshots more than 1 month old. If you want to keep and infinite history, just comment out the last line.

Scheduling backups

Now that you can create backup snapshots on demand, it’s time to automate this process and make it happen, say every 15 minutes. On Linux, the best way to schedule such periodic jobs is to use systemd (fear not!). For this, you need to create two files in ~/.config/systemd/user/; create this directory if it doesn’t already exist. Ok, here is content of these two files:

  • ~/.config/systemd/user/backup.service tells systemd what command to execute

    Description=Restic Backup service
  • ~/.config/systemd/user/backup.timer tells systemd how frequently to run this service

    Description=Run backup every 15 minutes

    You can change how frequently the service is run by modifying OnCalendar= line.

Finally enable this service with:

systemctl --user enable --now backup.service

Restoring a snapshot

There are many ways for restoring any snapshot of data from a restic repo, from dumping a particular file to mounting the restic repo. The official documentation on restoric backups covers this well.

There’s much more restic can do like directly backing up to a remote site using rclone, but a local snapshot style backup is all I needed for now.

comments powered by Disqus