1

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
  • What mount point are you specifying in your script? – Raman Sailopal Oct 6 '17 at 14:43
  • My script tries to peform a restic backup to a repository in /media/koichirose/4tb – koichirose Oct 6 '17 at 14:45
  • I can't understand how 4tb1 is even referenced if you haven't specified it in the script. Also udisksctl unmount -b $(df <mountpoint> | tail -1 | awk '{ print $1 }') and udisksctl power-off -b $(df -h <mountpoint> | tail -1 | awk '{ print substr($1,1,length($1)-1)}') will help you with your bonus question. You will need to look into how to set the command up with passwordless sudo prefix – Raman Sailopal Oct 6 '17 at 14:55
  • I believe something is happening before my backup script is called and systemd creates 4tb. My backup script expects the disk to already be mounted and doesn't do anything to mount it. Thank you for the bonus question answer, I'll look into it once I fix this :) – koichirose Oct 6 '17 at 15:08
  • Yes but if you only have 4tb set up as a directory with no 4tb1, 4tb1 mount which hasn't even been specified should fail? – Raman Sailopal Oct 6 '17 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.