← Docs

How to monitor a backup script (bash, rsync, pg_dump)

Backups are the classic silent failure. Wrap the job so you hear about both a crash and a job that never started.

#!/usr/bin/env bash
set -u
PING=https://heartbeat-monitor.morristech.ca/ping/YOUR-CHECK-ID
curl -fsS -m 10 --retry 3 -o /dev/null "$PING/start"

if pg_dump mydb | gzip > /backups/mydb-$(date +%F).sql.gz; then
  curl -fsS -m 10 --retry 3 -o /dev/null "$PING"
else
  curl -fsS -m 10 --retry 3 -o /dev/null "$PING/fail"
fi

The /start ping lets Heartbeat measure run time and alert if the job starts but never finishes. You can attach output to a ping by POSTing it: curl -fsS --data-raw "$(tail -c 2000 backup.log)" "$PING/fail". We keep the first 10 KB.