Plain text

backup

#linux #backup

I often have to transfer files from other computers and work on them in my laptop. So I walk around with an USB stick in my key chain precisely for that. Having all my precious files at hand at all times is very useful, and the following systemd-service is what makes it possible.

It does one very simple thing: detects when my USB stick is mounted, and runs rsync to copy all files to a folder in my /home directory. This tool has seen a few iterations, and at first it required an udev rule and a script. Now, this version the most simple and elegant I could come up with.

To define which USB device should be copied, use the partition label. It can also be found using sudo systemctl list-units -t mount. In this case my usb is called run-media-USER-SANDISK.

Create the systemd unit file at /etc/systemd/system/usb-copy.service

[Unit]
Description=Backup USB contents
Requires=run-media-USER-SANDISK.mount
After=run-media-USER-SANDISK.mount

[Service]
ExecStart=/usr/bin/rsync -a --delete /run/media/USER/SANDISK /home/USER/

[Install]
WantedBy=run-media-USER-SANDISK.mount

Reload the systemd-daemon with sudo systemctl daemon-reload

This unit will silently run the command when the run-media-USER-SANDISK.mount unit appears.