I'm trying to run a backup script when a USB HDD is plugged in, I'm using Ubuntu 16.04.
The backup script is also supposed to unmount (and possibly poweroff) the disk when done.
I have no entries in /etc/fstab for that disk. I simply assigned a label (4tb) to it with e2label and it will automatically mount to /media/koichirose/4tb, owned by koichirose. Everything good so far.
Here's the systemd service:
[Unit]
Description=Launches backup script when the 4TB WD drive is plugged in
Requires=media-koichirose-4tb.mount
After=media-koichirose-4tb.mount
[Service]
ExecStart=/home/koichirose/scripts/backup_to_4tb
[Install]
WantedBy=media-koichirose-4tb.mount
If I enable this and plug the HDD in, /media/koichirose/4tb is created, owned by root, and soon after /media/koichirose/4tb1 is created, owned by koichirose. The drive is actually mounted on 4tb1 and 4tb is empty. The backup script then runs and fails because it looks for '4tb' and not '4tb1'. If I unmount 4tb1, 4tb is still there, owned by root.
If I disable the service, everything is fine again, the drive is mounted to 4tb and the dir is correctly owned by the user.
How can I fix this?
Bonus question: at the end of my script I'm currently executing umount.
This is good but I found out about:
udisksctl unmount -b /dev/sdc1
udisksctl power-off -b /dev/sdc
Which would unmount and poweroff the drive. Two issues with it: it asks for my sudo password (I'd like it to run unattended) and it uses the device name (which could change) instead of the mount point (which I'm trying to make 'static').
Edit: here's the backup script (/home/koichirose/scripts/backup_to_4tb):
#!/bin/bash
export RESTIC_PASSWORD="mypassword"
repo_path=/media/koichirose/4tb/restic_incoming_backups
dir_to_backup=/home/koichirose
/usr/local/bin/restic -r $repo_path backup $dir_to_backup
#replace with udisksctl later...
umount /media/koichirose/4tb