Know when the nightly pg_dump fails or writes an empty file
On this page
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 happens | What Stallbell sees | Alert |
|---|---|---|
pg_dump exits non-zero (wrong password, missing database, disk full) | The exit code in the finish ping | Failed run |
| The backup directory is missing or the file is 0 bytes | output_len=0 in the finish ping | Empty output |
pg_dump hangs on a lock or a dead connection | A start ping with no finish | Stuck run |
| The cron entry is gone, or the server is off | No ping by 02:30 plus grace | Missed run |
| The next backup works | A good finish after the incident | Recovered |
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.
#!/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):
30 2 * * * /usr/local/bin/pg-backup
What it sends:
/startwith arun_idwhen the dump begins, so a hang shows up as Stuck run./<exit code>with the samerun_idwhen it ends:/0is a good run, anything else is a failure.output_lenset 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.msgwith the file name and size, for exampleappdb-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
| Field | Value | Why |
|---|---|---|
| Kind | Scheduled job | A label only. The backup runs from cron. |
| How it runs | On a cron schedule | The dump runs at a set clock time each night. |
| Cron expression | 30 2 * * * | 02:30 every night. Change it to match your crontab line. |
| Schedule timezone | Your account timezone | Follows your account timezone. Pick your server's zone if its cron runs on a different one. |
| Grace period | 30 minutes | 30 minutes, because the dump often starts after other night jobs finish. |
| Failures in a row | 1 | Every missed backup matters, so the first failure alerts. |
| Stuck after | 2 hours | 2 hours covers a large database. Set it well above your slowest normal dump. |
| Flag empty output | On | The 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.
Related
- Know when certbot renewals stop succeeding
- Know when Laravel's schedule:run stops running
- Docs: check-in fields
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.