Backup job monitoring: find out the night it fails

Backups fail quietly. The script errors out, the disk fills, the box gets rebuilt without its crontab, and nobody looks until there's something to restore. A Tissue Mast heartbeat expects a beat after every run and pages you when one is missing or says it failed.

Wrap the backup in a short script that tells Mast when it starts and how it ended, with the log as the body. A failed run pages at once, a hung run pages when it passes its longest run, and a run that never happened pages when the grace runs out.

The wrapper script

It runs whatever backup you already have.

#!/bin/sh
# /usr/local/bin/nightly-backup
# Runs backup.sh and reports the run to a Tissue Mast heartbeat.
. /etc/nightly-backup.env    # sets HEARTBEAT_URL
LOG=/var/log/nightly-backup.log

curl -fsS -m 10 --retry 3 -X POST "$HEARTBEAT_URL/start" >/dev/null

/usr/local/bin/backup.sh >"$LOG" 2>&1
status=$?

tail -c 16000 "$LOG" |
  curl -fsS -m 10 --retry 3 --data-binary @- "$HEARTBEAT_URL/$status" >/dev/null

exit $status
# /etc/nightly-backup.env  (chmod 600)
HEARTBEAT_URL="https://mast.tissue.dev/mk_8e2a…"

And the crontab line, which no longer needs anything on the end:

17 3 * * *  /usr/local/bin/nightly-backup
/start
Opens a run. It isn't a beat and pages nobody, but it lets Mast notice a run that never finishes.
/$status
The exit code of backup.sh. /0 is a beat. Anything from 1 to 255 marks the heartbeat down and pages, and the card reads nightly-backup exited 2.
tail -c 16000
Mast takes up to 16 KiB of body and keeps the first 4096 characters in the history, so the tail of the log is the part worth sending. It's what you'll want to read when the page comes in.
-m 10 --retry 3
Gives up on a request that hangs and tries again after a timeout or a server error. A beat that arrives twice is still one beat.

The script only knows what backup.sh tells it. A backup that writes an empty file and exits 0 is a good run as far as anyone can see, so have backup.sh check its own output and exit non-zero when the file is missing or empty.

The heartbeat to go with it

Make the heartbeat in the web app under Heartbeats, New heartbeat, and give it the same schedule as the crontab line. Or over the API:

curl -X PATCH https://api.tissue.systems/v1/mast/alert-sources/nightly-backup \
  -H "Authorization: Bearer $TISSUE_TOKEN" -H "Content-Type: application/json" \
  -d '{"schedule": "17 3 * * *", "schedule_tz": "America/Los_Angeles",
       "grace_secs": 1800, "max_duration_secs": 5400}'
SettingWhat it catches
Schedule and time zoneA run that never happened. Use the zone's name rather than an offset, so the schedule follows the clocks when they change.
Grace, here 30 minutesHow long after 03:17 Mast waits for the end of the run before paging. Give it the time the backup really takes, plus room. A schedule needs grace above zero.
Longest run, here 90 minutesA run that started and is still open after that long goes down with is still running. This is the backup stuck on a lock or a dead network mount.

If a backup sometimes fails once and succeeds on the next run, Failures before a page lets it fail a set number of runs in a row before anyone is told. A backup that goes quiet altogether still pages when the grace runs out.

Backups that can overlap

If a slow backup can still be running when the next one starts, give each run an id so Mast pairs each end with its own start:

rid=$(date +%s)-$$
curl -fsS -m 10 --retry 3 -X POST "$HEARTBEAT_URL/start?rid=$rid" >/dev/null
/usr/local/bin/backup.sh >"$LOG" 2>&1
status=$?
tail -c 16000 "$LOG" |
  curl -fsS -m 10 --retry 3 --data-binary @- "$HEARTBEAT_URL/$status?rid=$rid" >/dev/null

A run id is up to 64 letters, digits, dashes, underscores and dots.

Reading the history after a page

Each heartbeat keeps its last 200 beats for up to 30 days: when each run started and ended, how long it took, the exit code and the first 4096 characters of the log. The card on your phone says which kind of trouble it was, a failed run, one still running or one that never reported, because each of those has a different first move. The next good run closes the page on its own.

The same backup on another scheduler

The wrapper works anywhere a shell does. For a backup that runs from a systemd timer, a Kubernetes CronJob, GitHub Actions or Windows Task Scheduler, those pages show where the beat goes.

Free is ten heartbeats with 7 days of history. Team is $29 a month for 250 heartbeats and 90 days of history, with rotations and escalation for everyone on call.

Create a free account Ten heartbeats, ten probes, ten alert sources. An email address, no credit card.

Read more