How to monitor that your backup actually ran

Everyone who has lost data has a backup script. The script is rarely the problem — the problem is that nobody was told when it stopped.

Three distinct failures

"Is the backup working?" is really three questions, and they need different checks:

  1. Did it run? The schedule fired and the process started.
  2. Did it finish? It exited 0 rather than dying halfway through a 40 GB dump.
  3. Is the output sane? The file exists and is roughly the size you'd expect.

Pinging at the top of the script only answers the first. Ping at the end, gated on success:

#!/usr/bin/env bash
set -euo pipefail
PING="https://api.usedeadline.com/p/<token>"

pg_dump -Fc mydb > /backups/mydb-$(date +%F).dump
SIZE=$(stat -c%s /backups/mydb-$(date +%F).dump)

# A dump far smaller than usual means the dump "succeeded" but is empty.
if [ "$SIZE" -lt 10000000 ]; then
  curl -fsS -d "dump only $SIZE bytes" "$PING/fail"
  exit 1
fi

curl -fsS -m 10 --retry 3 "$PING"

That size check is the one people skip, and it's the one that catches a dump where the database connection dropped after the schema and before the data.

Set the window to catch the first miss

For a nightly backup, an interval of 24 hours with a two-hour grace period means you hear about a missed night the following morning — not a week later. The cost of a false alarm is a glance at your phone; the cost of a missed alert is the backup you don't have.

Verify restores too

Monitoring proves the file was written, not that it can be restored. Run a weekly restore into a scratch database, count rows in a table you care about, and ping a second monitor on success. If the restore job stops running, you'll learn about that too.

Offsite copies

If a separate job syncs backups to object storage, give it its own monitor. "The dump was made" and "the dump left the building" fail independently, and the second one is what matters when the building is the thing that failed.