Webhook icon
Schedule icon
If icon
Fail icon
Commands icon
Docker icon
Set icon
SlackIncomingWebhook icon

Prove Your Postgres Backups Restore to the Second You Need

Restore a Postgres base backup and WAL to a target time in a throwaway container, check the data, and fail when the restore misses your RTO.

Categories
DataInfrastructure

A backup you have never restored is a hope, not a backup. Point-in-time recovery (PITR) is what saves you from the accidental DELETE at 14:03: restore last night's base backup, replay the WAL archive up to 14:02:59, and the rows are back. But PITR breaks quietly. A WAL segment that never reached the bucket, an archive_command that started failing, a base backup from the wrong major version, you only find out during the incident.

This blueprint runs the recovery for real, on a schedule or right after each base backup, and tells you whether it worked and how long it took.

How it works

  1. after_base_backup (Webhook, called by your backup job) or weekly_drill (Schedule, shipped disabled) starts a drill. concurrency runs one drill at a time.
  2. validate (If) stops early when s3 mode has no bucket.
  3. restore runs one throwaway PostgreSQL container (Docker task runner):
    • demo mode builds a source cluster with WAL archiving, takes a pg_basebackup, writes 1000 more orders, notes the time, then deletes every order. The drill must bring back all 2000 orders.
    • s3 mode downloads base.tar.gz (and pg_wal.tar.gz) from s3_base_prefix and sets restore_command to aws s3 cp s3://<bucket>/<wal prefix>%f %p, the same layout an archive_command such as aws s3 cp %p s3://bucket/wal/%f writes.
    • It writes recovery.signal, recovery_target_time and recovery_target_action = 'promote', starts PostgreSQL, and waits until recovery has ended and the server was promoted.
    • Each SQL in checks runs on the recovered database and must return ok and detail.
  4. verdict (If) passes the drill only when every check passed and the restore took less than rto_minutes. The last good drill is kept in the KV store and quoted in every failure message.
  5. The flow sla (MAX_DURATION, 1 hour) fails a drill that hangs, for example on a download that never ends, and labels it drill: deadline_exceeded; afterExecution reports it.

Error handling

  • A WAL gap fails the drill, with PostgreSQL's reason. PostgreSQL accepts read-only connections before recovery is finished, then shuts down when a segment is missing. The drill watches the server and reports "recovery stopped: recovery ended before configured recovery target was reached".
  • Wrong or missing base backup: "no base.tar.gz under s3://bucket/base/latest/ (expected pg_basebackup -Ft -z output)". A backup the server cannot start from fails with the FATAL line from its log.
  • A check that cannot run (wrong table, syntax error) counts as failed and quotes the database error, so a broken check never passes silently.
  • Slower than the RTO is a failure, even when the data is right: "the restore took 2 s, over the 0 min RTO" (a test with rto_minutes: 0).
  • Hung drills are stopped by the SLA. Every failure sends one Slack message with the task, the cause and the date of the last good drill. A run the SLA stops is reported from afterExecution, because errors is not guaranteed to run after an SLA failure.
  • Nothing touches production. The drill only reads the bucket; the recovered database lives and dies inside the container.

What this blueprint teaches about Kestra

  • A flow-level sla (MAX_DURATION with behavior: FAIL and labels) as a deadline for the whole run, next to an RTO check on the measured duration, and afterExecution to alert on a run the SLA stopped.
  • The Docker task runner as a disposable environment: a full PostgreSQL server starts, recovers and is thrown away in one task.
  • Script outputs (::{"outputs": ...}::) built as JSON by PostgreSQL itself, then used in If conditions and Slack messages.
  • The KV store as memory between runs ("last good drill").
  • Webhook + Schedule: event-driven after each backup, with a periodic safety net.

Prerequisites

  • A Kestra worker that can run Docker containers.
  • For s3 mode: a base backup made with pg_basebackup -Ft -z (for example pg_basebackup -D /tmp/b -Ft -z -X stream then aws s3 cp /tmp/b s3://bucket/base/latest/ --recursive), and WAL archived with archive_command = 'aws s3 cp %p s3://bucket/wal/%f'.
  • A Slack incoming webhook.

Secrets

  • SLACK_WEBHOOK_URL: Slack incoming webhook.
  • PITR_DRILL_WEBHOOK_KEY: a long random string, the key in the webhook URL your backup job calls.
  • AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY: read access to the bucket (s3 mode only; demo mode needs no AWS secrets).

Inputs

  • mode (SELECT demo or s3, default demo).
  • s3_bucket, s3_base_prefix (base/latest/), s3_wal_prefix (wal/), s3_endpoint (MinIO, Ceph or R2; empty for AWS).
  • target_time (STRING): recover to this moment; empty replays every archived segment.
  • checks (ARRAY of SQL): each returns ok (boolean) and detail (text), for example SELECT count(*) > 0 AS ok, count(*) || ' orders' AS detail FROM orders.
  • rto_minutes (INT, default 30), postgres_image (default postgres:16-alpine; match your server's major version).

Outputs

  • result (JSON): restore seconds, WAL segments replayed, base backup size, target, last replayed transaction time, and every check with its result.

Quick start

  1. Add SLACK_WEBHOOK_URL and PITR_DRILL_WEBHOOK_KEY, then run the flow with the defaults. The demo deletes every order and recovers to the second before the DELETE: "PostgreSQL restore drill PASSED (demo): recovered to … in 2 s … ok: 2000 orders recovered (2000 at the target time, 0 after the accidental DELETE)".
  2. Switch to s3, set the bucket, prefixes and your own checks, and add the AWS secrets.
  3. Call the webhook at the end of your base backup job (curl -X POST "$KESTRA/api/v1/main/executions/webhook/company.team/postgres-pitr-restore-drill/$KEY"), and enable weekly_drill as a safety net.

How to extend

  • Restore from pgBackRest or WAL-G instead: replace the download and restore_command lines with pgbackrest restore --type=time or wal-g backup-fetch / wal-g wal-fetch.
  • Record restore_seconds in a table and alert when it trends towards the RTO.
  • Run checks that matter to your business: the newest order is less than an hour older than the target, invoices balance, no table is empty.

Plugins used

See How

New to Kestra?

Use blueprints to kickstart your first workflows.