lpaste.de

rsync + systemd = simple backups

Intro

Backups are one of those things I know I should do regularly but rarely do. Although I have never had a SSD or HDD fail, there is always this uneasy feeling when I have no backup or no recent backup. This used to be the case but, recently, I invested some time and came up with the following backup solution to perform simple, non-incremental, non-compressed backups.

rsync + systemd = backups

My backup solution is a combination of systemd and the rsync command. It runs weekly backups and copies the changed data from my main SSD to my backup HDD which are both internal "drives" of my desktop computer.

systemd

The systemd suite provides all the necessary utilities to run time-triggered backups as well as automatically mounting and unmounting the partition for the backup destination. Each of the three required configuration files is described in one of the following sections.

Service

The service unit file specifies the commands/script that should be executed to perform the backup. In my case, these are multiple rsync commands.

/etc/systemd/system/back-up.service
[Unit]
Description=Back up data from SSD to HDD

[Service]
User=<user> # sets the user that the commands are executed as
Group=<user> # same for the group
Type=oneshot # enables to execute multiple commands
ExecStart=/usr/bin/rsync -a --quiet /home/<user>/Documents /mnt/backup/<user>/
ExecStart=/usr/bin/rsync -a --quiet /home/<user>/Music /mnt/backup/<user>/
ExecStart=/usr/bin/rsync -a --quiet /home/<user>/Pictures /mnt/backup/<user>/
ExecStart=/usr/bin/rsync -a --quiet /home/<user>/Videos /mnt/backup/<user>/

All rsync commands are invoked sequentially in the same order as specified in the unit file. If one command fails, subsequent commands are not executed and the unit fails. For test purposes, the unit can be manually started using the systemctl start back-up.service command.

The service unit file does not require a [Install] section since it is the timer unit that is enabled.

Preparing the backup destination folder

Before starting the backup service unit, make sure that the backup destination folder exists and that the backup unit has the necessary rights to access the folder. It is important to make the backup folder only accessible to the intended user by executing the following commands to change the mode bits and the owner of the folder:

chmod 700 /mnt/backup/<user>/
chown <user>:<user> /mnt/backup/<user>/

Timer

The timer unit file defines when the service unit should be triggered. It should have the same name as the service unit file it controls or otherwise needs to specify the unit it activates by adding Unit= and the name of the unit it triggers in the [Timer] section.

/etc/systemd/system/back-up.timer
[Unit]
Description=Run weekly backups

[Timer]
OnCalendar=Sun 13:00
Persistent=true

[Install]
WantedBy=timers.target

Automount

The automount unit automatically mounts the partition of the backup destination when it is used. Since systemd generates the mount unit files from the /etc/fstab file at boot time, we only have to add a line to this file that looks similar to the following.

/etc/fstab
# /dev/sdb3 LABEL=arch_data
UUID=91a2142f-a1f8-4d3d-8dba-60375c67380b /mnt/backup ext4 noauto,x-systemd.automount,x-systemd.idle-timeout=2min 0 2

The noauto option tells systemd that it should not mount this partition at boot while the x-systemd.automount tells systemd to automatically mount this partition when it is used. The last option x-systemd.idle-timeout=2min tells systemd to unmount the partition if it is idle for more than 2 minutes.

Everything you need to know about your backup device can be gathered from the output of the lsblk -f command.

Activating the backup timer

Finally, the timer unit needs to be started and enabled using the systemctl start back-up.timer and systemctl enable back-up.timer commands.

All active systemd timers can be listed with the systemctl list-timers command.

Conclusion

In the future, I might change to a more sophisticated backup script that performs incremental backups or to a different file system with snapshot support such as btrfs but for now, this solution is sufficient and simple. However, I hope you enjoyed reading this blog post.

← Home