Monitor pg_dump backups with CronMissed

On this page

pg_dump returns a non-zero exit status when a dump fails, but a pipe to gzip or an upload command can hide it. The wrapper below checks the status of the whole pipeline and pings CronMissed with the real result.

How can a scheduled job fail silently on pg_dump database backups?

  • In a shell pipeline such as pg_dump | gzip > file, the exit status is that of the last command unless pipefail is set. A failed dump can then look like a success.
  • pg_dump makes a consistent export while the database is in use and does not block readers or writers, so a dump can run for a long time without anyone noticing it is late.
  • The PostgreSQL manual says that, except in simple cases, pg_dump is generally not the right choice for regular backups of production databases, and points to the backup chapter for the alternatives.
  • A dump only holds one database. Roles and tablespaces need pg_dumpall.
  • A custom-format dump (--format=custom) can be inspected and restored selectively with pg_restore, but nobody learns it is broken until a restore is tried.

What are the limits of pg_dump database backups?

LimitWhat the documentation says
ScopeOne database per pg_dump; pg_dumpall covers a cluster and its global objects.
ConsistencyConsistent export even with concurrent use.
LockingDoes not block other users; parallel dumps take shared locks on the objects they will dump.
FormatsPlain SQL script or archive formats; custom and directory formats work with pg_restore.
Parallel dumpThe --jobs option needs synchronized snapshots on the server.

How do you add a CronMissed ping to pg_dump database backups?

This wrapper reports the start, runs the dump, checks that the file is not empty, and sends the exit status. The --format=custom file is easy to restore selectively.

nightly-pgdump-pg_dump.sh
#!/bin/sh
# pg_dump backup: reports the start, the exit status and the last 100 kB of output to CronMissed.
PING_URL="https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10"
curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null "$PING_URL/start"
output=$(pg_dump --format=custom --file="/var/backups/db-$(date +%Y-%m-%d).dump" "$DATABASE_URL" 2>&1)
status=$?
printf '%s\n' "$output" | tail -c 100000 | curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null --data-binary @- "$PING_URL/$status"
exit $status

If you pipe the dump through other tools, enable pipefail so the status covers the whole pipeline.

bash
set -o pipefail
pg_dump --format=custom "$DATABASE_URL" | gzip > "/var/backups/db-$(date +%F).dump.gz"
status=$?

Which check settings fit pg_dump database backups?

Create a cron check with the dump schedule and a grace set from the longest dump you have seen; a database that grows will need the number raised. Add a second check for the restore test you run each week or month. A dump that exists but cannot be restored is the failure the first check cannot see.

How do you confirm the check works?

  1. Run the script by hand and confirm the start and exit pings and a non-empty file.
  2. Use a wrong password in a test and confirm the status is non-zero and the check goes down.
  3. Restore the dump into a scratch database with pg_restore at least once, and keep that as a scheduled job with its own check.

Sources

Questions and answers

Is pg_dump enough as a backup strategy?
The PostgreSQL documentation says it often is not for production, and refers to continuous archiving. CronMissed watches whichever tool you choose, whether it is pg_dump, a base backup or a managed snapshot job.
Why check that the file is not empty?
Some failures leave a zero-byte file and still return from the next command normally. A size check in the script is a cheap second line of defense.
Know when this job stops

Create a check, add the ping to the job, and CronMissed alerts you the first time a run is late or reports a failure.

Start monitoring