Backup at shutdown

Banging my head against the wall. Why doesn’t this Systemd service run?
I’m trying to run a user service , to backup my documents, before the desktop system shuts down every night.

`# Needs to be placed in ~/.config/systemd/user/
[Unit]
Description=Backup with restic before shutdown
Before=poweroff.target halt.target
After=networking.service

[Service]
Type=oneshot
ExecStart=/usr/bin/restic backup --verbose --one-file-system --tag systemd.shutdown $BACKUP_EXCLUDES $BACKUP_PATHS
ExecStartPost=/usr/bin/restic forget --verbose --tag systemd.shutdown --group-by paths,tags --keep-daily $RETENTION_DAYS --keep-weekly $RETENTION_WEEKS --keep-monthly $RETENTION_MONTHS --keep-yearly $RETENTION_YEARS
EnvironmentFile=%h/.config/restic-backup.conf
RemainAfterExit=yes

[Install]
WantedBy=poweroff.target halt.target`

It runs if I start it manually: systemctl --user start restic-backup.service
It seems to enable fine: systemctl --user enable restic-backup.service
Returns:
Created symlink /home/lance/.config/systemd/user/poweroff.target.wants/restic-backup.service /home/lance/.config/systemd/user/restic-backup.service. Created symlink /home/lance/.config/systemd/user/halt.target.wants/restic-backup.service /home/lance/.config/systemd/user/restic-backup.service.

…but when I go to the desktop menu & shutdown, the script does not run, the computer just shuts down.

Anyone see what I’m missing?

Yep. You can’t have dependencies on system level services in a user service file. Create/enable it as a system service but with the User= directive (if required).

1 Like

I switched to a system service, but that’s got problems:
Before SystemD, I used to put the daily backup into Init0, causing the backup to run every night as part of the desktop shutdown procedure. But on SystemD it’s apparently much more difficult to get a service to complete before shutdown. I’ve looked & looked, there are lots of posts discussing how to write a service that will interrupt shutdown until the process is complete, but I’ve seen no method that works for everyone.

I’m running a local backup using Rdiff-Backup, it runs at shutdown and completes properly. But when I tell Restic to do the same thing, Fedora shuts-down, leaving an abandoned file lock on B2.

OK here’s what’s actually preventing users from easily running a backup at shutdown:
In the old Init days it was easy, you just linked your backup script in Init0, and everything worked.
Now it’s much harder! SELinux prevents some things from happening, but as any Fedora user knows, SELinux is triggered by a lot of routine things…like drives mounting. This may very well prevent your internal or external backup drive from mounting. Once I prevented SELinux from actively blocking access, I found that SystemD has pre-programed timeout limits that were being hit, because they have very short time limits, 90 seconds on my system. I set those individually to not time-out at all: TimeoutStopSec=infinity.
Only then would my backups run uninterrupted!

1 Like