Know when the nightly pg_dump fails or writes an empty file

Updated 4 min read

On this page
  1. The problem
  2. What goes wrong silently
  3. Setup
  4. Recommended settings
  5. Gotchas we hit while testing
  6. Related
  7. FAQ

Stallbell is told when the backup starts and ends and how big the file is, and emails you if it fails, never ends, doesn't run or leaves an empty file.

The problem

A cron job runs pg_dump every night and nobody looks at it. Then the password changes, the backup disk fills up, or the mount isn't there after a reboot. In our test, a failed pg_dump --file still left a 0-byte .dump file with tonight's date, so ls looks fine. You find out on the day you need to restore.

This page is a ready-made setup for one scenario.

What goes wrong silently

What Stallbell catches
What happensWhat Stallbell seesAlert
pg_dump exits non-zero (wrong password, missing database, disk full)The exit code in the finish pingFailed run
The backup directory is missing or the file is 0 bytesoutput_len=0 in the finish pingEmpty output
pg_dump hangs on a lock or a dead connectionA start ping with no finishStuck run
The cron entry is gone, or the server is offNo ping by 02:30 plus graceMissed run
The next backup worksA good finish after the incidentRecovered
Apply this template

Creates a monitor called postgres-nightly-backup with the settings below. You can change everything later.

Setup

Save this as /usr/local/bin/pg-backup and make it executable (chmod +x). Set URL to your ping URL, DB to your database and OUT to your backup folder.

Shell
#!/bin/sh
# pg-backup: nightly pg_dump that tells Stallbell when it starts, how it ended and how big the file is.
URL="https://stallbell.com/p/TOKEN"
DB="appdb"
OUT="/var/backups/postgres/$DB-$(date +%F).dump"
RUN_ID="$DB-$(date +%Y%m%dT%H%M%S)-$$"

curl -fsS -m 10 --retry 3 -o /dev/null "$URL/start?run_id=$RUN_ID"

pg_dump --format=custom --file="$OUT" "$DB"
code=$?

bytes=0
[ -f "$OUT" ] && bytes=$(wc -c < "$OUT")
curl -fsS -m 10 --retry 3 -o /dev/null -G "$URL/$code" \
  --data-urlencode "run_id=$RUN_ID" \
  --data-urlencode "output_len=$bytes" \
  --data-urlencode "msg=$(basename "$OUT"): $bytes bytes"
exit "$code"

Then add it to the crontab of a user that can read the database (for example crontab -u postgres -e):

crontab
30 2 * * * /usr/local/bin/pg-backup

What it sends:

  • /start with a run_id when the dump begins, so a hang shows up as Stuck run.
  • /<exit code> with the same run_id when it ends: /0 is a good run, anything else is a failure.
  • output_len set to the file size in bytes. With Flag empty output on, a missing or 0-byte file raises Empty output, even if something reported success.
  • msg with the file name and size, for example appdb-2026-10-20.dump: 56088 bytes. It shows under Recent runs; click a run to see it in the Run drawer with the exit code and duration.

The script keeps pg_dump's exit code, so cron's own mail still works. If a ping fails, curl prints an error and the backup carries on.

Recommended settings
FieldValueWhy
KindScheduled jobA label only. The backup runs from cron.
How it runsOn a cron scheduleThe dump runs at a set clock time each night.
Cron expression30 2 * * *02:30 every night. Change it to match your crontab line.
Schedule timezoneYour account timezoneFollows your account timezone. Pick your server's zone if its cron runs on a different one.
Grace period30 minutes30 minutes, because the dump often starts after other night jobs finish.
Failures in a row1Every missed backup matters, so the first failure alerts.
Stuck after2 hours2 hours covers a large database. Set it well above your slowest normal dump.
Flag empty outputOnThe script sends the dump size as output_len, so a 0-byte or missing file raises Empty output.

Gotchas we hit while testing

A failed dump still leaves a file. With --file, pg_dump created a 0-byte nosuchdb-2026-10-20.dump before it failed to connect. That's why the script reports the size and not just whether the file exists.

A gzip pipeline hides the failure. pg_dump nosuchdb | gzip > backup.sql.gz exited 0 under sh and left a 20-byte file, which is not empty, so neither the exit code nor the size check would catch it. Use --format=custom as above, which is already compressed. If you need a pipe, run the script with bash and set -o pipefail: the same pipeline then exited 1.

--list doesn't prove the file is whole. On a dump cut short to 20,000 bytes, pg_restore --list still exited 0. pg_restore --file=/dev/null read the whole archive and exited 1 with "could not read from input file: end of file". To check that, run it after the dump and send its exit code instead.

FAQ

Does Stallbell check that the backup can be restored?

No. It checks that pg_dump ran, how it exited and that the file isn't empty. To check the archive reads end to end, run pg_restore --file=/dev/null on it and report that exit code.

Why not pg_dump | gzip?

In a plain sh pipeline the exit code is gzip's, not pg_dump's. In our test a dump of a missing database exited 0 and left a 20-byte .gz file. --format=custom is already compressed and keeps pg_dump's exit code.

Where does the database password go?

Not in the script. Run the cron job as a user with a ~/.pgpass file (mode 600), or use peer authentication as the postgres user.

Does Stallbell see my data?

No. The script sends the exit code, a run ID, the file size and a short message with the file name. The dump never leaves your server.