Monitor restic backups with exit codes

On this page

A restic backup that finishes with a warning can leave a snapshot that is missing files. restic returns a distinct exit code for that, and for a locked or missing repository, so the wrapper script should hand the code to CronMissed and let the check decide.

How can a scheduled job fail silently on restic backups?

  • Exit code 3 means backup could not read some source data, or forget could not remove one or more snapshots. The snapshot may be missing files.
  • Exit code 11 means restic failed to lock the repository, which happens when another run holds it. A stuck earlier run turns every later run into a failure.
  • Exit code 10 means the repository does not exist, and 12 means a wrong password; both are new in recent releases, and older versions return 1.
  • restic documents that an unknown exit code must be treated as a command failure, since new codes are added over time.
  • A cron line that pipes or redirects restic can hide its exit code unless the shell reports it.

What are the limits of restic backups?

LimitWhat the documentation says
0The command was successful.
1The command failed.
3backup could not read some source data, or forget could not remove snapshots.
10, 11, 12Repository missing (since 0.17.0), repository could not be locked (since 0.17.0), wrong password (since 0.17.1).
Unknown codesMust be treated as failures.

How do you add a CronMissed ping to restic backups?

The script reports the start, runs the backup, and sends the exit code as the ping path, so any code other than 0 turns the check down at once. Credentials come from the environment, not from the script.

nightly-restic-restic.sh
#!/bin/sh
# restic backup: reports the start, the exit status and the last 100 kB of output to CronMissed.
# restic reads RESTIC_REPOSITORY and RESTIC_PASSWORD_FILE from the environment.
PING_URL="https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10"
curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null "$PING_URL/start"
output=$(restic backup /path/to/data 2>&1)
status=$?
printf '%s\n' "$output" | tail -c 100000 | curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null --data-binary @- "$PING_URL/$status"
exit $status

If you decide that a snapshot with unreadable files is acceptable, map code 3 to success before the last ping.

sh
if [ "$status" -eq 3 ]; then status=0; fi

Which check settings fit restic backups?

Create a cron check with the backup's schedule and a grace that covers the longest run you have seen plus 25 percent. Backups grow, so review the grace every few months or use the duration the start ping records. Run restic check on a separate weekly job with its own check.

How do you confirm the check works?

  1. Run the script by hand against a test repository and confirm the start and exit pings.
  2. Point RESTIC_REPOSITORY at a path that does not exist and confirm the check goes down with status 10 or 1.
  3. Make a source file unreadable and confirm exit code 3 turns the check down (or is mapped, if you chose to).

Sources

Questions and answers

Should exit code 3 raise an alert?
By default yes, because the snapshot may be missing files you meant to keep. If a known file is always unreadable, exclude it from the backup or map 3 to success deliberately.
How do I watch the restore side?
Create a separate check for a scheduled restic check or a test restore. A backup you never test is the one that fails when you need it.
Know when this job stops

Create a check, add the ping to the job, and CronMissed alerts you the first time a run is late or reports a failure.

Start monitoring