Sub topic 12
Scheduling backups with systemd
This chapter assumes you can use the Linux command line, understand basic systemd units, and have an executable backup script. The script should already know how to reach the restic or Borg repository and should return a nonzero exit status when the backup fails.
Service and timer units
A systemd service describes the backup job. A timer describes when systemd should start that job.
Use a oneshot service because the backup runs once and then exits. The timer starts it again on the next scheduled run.
Create the service at /etc/systemd/system/restic-backup.service:
[Unit]
Description=Back up this server
Wants=network-online.target
After=network-online.target
[Service]
Type=oneshot
ExecStart=/usr/local/bin/restic-backup.shAfter=network-online.target orders the service after the network is considered ready. It does not create a network connection or guarantee that a remote host is reachable. The script still needs to handle connection failures.
Create the timer at /etc/systemd/system/restic-backup.timer:
[Unit]
Description=Run the server backup daily
[Timer]
OnCalendar=daily
RandomizedDelaySec=30min
Persistent=true
[Install]
WantedBy=timers.targetOnCalendar=daily schedules one run each day. RandomizedDelaySec spreads the actual start time across a 30-minute window. This reduces simultaneous load when many servers use the same backup destination.
Persistent=true records the last scheduled run. If the server was powered off when a run was due, systemd starts the service after the server returns. It does not make up every missed run.
| Setting | Purpose |
|---|---|
| `OnCalendar=daily` | Defines the recurring schedule |
| `RandomizedDelaySec=30min` | Adds a random start delay |
| `Persistent=true` | Runs after a missed schedule |
Enable and test the schedule
After creating or changing unit files, reload systemd and enable the timer:
sudo systemctl daemon-reload
sudo systemctl enable --now restic-backup.timer
systemctl list-timers restic-backup.timerThe timer should show its next scheduled run. Test the service immediately instead of waiting for the timer:
sudo systemctl start restic-backup.service
sudo systemctl status restic-backup.service --no-pagerRead the service output from the journal:
sudo journalctl -u restic-backup.service --since "today" --no-pagerA successful service normally exits with status 0. A failed command should cause the script to exit with a nonzero status. For example, a shell script using set -euo pipefail stops when a backup command fails. Without correct exit handling, the timer may appear successful even though the backup was incomplete.
Preventing overlapping runs
A timer can start a new service run while an earlier run is still active. A systemd service normally permits only one active instance of the same unit, so a second start request is not a second parallel process. Keep the backup interval longer than the expected backup duration, and inspect the journal if a run appears stuck.
The timer itself is not a monitoring system. Check systemctl status, journal entries, or an external monitoring system for failures. A scheduled timer only proves that systemd attempted to start the service.
Exercise
Which command runs the backup service immediately, and which command shows its output?
Show answer
Run sudo systemctl start restic-backup.service, then use sudo journalctl -u restic-backup.service --since "today" --no-pager.