You are not logged in.
I've been trying to automate backup of my multimedia whenever I plug in a USB drive into the computer.
The way I'm doing this is as follows:
* Udev rule to create symlink to partition and run systemd service (udev-usb-mount@.service) which mounts that drive
* Second systemd service (external-backup@.service) to run rsync to copy files
I had originally hoped to use a systemd path unit to watch for when the drive was mounted and then start the backup but that doesn't seem to work. As far as I can tell, this is a limitation in inotify (which I assume systemd uses for path units):
If a filesystem is mounted on top of a monitored directory, no event is generated, and no events are generated for objects immediately under the new mount point. If the filesystem is subsequently unmounted, events will subsequently be generated for the directory and the objects it contains.
I therefore thought I could just use sequencing in the units as follows:
$ systemctl cat udev-usb-mount@videobackup.service
[Unit]
Description=Mount USB devices based on custom udev rule at %I
[Service]
Type=oneshot
ExecStart=/usr/bin/udev-usb-mount -m %i
ExecStop=/usr/bin/udev-usb-mount -u %i
RemainAfterExit=TrueThis mounts the usb drive to /mnt/usb/videobackup and works successfully.
$nsystemctl cat external-backup@films.service
[Unit]
Description=Backup folder to external USB drive - %I
ConditionDirectoryNotEmpty=/mnt/usb/videobackup
Wants=udev-usb-mount@videobackup.service
Requires=udev-usb-mount@videobackup.service
[Service]
Type=oneshot
# Backup Blu-rays
ExecStart=/usr/bin/external-backup \
-s /htpc/videos/bluray/ \
-d /mnt/usb/videobackup/bluray/ \
-p htpc
# Backup DVDs
ExecStart=/usr/bin/external-backup \
-s /htpc/videos/films/ \
-d /mnt/usb/videobackup/dvd/ \
-x ISOs/ \
-x extrathumbs/ \
-x nfo/ \
-x .Trash* \
-p htpc
# Lastly, backup films recorded from TVHeadend
# We sync drive after this one
ExecStart=/usr/bin/external-backup \
-s /htpc/videos/converted/films/ \
-d /mnt/usb/videobackup/recordedfilms/ \
-p htpc \
--sync
[Install]
WantedBy=udev-usb-mount@videobackup.serviceThe backup-script basically wraps rsync and works fine.
You'll note that the external-backup service includes a "ConditionDirectoryNotEmpty" to ensure that the drive is mounted (it has a folder structure on it already) before starting the backup.
However, once I plug in the usb drive and check the status of the two units, it looks like the backup is started before the mounting has completed so the condition fails:
$ systemctl status udev-usb-mount@videobackup
● udev-usb-mount@videobackup.service - Mount USB devices based on custom udev rule at videobackup
Loaded: loaded (/usr/lib/systemd/system/udev-usb-mount@.service; static)
Active: active (exited) since Sat 2020-09-19 17:18:52 BST; 27min ago
Main PID: 103266 (code=exited, status=0/SUCCESS)
Tasks: 1 (limit: 4624)
Memory: 1.3M
CGroup: /system.slice/system-udev\x2dusb\x2dmount.slice/udev-usb-mount@videobackup.service
└─103269 /usr/bin/mount.ntfs /dev/sdd1 /mnt/usb/videobackup -o rw
Sep 19 17:18:51 htpc systemd[1]: Starting Mount USB devices based on custom udev rule at videobackup...
Sep 19 17:18:52 htpc ntfs-3g[103269]: Version 2017.3.23 external FUSE 29
Sep 19 17:18:52 htpc ntfs-3g[103269]: Mounted /dev/sdd1 (Read-Write, label "MEDIABACKUP", NTFS 3.1)
Sep 19 17:18:52 htpc ntfs-3g[103269]: Cmdline options: rw
Sep 19 17:18:52 htpc ntfs-3g[103269]: Mount options: rw,allow_other,nonempty,relatime,fsname=/dev/sdd1,blkdev,blksize=4096
Sep 19 17:18:52 htpc ntfs-3g[103269]: Ownership and permissions disabled, configuration type 7
Sep 19 17:18:52 htpc systemd[1]: Finished Mount USB devices based on custom udev rule at videobackup.$ systemctl status external-backup@films.service
● external-backup@films.service - Backup folder to external USB drive - films
Loaded: loaded (/usr/lib/systemd/system/external-backup@.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/external-backup@films.service.d
└─override.conf
Active: inactive (dead)
Condition: start condition failed at Sat 2020-09-19 17:18:51 BST; 28min ago
Sep 19 17:18:51 htpc systemd[1]: Condition check resulted in Backup folder to external USB drive - films being skipped.Looking at the timestamps, the backup tried to run before the mount service had finished. My understanding was that oneshot services are only active once the "ExecStart" command has exited.
Can anyone see where I've gone wrong here?
Last edited by elParaguayo (2020-09-19 17:09:12)
Offline
...
Wants=
...
Note that requirement dependencies do not influence the order in which services are started or stopped. This has to be configured independently with the After= or Before= options. If unit foo.service pulls in unit bar.service as configured with Wants= and no ordering is configured with After= or Before=, then both units will be started simultaneously and without any delay between them if foo.service is activated.
Offline
Um. Well, this is a bit embarrassing.
It's just a typo in my service. Instead of "Wants=" and "Requires=", it was meant to be "After=" and "Requires=" (I even had "After=" in my subject line...)
Good think I posted this in "Newbie Corner" as I'm showing my colours!
Thanks, loqs. Much appreciated.
Offline