Sub topic 14
Knowing when a backup fails
This chapter assumes that you can use the Linux command line, read basic shell scripts, and manage systemd services and timers. It also assumes that you already have a working restic or Borg backup job.
A scheduled backup is useful only if you know whether it completed. A timer can run on schedule while the backup command fails. Monitoring must check the service result, preserve the error details, and alert someone who can respond.
Treat backup success as a testable result
A backup service should return exit status 0 only after the backup and its required follow-up steps succeed. Any other status means failure. With a shell script, set -euo pipefail makes many errors stop the script:
#!/bin/bash
set -euo pipefail
restic backup --exclude-file=/etc/restic/excludes.txt /etc /home /var/www
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
restic checkFor Borg, use the equivalent borg create, borg prune, borg compact, and borg check commands. Do not hide errors with constructs such as command || true. That makes systemd report success even when the backup failed.
A successful command does not prove that recovery will work. Monitoring should therefore cover both job execution and repository health.
Inspect systemd results
The service records its latest result, while the timer records when it last ran and when it will run next:
systemctl status restic-backup.service
systemctl show restic-backup.service \
--property=Result,ExecMainStatus,ActiveEnterTimestamp
systemctl list-timers restic-backup.timer
journalctl -u restic-backup.service --since "24 hours ago"Useful results include:
| Signal | Meaning | Response |
|---|---|---|
| `Result=success` and status `0` | The service completed | Confirm the expected snapshot exists |
| `Result=exit-code` | A command returned an error | Read the journal and fix the cause |
| Timer has no recent activation | The timer did not run | Check whether it is enabled and active |
| Backup succeeds but `check` fails | Repository integrity needs attention | Stop relying on that repository until investigated |
Check the timer separately from the service. A timer may be disabled, blocked by a dependency, or scheduled for a time that has not arrived. Persistent=true causes a missed run to start after the system returns, but it does not fix a broken script or unavailable repository.
Create actionable alerts
A log entry is not an alert unless someone sees it. Small systems can use a wrapper that sends mail or a message when the service fails. Larger systems can collect systemd state and journal entries with a monitoring platform.
Keep alerts specific. Include the host, job name, failure time, and a command to inspect the logs. Avoid alerting on every normal snapshot message. Alert when:
- The service exits unsuccessfully.
- The timer has not run within the expected interval.
- The repository check fails.
- The backup completes but the snapshot is unexpectedly small or missing.
- Remote storage, credentials, disk space, or network access fails.
Trace a failed run
When investigating, record the exact time, host, repository, command, and error. Common causes include expired SSH keys, a full destination, incorrect permissions, an unavailable network, and an expired or unreadable passphrase file. After fixing the cause, run the service manually and verify the new snapshot.
Exercise
The timer is active, but the latest service run shows Result=exit-code. What should you inspect first?
Show answer
Read journalctl -u restic-backup.service for the failed command and error, then rerun the service after correcting the cause.