3

I'm trying to automate a mount/rsync/umount on a USB key (NTFS formatted but it doesn't matter, I think) with systemd. My goal is to copy the content of a given folder on my key when plugged, then unmount it so I can unplug it properly.

I can nearly get what I want but the unmount part seems more tricky than I first expected.

Here is my current configuration:

I created two files in /etc/systemd/system/ : syncUSB_NTFS.service and media-USB_NTFS.mount.

cat syncUSB_NTFS.service:

[Unit]
Description=Synchronisation USB_NTFS
After=media-USB_NTFS.mount
BindsTo=media-USB_NTFS.mount

[Service]
ExecStart=/usr/bin/rsync -r /mnt/Espace/USB/ /media/USB_NTFS/

[Install]
WantedBy=media-USB_NTFS.mount

cat media-USB_NTFS.mount :

[Unit]
DefaultDependencies=no
Conflicts=umount.target
Before=umount.target
;StopWhenUnneeded=true

[Mount]
What=/dev/disk/by-uuid/AC385D2D385CF7B2
Where=/media/USB_NTFS

The problem is : the USB key mounts, copy but then doesn't umount. It tried to add a "; umount /media/USB_NTFS" after my rsync instruction but it doesn't work. When I uncomment the "StopWhenUnneeded=true", it doesn't mount the key back when I replug it.

I know the problem is obviously my poor understanding of systemd... could someone enlighten me?

PS: my first try was to use lsyncd --nodaemon with a configuration file (+"StopWhenUnneeded=true"), wich worked too but I had the same problem with the umount. Any solution here?

[Unit]
Description=Synchronisation USB_NTFS
After=media-USB_NTFS.mount
BindsTo=media-USB_NTFS.mount

[Service]
ExecStart=/usr/bin/lsyncd --nodaemon /etc/lsyncd/lsyncd-usb.conf.lua
User=xxxx
Group=users

[Install]
WantedBy=media-USB_NTFS.mount

Thanks in advance for your help!

Vigon
  • 43
  • 6

0 Answers0