Quick tip: Easy weekly backups with systemd

Creating regular backups does not have to bee a cumbersome job but can be easily automated. Instead of using cron I have settled to use systemd for such tasks. In my eyes the benefit is that I can still see any job output after it has been run by using either systemctl status or journalctl without the need to configure any logging whatsoever. And I have easy access to things like execution time or exit code.

We need to place the following two files in /etc/systemd/system on the opsi configserver.

opsi-backup.timer:

[Unit]
Description=Trigger a regular run of opsi-backup.

[Timer]
OnCalendar=weekly

[Install]
WantedBy=timers.target

opsi-backup.service:

[Unit]
Description=Create an opsi backup.
Wants=local-fs.target
After=local-fs.target

[Service]
Type=oneshot
ExecStart=/usr/bin/opsi-backup create --compression gz /var/lib/opsi/backup.tar.gz

The things left to do are to enable the timer so it will start after the next reboot and start the timer right away so we do not have to reboot our server for the timer mechanism to become active:

systemctl enable opsi-backup.timer
systemctl start opsi-backup.timer

To create an backup right now you can run systemctl start opsi-backup.service any time. This will create an backup at /var/lib/opsi/backup.tar.gz. The backup will be overridden every week so if you plan on keeping it either save this file to another location every week or change the service so that it creates backup in a directory instead.