The old thread “Failed database dump results in valid snapshot” shows the trap well: pg_dump | restic backup --stdin printed a pg_dump error, and restic still said snapshot e4ad51ae saved. restic only sees the stream end. It can’t tell whether pg_dump finished or died.
In BackupDrill’s pg_dump to S3 test without restic, aws s3 cp - reached the end of its input when pg_dump failed, and uploaded the truncated dump under the usual timestamped name.
1. Let restic run pg_dump
# RESTIC_REPOSITORY and RESTIC_PASSWORD_FILE come from the environment
restic backup --tag postgres \
--stdin-filename app.dump \
--stdin-from-command -- \
pg_dump --format=custom --dbname="postgresql://backup_user@db.internal:5432/app"
With --stdin-from-command, restic starts the command itself and checks its exit code. According to the restic docs, a non-zero exit cancels the backup, restic exits with 1, and no snapshot is created. Leave the password out of the connection string, since it can show up in the process list while pg_dump runs. Put it in the backup user’s ~/.pgpass with mode 0600. If you keep the file elsewhere, use the same permissions and set PGPASSFILE to its path.
Custom format is compressed, and pg_restore can pull a single table out of it later.
2. Read the dump back from the repository
An exit code of 0 tells you pg_dump thought it was done. To check the archive restic stored, read it back in full. Set pipefail first, so a failing restic dump fails the check too:
set -o pipefail
restic dump --path /app.dump latest app.dump | pg_restore --file=/dev/null
--path /app.dump matters if the repo also holds file backups: a plain latest can resolve to a snapshot that has no app.dump at all.
Use --file=/dev/null, not --list. --list only reads the table of contents at the start of the archive. I cut a custom dump to two thirds of its size and piped it through both: pg_restore --list exited 0, while pg_restore --file=/dev/null stopped with could not read from input file: end of file and exited 1. The full dump passed both.
With pipefail set, a zero exit means restic streamed the dump and pg_restore read the whole archive, table data included, without an error. That’s not a restore test: it never loads anything into a server with your roles and extensions. For that, restore the dump into a throwaway Postgres container of the same major version now and then, and compare row counts.
Open question
If you’ve tried both --format=custom and --format=plain for daily dumps of the same PostgreSQL database, how much did the restic repo grow per run with each?