Monitor BorgBackup runs with return codes

On this page

BorgBackup separates warnings from errors: return code 1 means the operation finished but with warnings, 2 means it did not. The difference matters for a backup that finishes with a vanished file. Send the code to CronMissed and decide what you consider a failure.

How can a scheduled job fail silently on BorgBackup?

  • Return code 1 means the operation reached its normal end but there were warnings. Borg logs them at WARNING level, which nobody sees without the log.
  • Return code 2 is a generic error: a fatal error, or an exception, so the operation did not reach its normal end.
  • With BORG_EXIT_CODES=modern, specific errors use codes 3 to 99, specific warnings use 100 to 127, and a code of 128 plus N means the process was killed by signal N (137 is kill -9).
  • Borg writes log messages to standard error, and the docs say to check the log levels and the return code to separate error, warning and success.

What are the limits of BorgBackup?

LimitWhat the documentation says
0Success.
1Generic warning: the operation ended normally, with warnings.
2Generic error.
3 to 99, 100 to 127Specific errors and specific warnings, when BORG_EXIT_CODES=modern is set.
128+NKilled by signal N.

How do you add a CronMissed ping to BorgBackup?

The script below reports the start, runs the backup, and sends the return code. CronMissed counts only 0 as success. Add --show-rc to see the code in the log, and set the repository and passphrase in the environment.

nightly-borg-borg.sh
#!/bin/sh
# borg backup: reports the start, the exit status and the last 100 kB of output to CronMissed.
# borg reads BORG_REPO and BORG_PASSCOMMAND 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=$(borg create --stats "$BORG_REPO::{hostname}-{now}" /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 a warning (code 1) should still count as a good backup, map it after the run.

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

Which check settings fit BorgBackup?

Create a cron check with the backup schedule and a grace based on the longest run. For a prune and compact job on a weekly schedule, create a second check, since a repository that is never pruned fills up quietly. A start ping records the duration of every run.

How do you confirm the check works?

  1. Run the script by hand with a test repository and confirm the start and result pings.
  2. Run it against a repository path that does not exist and confirm the check goes down with a non-zero code.
  3. Delete a source file mid-run (in a test) to see how your Borg version reports it, and decide whether to map the warning.

Sources

Questions and answers

Should warnings count as failures?
It depends on the data. A warning for a file that vanished during the run is often harmless; a warning for an unreadable directory is not. Start strict, and map specific warnings once you know them.
Does the check see the backup size?
The ping body can carry the end of Borg's statistics output. Pipe the output through tail -c 100000 as the script does, and read it in the ping log.
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