Taming systemd Timer Services to Run Your Daily Backup at a Reasonable Hour

Introduction to systemd Timer Services

I’ve been using systemd timer services for years to schedule tasks on my Linux systems, and I have to say, they’re a game-changer. Most Linux distributions, including Debian, Arch Linux, and OpenSUSE, use systemd as their default init system, so it’s worth learning how to use them. In this article, I’ll show you how to use systemd timer services to run daily backups at a reasonable hour.

Creating a Backup Script

Before we can schedule our backup, we need a script that performs the actual backup. For this example, I’ll use a simple rsync command to backup my home directory to an external hard drive.

#!/bin/bash

# Set the source and destination directories
SOURCE=/home/user
DESTINATION=/media/external/hbackup

# Perform the backup using rsync
rsync -avz --progress $SOURCE $DESTINATION

Save this script to a file, for example, backup.sh, and make it executable with the command chmod +x backup.sh. Don’t bother with complicated backup scripts for now - this will do the job.

Creating a systemd Service File

To schedule our backup script using systemd, we need to create a service file that defines the backup script as a service. Create a new file called backup.service in the /etc/systemd/system directory with the following contents:

[Unit]
Description=Daily Backup Service
After=network.target

[Service]
ExecStart=/path/to/backup.sh

Replace /path/to/backup.sh with the actual path to your backup.sh script. This is where people usually get burned - make sure the path is correct, or your service won’t start.

Creating a systemd Timer File

Now that we have our service file, we need to create a timer file that schedules the service to run at a specific time. Create a new file called backup.timer in the /etc/systemd/system directory with the following contents:

[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=daily
Persistent=true
Unit=backup.service

[Install]
WantedBy=timers.target

This timer file schedules the backup.service to run daily at the same time every day. The real trick is to make sure the OnCalendar directive is set correctly - in this case, we’re using daily, but you can customize it to suit your needs.

Enabling and Starting the Timer

To enable and start the timer, use the following commands:

sudo systemctl enable backup.timer
sudo systemctl start backup.timer

You can verify that the timer is enabled and running with the command sudo systemctl status backup.timer. I usually start with a simple systemctl status command to make sure everything is working as expected.

Troubleshooting

If your backup script is not running as scheduled, there are a few things you can check:

  • Make sure the backup.service file is correct and the ExecStart path is correct.
  • Check the system logs for any errors related to the backup.service or backup.timer.
  • Use the command sudo journalctl -u backup.service to view the logs for the backup.service.
  • Use the command sudo journalctl -u backup.timer to view the logs for the backup.timer.

Security Considerations

When creating a backup script, it’s essential to consider the security implications. Make sure to:

  • Use secure protocols for transferring data, such as rsync over ssh.
  • Use encryption to protect your backup data, such as using rsync with --encrypt option.
  • Limit access to the backup data and scripts to authorized users only.
  • Regularly review and update your backup scripts and timers to ensure they are still secure and functional.

For more information on systemd timer services, you can refer to the systemd.io documentation. Additionally, you can check the freedesktop.org website for more information on systemd and other Linux technologies.


See also