6

When I plug a HD via USB, it should a) mount it b) run my backup script.

What I currently have is backintime.service

[Unit]
Description="Runs backintime to backup."

[Service]
User=user
ExecStart=/usr/bin/systemd-inhibit --what=sleep --why="Backup" "backintime -b"

To start the backup script and backup.mount

[Unit]
Description=Backup Disk
Before=backintime.service

[Mount]
What=/dev/disk/by-uuid/931129c7-7f15-4042-9a69-796c9eb8ffc9
Where=/media/backup

But how do I execute it automatically?

4

I'm struggling to implement a very similar system. I got it to work, but I still want to tweak this and that (see this more general question: systemd - umount device after service which depends on it finishes).

Here's a working solution to your problem:

backup.service

[Unit]
Description=<DESCRIPTION HERE>
BindsTo=<STORAGE DEVICE UNIT HERE>.device mnt-backup.mount
After=<STORAGE DEVICE UNIT HERE>.device mnt-backup.mount

[Service]
ExecStart=<CALL TO BACKUP SCRIPT HERE>

Note: to get a list of the storage device units use systemctl list-units --all --full | grep disk

You might want to add RefuseManualStart=yes under [Unit] too.

mnt-backup.mount

[Unit]
DefaultDependencies=no
Conflicts=umount.target
Before=umount.target

[Mount]
What=/dev/disk/by-uuid/<DEVICE UUID HERE> 
Where=/mnt/backup
Type=<FILESYSTEM HERE>

You pretty much got here, now to automaticaly start backup.service, we will be using a udev rule.

Here also you might want to add RefuseManualStart=yes under [Unit].

90-backup.rules

KERNEL=="sd*", ATTRS{serial}=="<HD SERIAL HERE>", TAG+="systemd", ENV{SYSTEMD_WANTS}+="backup.service"  

Note: to get a list of the attributes of your specific device, include its serial number, use udevadm info -a -n /dev/sd*

| improve this answer | |
  • To get it work I suppose one runs sudo systemctl enable both service and mount files – George Udosen Sep 12 '17 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.