Systemd: desktop backups before shutting-down for the night

I want to run daily desktop backups before shutting-down for the night, which means I don’t want them to run before reboot, only before shutdown. But that’s not what’s happening:

[Unit]
Description=Backup with restic at shutdown
Before=poweroff.target
Requires=network.target

[Service]
Type=oneshot
TimeoutStopSec=infinity
ExecStop=/usr/bin/restic backup --verbose --one-file-system --tag systemd.shutdown $BACKUP_EXCLUDES $BACKUP_PATHS
ExecStopPost=/usr/bin/restic forget --verbose --tag systemd.daily --group-by paths,tags --keep-daily $RETENTION_DAYS --keep-weekly $RETENTION_WEEKS --keep-monthly $RETENTION_MONTHS --keep-yearly $RETENTION_YEARS
EnvironmentFile=/etc/systemd/system/restic-backup.conf
RemainAfterExit=true

[Install]
WantedBy=multi-user.target

correctly runs before shutting-down, but it also runs before a reboot. It shouldn’t, as I did not say:
Before=shutdown.target reboot.target

Is this a systemD bug, since shutdown & reboot are mutually exclusive?
…or at least it seems like they should be mutually exclusive.

Nice idea, just googled it :

seems systemd fault …
perhaps add :

Conflicts=reboot.target

Better last post looks like it works :

1 Like

There seems to be an argument about conflicts=XXXXX.target That second article you listed has the following reply:

From reading the answers here there seems to be a lot misunderstanding about how systemd works. Firstly don’t use conflicts to exclude a target. It is ment to keep to conflicting services from being run at the same time.

If a unit has a Conflicts= setting on another unit, starting the former will stop the latter and vice versa.

Unit means a .service file ment to launch perticular service not a target to be reach. In other-words Conflicts=reboot.target is meaningless at best and at worst it will prevent you from rebooting. Don’t do this. It does not mean don’t run this on reboot. It means abort either this service or the reboot.target depending on timing and how systemd interprets this erroneous use of conflicts.

So did you have the chance and actually try this on your system? How did it work out / what behaviour did you observe?
I have a similar idea on my to-do-list and your discussion was ringing a bell for me…

I think you’ve mixed up some of the stuff here, basically your unit file is “starting” every time you boot, then stopping via your command no matter how its powered off. The Before=poweroff.target is only saying “this service should be started before a normal poweroff begins”.

Also reboot.target and poweroff.target are mutually exclusive, but they both depend on shutdown.target.

I think what might work (completely untested) would be to set the unit’s WantedBy=poweroff.target, as that would make the unit activate and immediately deactivate on normal poweroff (not reboot) only.

Some more stuff to note:

  • If you have networking issues, you may need to try something like After=network-online.target, maybe in combination with a similar Requires, instead.
  • It’s systemd, not systemD :wink:
1 Like