45-line Postgres backup.
A backup you have never restored is a hope. Dump, verify the archive, upload, and ping a dead-man's switch so silence wakes you up.
Two details people miss
- Alert on silence, not on error: an email sent inside a catch block never fires when cron dies, the container fails to pull, or an IAM policy breaks. A heartbeat monitor (Healthchecks.io, Better Stack) pages you whenever a ping does not arrive on schedule.
- Encryption note: this script does not run client-side encryption by default. Enable bucket-level encryption (SSE-S3 or SSE-KMS) on your backup bucket, or pipe through
ageif you want separate client-side keys.
Runs pg_dump, checks non-empty gzip integrity, uploads to S3, then pings your heartbeat URL. If the job never starts or dies midway, silence is the alarm.
#!/usr/bin/env bash
set -euo pipefail
: "${DATABASE_URL:?DATABASE_URL is required}"
: "${BACKUP_BUCKET:?BACKUP_BUCKET is required}"
: "${HEALTHCHECK_URL:?HEALTHCHECK_URL is required}"
STAMP="$(date -u +%Y-%m-%dT%H%M%SZ)"
TMP_FILE="/tmp/pg-${STAMP}.sql.gz"
cleanup() { rm -f "$TMP_FILE"; }
trap cleanup EXIT
curl -fsS -m 10 --retry 3 "${HEALTHCHECK_URL}/start" >/dev/null || true
# 1. Dump and compress
pg_dump "$DATABASE_URL" --no-owner --no-privileges --format=plain | gzip -9 > "$TMP_FILE"
# 2. Verify non-empty and valid gzip stream
[ -s "$TMP_FILE" ] || { echo "ERROR: empty backup" >&2; exit 1; }
gzip -t "$TMP_FILE"
BYTES="$(wc -c < "$TMP_FILE" | tr -d ' ')"
[ "$BYTES" -ge 256 ] || { echo "ERROR: backup too small (${BYTES} bytes)" >&2; exit 1; }
# 3. Upload to object storage
DEST="${BACKUP_BUCKET%/}/pg-${STAMP}.sql.gz"
aws s3 cp "$TMP_FILE" "$DEST" --only-show-errors
# 4. Ping dead-man's switch ONLY after every step succeeded
curl -fsS -m 10 --retry 3 "$HEALTHCHECK_URL" >/dev/null
echo "OK: uploaded ${DEST} (${BYTES} bytes)"Free zip bundleZIP, 2 KB
Download the Postgres Backup + Restore Drill Kit
Drop into AWS ECS Scheduled Tasks, GCP Cloud Run Jobs, Fly.io Machines, or a plain VPS cron.
- backup.sh (pg_dump, gzip integrity verify, upload, dead-man’s switch ping)
- Dockerfile (Alpine Postgres 16 + aws-cli)
- RESTORE-DRILL.md (15-minute quarterly restore checklist)
One email each Sunday, unsubscribe in one click. Or skip the box: the file is right here. Need help wiring this into Cloud Run Jobs, ECS, or a VPS? Reply to the email and I will help for free.
Want me to look at yours for free?
I do five free 10-minute Loom teardowns a month. Drop your email and I will reach out within 24 hours to ask what you want checked.
Just your email. Nothing to write upfront. Or book a 20-minute call.
One email each Sunday.
The new video and the real numbers behind it.