Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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*

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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