I'd like to have a backup script execute when certain external USB drives get mounted. I'm trying to achieve this systemd.
/etc/systemd/user/autobackup.service, I've added:
[Unit] Description=Autobackup to USB drive Requires=media-david-backup.mount After=media-david-backup.mount [Service] ExecStart=/home/david/Documents/computer/backup/scripts/backup_to_usb.sh [Install] WantedBy=default.target media-david-backup.mount
(Note: I'm note sure whether both
WantedBy values are necessary or not.)
The mount name was determined by mounting the desired USB drive and executing
systemctl list-units -t mount.
After adding the above service file, I can run
systemctl --user start autobackup.service and the backup executes as expected.
I then execute
systemctl --user enable autobackup.service for the service to be running every time my user logs in, which creates the necessary symlinks in
However, when I next log on the backup doesn't trigger when the USB drive is mounted: it will only work once I manually execute
systemctl --user start autobackup.service again.
journalctl -r --user-unit autobackup shows:
Mai 19 21:15:42 david-ThinkPad-T490 systemd: autobackup.service: Succeeded. Mai 19 21:15:42 david-ThinkPad-T490 backup_to_usb.sh: snapshot fefecca4 saved # I've cut a bunch of backup-related log lines Mai 19 21:15:39 david-ThinkPad-T490 backup_to_usb.sh: open repository Mai 19 21:15:39 david-ThinkPad-T490 systemd: Started Autobackup to USB drive.
systemctl --user -t service does NOT show the service even after it just executed successfully after manually starting the service.
As far as I can gather, I'm not expected to put
systemctl --user start autobackup.service in some sort of startup script, since
systemctl --user enable autobackup.service should be taking care of that.
What am I doing wrong?