My backup strategy to USB disk using duply
I don't have a lot of data to backup - just my home folder (on my Archlinux laptop) which just has configuration for all the tools I'm using and my programming work.
For photos or videos taken from my phone, I use google photos for backup - which works pretty well. Even if I delete the original files from my phone, the photos app still keeps them online.
Coming back to my laptop, I'm currently using duplicity (with the duply wrapper) to backup to multiple destinations. Why multiple locations? I wanted one local copy so that I can restore fast, and at least one at a remote location so that I can still restore if the local disk fails.
For off-site, I'm using the fantastic rsync.net service. For local backups, I'm using two destinations: a USB HDD at my home, and a NFS server at my work. Depending on where I am, the backup will be done to the correct destination.
This post will deal with the backups to my local USB disk.
Here's what I've been able to achieve: the backups will run every hour as long as the USB disk is connected. If it is not connected, the backup script will not even be triggered. I did not want to see backup failures in my logs if the HDD is not connected.
I've done this using a systemd timer and service. I've defined these units in the user-level part for systemd so that root privileges are not required.
Mounting the USB Disk
To automatically mount the USB disk, I've added the following line to my
Duply config for running the backup
Here's my duply config file (kept at
~/.duply/ext01/conf) (mostly self-explanatory):
This can be run manually using:
Exclusions can be specified in the file
~/.config/ext01/exclude in a glob-like format.
Systemd Service for running the backup
Next, here's the service file (kept at
Requires option says that this unit has a dependency on the mounting of Ext01. The
After option specifies the order in which these two should be started (run this service after mounting).
After this step, the service can be run manually (via systemd) using:
Systemd timer for triggering the backup service
Next step is triggering it automatically every hour. Here's the timer file (kept at
BindsTo option defines a dependency similar to the
option above, but also declares that this unit is stopped when the
mount point goes away due to any reason. This is because I don't want
the trigger to fire if the HDD is not connected.
Persistent=true option ensures that when the timer
is activated, the service unit is triggered immediately if it would have
been triggered at least once during the time when the timer was
inactive. This is because I want to catch up on missed runs of the
service when the disk was disconnected.
After creating this file, I ran the following to actually link this timer to mount / unmount events for the Ext01 disk:
That's it. Now, whenever I connect the USB disk to my laptop, the timer is started. This timer triggers the backup service to run every hour. Also, it takes care that if some run was missed when the disk was disconnected, then it would be triggered as soon as the disk is connected without waiting for the next hour mark. Pretty cool!
- Changing any systemd unit file requires a
systemd --user daemon-reloadbefore systemd can recognize the changes.
- The systemd documentation was very helpful.
Although it would be similar, but I'll also document how to do the above with NFS or SSHFS filesystems (instead of local disks). The major difference would be handling loss of internet connectivity, timeouts, etc.