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.servicefile is correct and theExecStartpath is correct. - Check the system logs for any errors related to the
backup.serviceorbackup.timer. - Use the command
sudo journalctl -u backup.serviceto view the logs for thebackup.service. - Use the command
sudo journalctl -u backup.timerto view the logs for thebackup.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
rsyncoverssh. - Use encryption to protect your backup data, such as using
rsyncwith--encryptoption. - 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
- Troubleshooting Common Connection Issues with resolvectl and ss on Linux
- Taming Package Versions with apt-mark and pinning to Avoid Dependency Conflicts
- Troubleshooting Disk Usage Issues with Duplicate Files and Unnecessary Logs on Btrfs and Ext4 Filesystems
- Taming systemd-resolved: Troubleshooting DNS leaks and resolving domain name surprises on Linux desktops and servers
- Taming systemd-resolved: How to Configure DNS Settings for Split Horizon Environments