janie.page

Living Your Best Life With SystemD Timers

Setup

Here is a simple SystemD timer which implements a backup for your trusty Fedora laptop.

There are two files involved as shown below. It’s necessary that fedorabackup.service and fedorabackup.timer have the same name before their .{service,timer} suffixes.

File #1: /etc/systemd/system/fedorabackup.timer.

[Unit]
Description=fedora backup job

[Timer]
OnCalendar=*-*-* *:00:00

[Install]
WantedBy=timers.target

File #2: /etc/systemd/system/fedorabackup.service.

[Unit]
Description=fedora backup service

[Service]
Type=oneshot
ExecStart=/usr/bin/bash /home/janie/.local/bin/backup
User=janie
Group=systemd-journal

The contents of /home/janie/.local/bin/backup are as follows- notice the -e argument I had to pass to rsync to get it to work.

#!/usr/bin/bash
/usr/bin/rsync -av -e '/usr/bin/ssh -i /home/janie/.ssh/id_ed25519' \
        --exclude Downloads \
        /home/janie/ janie@0.0.0.0:/mnt/externalssd/fedora-laptop/

Finally, enable the service with sudo systemctl enable fedorabackup.timer and sudo systemctl start fedorabackup.timer.

End result

We end up with a job which executes our fedorabackup.sh script every hour on the hour.

This is quicker to do with cron, but our friend SystemD timers give us the following improvements over cron:

SELinux considerations

Compare the labels of your files /etc/systemd/system/fedorabackup.{service,timer} to other files in their directory per usual.

I had to use setsebool to enable the SELinux booleans rsync_client and rsync_export_all_ro to get rsync to work in this setup.