Intro

It’s common sense that backups are important, but do we actually configure it properly to run every X hours or do we rely on remembering to backup stuff every once in a while manually?

In this post we’ll learn how to leverage the backup software restic and the cp-like command for cloud storage rclone to create incremental backups stored both in a local copy in a hard disk and also in a cloud service (Google Drive, OneDrive, etc). At the end we’ll also explore how to use systemd to schedule regular backups.

Basically we’ll use restic to create a encrypted backup repository in a local hard drive and then we’ll use rclone to upload this backup to a storage provider. Since all your data is encrypted there is no problem in just uploading all the data directly to a remote server. (But you could also choose to encrypt the data again with rclone before uploading).

Basic setup

Before we worry about how to create these backups we need to ensure we have the necessary programs. You can find the installation instructions for both softwares below, but note that if you are running any Linux distro it’s likely that you’ll be able to install it via your package manager.

Restic

After installing restic, you’ll need to create a new repository. I strongly advise you to read the official doc regarding this.

Rclone

Same as before, you’ll need to configure rclone. Please have a look into the official documentation about how to configure your desired storage provider.

Creating your local and remote backup

Assuming you already read and understand how restic works, we’ll just need to create a backup repository:

restic --repo /path/to/repo init

Than we’ll need to choose which files/directories you want to backup. I’ll keep things simple here, but there are a lot more details on the official docs, please take some time to read and learn all the possibilities restic offers you.

restic --repo /path/to/repo backup /directory/to/backup

After backing up your files, we need to upload them to a remote storage. (It’s advisable to have redundancy of data).

We just need to use rclone to synchronize all the local files to a remote storage. More details in the official doc.

rclone sync /path/to/repo remote_storage_name:path/to/remote/backup

Scheduling recurring backups

In this step, if you are in an operating system other than Linux you will have to search for a similar way to schedule scripts. For linux, if your distro adopts systemd, you could create units for both restic and rclone. I’ll leave below examples for both service and timer units:

File restic-backup.service:

[Unit]
Description=Restic - Backup
Documentation=man:restic(1)

[Service]
Type=exec
ExecCondition=test -z "$(/usr/bin/restic --repo /path/to/repo --password-file /path/to/password-file list locks --no-lock --quiet)"
ExecStart=/usr/bin/restic --repo /path/to/repo --password-file /path/to/password-file backup /home/user --tag incremental
# Resource control
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7

File rclone-sync-backup.service:

[Unit]
Description=Rclone - Sync Local Restic Backup Into {YOUR_CLOUD_STORAGE}
Documentation=man:rclone(1)

[Service]
Type=exec
ExecCondition=test -z "$(/usr/bin/pgrep -af rclone)"
ExecStart=/usr/bin/rclone sync --verbose --create-empty-src-dirs /path/to/repo remote_storage:path/to/repo
# Resource control
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7

Note that for both services we specify ExecCondition, which is important because we only want to run the backup and synchronization steps if there is no other backup instance running already.

The content for the timer for both services can be the same if you wish, but you’ll still have to create two different timers (restic-backup.timer and rclone-sync-backup.timer):

[Unit]
Description=(Restic/Rclone) - Backup Timer

[Timer]
OnBootSec=15min
OnUnitActiveSec=5h
RandomizedDelaySec=1h

[Install]
WantedBy=timers.target

Once you create those files, I’d advise you to place them for only your user. To do that, you’ll need to create the directory for your own systemd files:

mkdir -p ~/.config/systemd/user

And then you move your config files there:

mv restic-backup.service rclone-sync-backup.service restic-backup.timer rclone-sync-backup.timer ~/.config/systemd/user

Finally, you must enable and start the timers:

systemctl --user daemon-reload
systemctl --user enable --now restic-backup.timer
systemctl --user enable --now rclone-sync-backup.timer

And that’s it! :)

Now you have your backups configured to run every few hours incrementally, so you’ll never need to worry about making them manually again (hopefully :P).

I suggest you to take your time reading both official docs for restic and rclone, as there is waaay more cool things that can be done with this programs.

That’s it for now!

All the best & keep learning.