I've created a systemd user service to run a duplicity backup job once a day at 17:00 broadly based on an article in Fedora Magazine. I'm running Fedora 28 with the Gnome desktop. My systemd timer config is:
[Unit]
Description=daily-backup timer
[Timer]
OnCalendar=*-*-* 17:00:00
Unit=daily-backup.service
Persistent=true
[Install]
WantedBy=default.target
This works well except in the case where the machine was switched off when the backup was due to run. The next time I login, the persistent
option ensures the backup runs immediately. Unfortunately the backup script fails at this point because the script tries to run before the user session is active. The backup script starts by trying to mount the backup disk if its not already mounted:
#!/usr/bin/env bash
USER=$(whoami)
MOUNTPOINT="/run/media/$USER/$2"
DEVICE=$(blkid -l -o device -t LABEL="$2")
if [ ! -d "$MOUNTPOINT" ]; then
gio mount --device $DEVICE
fi
# Call duplicity...
It appears, however, that until the user session is active, the script cannot mount the disk. I can work around this by polling for an active session, but this is a crude solution to the problem:
#!/usr/bin/env bash
USER=$(whoami)
MOUNTPOINT="/run/media/$USER/$2"
DEVICE=$(blkid -l -o device -t LABEL="$2")
# Poll for an active session
for i in {1..30}
do
SESSION_STATE=$(loginctl show-user "$USER" -p State --value)
[ "$SESSION_STATE" != "active" ] && sleep 1s || break
done
if [ ! -d "$MOUNTPOINT" ]; then
gio mount --device $DEVICE
fi
It seems like there should be a way to configure my systemd user service to require an active user session but I've been unable to work out how to do that. Does anyone know if this is possible?
/etc/fstab
with optionsnoauto,user
? That should allowmount
as any unprivileged user. This is assuming the disk isn't encrypted or something, and that the technically more lax security wouldn't bother you. I haven't tested whether you have to usemount
, or whethergio mount
/ the udisks backend are then smart enough to let you do it from an inactive session. – sourcejedi May 08 '18 at 16:23/etc/fstab
option again but iirc from the last time I tried that, the backup script still failed because the gnome-keyring had not yet initialized/unlocked and that was required so that duplicity could access the GPG keys. – suilven May 09 '18 at 23:27