How to setup systemd.service files for auto backup to USB Drive

The approach with udev rules is viable, but it’s rather complicated and not the way to approach this. Instead, a systemd unit should be used.

I have the following example, which is a tool that runs a script when a drive is plugged and mounted by udisks. (The script reads keys from the usb drive, instead of putting things on the drive, adapting the script is left as an exercise for the reader ;)):

https://github.com/ASPP/installation/tree/main/files/keyadder:

# keyadder.conf
# Install as ~/.config/systemd/user/run-media-.mount.d/keyadder.conf

[Unit]
Wants=keyadder@%N.service
# keyadder@.service
# Install as ~/.config/systemd/user/keyadder@.service

[Unit]
BindsTo=%i.mount

[Service]
ExecStart=sh -x -c 'for i in %f/ssh/id_*.pub; do ssh-add - <$${i%%.pub}; done'
ExecStop=ssh-add -D

Type=oneshot
RemainAfterExit=true
2 Likes

Thanks @grumpey , @zbyszek for the recent post. I’ll have a look at it this afternoon and see what happens.

To all giving this a try or looking through the process, i’ll post more logs, and the fresh scripts to replicate these steps. Honestly don’t see why this “autorun” style of mechanism seems so difficult to pull off. . . Maybe the fact the drive in question is Encrypted and and I unlock either manually though password or through a key-file, Althogh I have not see any errors pertaining to that in the logs.

I initially suspected that the .mount file would be the best approach, due in part because the Drive has a UUID for luks-XXX-xx and a UUID for the mounted Filesystem, but that issue has not come u yet.


Interesting, although that would change how I use that drive. Seeing as it’s a 8TB and I have other things in it. . . It’s worth the try. :handshake:t5: :fedora:


:thinking: I’ll look into this more :exclamation:

Added backup, rsync, udisks2

Added udev-rules