Schedule icon
Query icon
Commands icon
Docker icon
Upload icon
If icon
Log icon
SlackIncomingWebhook icon
Fail icon

Automated PostgreSQL Backup to AWS S3 with Restore Verification

Schedule pg_dump backups to AWS S3, test-restore into an ephemeral database to verify schema parity, and send Slack alerts on success or failure.

Categories
CloudDataInfrastructure

Automating backups is only half the battle: an unverified backup is an outage waiting to happen. Silent schema corruptions, broken permissions, or incomplete pg_dump executions frequently go unnoticed until disaster strikes and recovery fails.

This blueprint implements a disaster recovery workflow for PostgreSQL: it schedules automated custom-format pg_dump backups, streams the archive to Amazon S3, and immediately performs an automated restore drill into an ephemeral scratch database. Only backups that pass full schema and table parity checks are certified and announced to the team.

How it works

  1. source_stats queries information_schema.tables on the production database using the PostgreSQL JDBC plugin to record baseline table counts.
  2. dump_database launches a postgres:16-alpine container via Docker task runner and runs pg_dump -F c -b -v to generate a compressed custom-format archive, recording the byte size.
  3. upload_to_s3 uses the AWS S3 plugin (io.kestra.plugin.aws.s3.Upload) to ship the timestamped archive to a secured S3 backup bucket.
  4. restore_drill launches a container to restore the archive into a dedicated scratch database (restore_database) using pg_restore --clean --if-exists.
  5. restore_stats inspects the scratch database and counts the restored user tables.
  6. verify_backup_integrity (io.kestra.plugin.core.flow.If) compares source and restored table counts:
    • On match: Logs verification metrics, publishes output metadata, and notifies Slack.
    • On mismatch: Dispatches an incident alert to Slack and fails the execution via Fail.
  7. An errors block catches unexpected worker failures, network issues, or S3 upload timeouts.

What you get

  • Automated Disaster Recovery Testing: Every night's backup is tested against a real PostgreSQL instance before you need it in production.
  • Cloud Cold Storage: Timestamped archives in Amazon S3 ready for point-in-time recovery.
  • Zero Credential Leaks: Fully parameterized with secure Kestra secret placeholders.
  • Instant Feedback: Slack alerts containing backup size, table counts, and exact S3 paths.

Prerequisites

  • A PostgreSQL 13+ instance accessible from the Kestra worker.
  • A dedicated scratch database or permissions to drop and recreate drill tables (never point restore_database to production).
  • An AWS S3 bucket and IAM credentials with s3:PutObject permission.
  • An incoming Slack webhook URL for alerts.

Secrets

  • POSTGRES_HOST: Hostname or IP of the PostgreSQL server.
  • POSTGRES_PORT: Port of the PostgreSQL server (default 5432).
  • POSTGRES_USER: PostgreSQL user with pg_read_all_data / dump permissions.
  • POSTGRES_PASSWORD: Password for the PostgreSQL user.
  • AWS_ACCESS_KEY_ID: AWS IAM access key ID.
  • AWS_SECRET_ACCESS_KEY: AWS IAM secret access key.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook URL for backup status alerts.

Quick start

  1. Add the required PostgreSQL, AWS, and Slack secrets to your Kestra namespace.
  2. Ensure your scratch database (e.g. restore_drill_db) is created on your test PostgreSQL server.
  3. Adjust s3_bucket and database inputs to match your environment.
  4. Trigger a manual execution in the Kestra UI to run the first verification drill.
  5. Keep the nightly_backup_schedule active for recurring 02:00 UTC runs.

How to extend

  • S3 Retention Policy: Chain an S3 lifecycle rule or task to prune archives older than 30 or 90 days.
  • Row Count Parity: Extend source_stats and restore_stats to query exact pg_stat_user_tables.n_live_tup or run checksum queries on critical tables.
  • Multi-Cloud Replication: Add a secondary upload task to replicate dumps to Google Cloud Storage or Azure Blob Storage in parallel.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.