id: postgres-pitr-restore-drill
namespace: company.team
description: |
Prove that your PostgreSQL backups restore, to the second you choose. The drill takes the latest base backup and the
archived WAL, replays them into a throwaway PostgreSQL to a target time, runs your checks against the recovered
database, and measures how long it took against your recovery time objective (RTO). Demo mode needs only Docker:
it builds a small database, "accidentally" deletes every row, and recovers to the moment before the DELETE.
triggers:
- id: after_base_backup
type: io.kestra.plugin.core.trigger.Webhook
description: Call this when the nightly base backup has been uploaded, so every
new backup is proven restorable.
key: "{{ secret('PITR_DRILL_WEBHOOK_KEY') }}"
- id: weekly_drill
type: io.kestra.plugin.core.trigger.Schedule
description: A weekly drill even when no backup job calls the webhook. Shipped
disabled; enable it once the S3 inputs are yours.
cron: "13 3 * * 1"
disabled: true
concurrency:
limit: 1
behavior: QUEUE
sla:
- id: restore_deadline
type: MAX_DURATION
duration: PT1H
behavior: FAIL
labels:
drill: deadline_exceeded
inputs:
- id: mode
type: SELECT
values: [ demo, s3 ]
defaults: demo
description: demo builds and restores its own database inside the drill
container. s3 restores your base backup and WAL archive from S3 (or
MinIO).
- id: s3_bucket
type: STRING
required: false
description: Bucket that holds the base backup and the WAL archive (s3 mode).
- id: s3_base_prefix
type: STRING
defaults: base/latest/
description: Prefix of the base backup made with `pg_basebackup -Ft -z`, i.e.
base.tar.gz and (if WAL was streamed) pg_wal.tar.gz.
- id: s3_wal_prefix
type: STRING
defaults: wal/
description: Prefix your archive_command writes WAL segments to, e.g. `aws s3 cp
%p s3://bucket/wal/%f`.
- id: s3_endpoint
type: STRING
required: false
description: Custom S3 endpoint (MinIO, Ceph, R2). Leave empty for AWS.
- id: target_time
type: STRING
required: false
description: Recover to this moment, e.g. "2026-10-14 03:00:00+00". Empty means
replay every archived WAL segment. Demo mode picks its own target.
- id: checks
type: ARRAY
itemType: STRING
required: false
description: One SQL query per item, run on the recovered database. Each must
return rows with a boolean `ok` and a text `detail`. Demo mode adds its
own check.
- id: rto_minutes
type: INT
defaults: 30
description: Recovery time objective. A restore that takes longer fails the
drill even if every check passes.
- id: postgres_image
type: STRING
defaults: postgres:16-alpine
description: Use the major version of your server; WAL can only be replayed by
the same major version.
tasks:
- id: validate
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.mode == 's3' and (inputs.s3_bucket ?? '') == '' }}"
then:
- id: missing_bucket
type: io.kestra.plugin.core.execution.Fail
errorMessage: s3 mode needs s3_bucket (and the AWS_ACCESS_KEY_ID /
AWS_SECRET_ACCESS_KEY secrets).
- id: restore
type: io.kestra.plugin.scripts.shell.Commands
description: |
Everything happens inside one throwaway container: fetch the base backup, configure recovery (restore_command,
recovery_target_time), start PostgreSQL, wait until recovery ends, then run the checks and report as JSON.
containerImage: "{{ inputs.postgres_image }}"
taskRunner:
type: io.kestra.plugin.scripts.runner.docker.Docker
pullPolicy: IF_NOT_PRESENT
interpreter: [ sh, -c ]
env:
MODE: "{{ inputs.mode }}"
S3_BUCKET: "{{ inputs.s3_bucket ?? '' }}"
S3_BASE_PREFIX: "{{ inputs.s3_base_prefix }}"
S3_WAL_PREFIX: "{{ inputs.s3_wal_prefix }}"
S3_ENDPOINT: "{{ inputs.s3_endpoint ?? '' }}"
TARGET_TIME: "{{ inputs.target_time ?? '' }}"
CHECKS: "{{ (inputs.checks ?? []) | join('
') }}"
AWS_ACCESS_KEY_ID: "{{ inputs.mode == 's3' ? secret('AWS_ACCESS_KEY_ID') : '' }}"
AWS_SECRET_ACCESS_KEY: "{{ inputs.mode == 's3' ? secret('AWS_SECRET_ACCESS_KEY') : '' }}"
AWS_DEFAULT_REGION: us-east-1
commands:
- |
set -eu
W=/tmp/drill; rm -rf $W; mkdir -p $W/archive $W/base $W/restore; chown -R postgres $W
pg() { su-exec postgres "$@"; }
fail() { echo "DRILL FAILED: $*" >&2; exit 1; }
if [ "$MODE" = demo ]; then
# a source cluster that archives its WAL, a base backup, more writes, then the accident
pg initdb -D $W/src -A trust --no-locale -E UTF8 >/dev/null 2>&1
printf "wal_level = replica\narchive_mode = on\narchive_command = 'cp %%p $W/archive/%%f'\nlisten_addresses = ''\nunix_socket_directories = '$W'\n" >> $W/src/postgresql.conf
pg pg_ctl -D $W/src -w -l $W/src.log start >/dev/null
SQL="psql -h $W -p 5432 -U postgres -qAt -v ON_ERROR_STOP=1"
pg $SQL -c "CREATE TABLE orders (id int PRIMARY KEY, total numeric)" -c "INSERT INTO orders SELECT i, i * 1.5 FROM generate_series(1, 1000) i"
pg pg_basebackup -h $W -p 5432 -U postgres -D $W/base -Ft -z -X stream
pg $SQL -c "INSERT INTO orders SELECT i, i * 1.5 FROM generate_series(1001, 2000) i"
sleep 1; TARGET_TIME=$(pg $SQL -c "SELECT now()"); sleep 1
pg $SQL -c "DELETE FROM orders" -c "SELECT pg_switch_wal()" >/dev/null
sleep 2; pg pg_ctl -D $W/src -m fast -w stop >/dev/null
echo "demo: 2000 orders existed at $TARGET_TIME, then every row was deleted; recovering to $TARGET_TIME"
RESTORE_CMD="cp $W/archive/%f %p"
CHECKS="SELECT count(*) = 2000 AS ok, count(*) || ' orders recovered (2000 at the target time, 0 after the accidental DELETE)' AS detail FROM orders
$CHECKS"
else
command -v aws >/dev/null || apk add --no-cache aws-cli >/dev/null || fail "aws-cli is not installed and could not be installed"
EP=""; [ -n "$S3_ENDPOINT" ] && EP="--endpoint-url $S3_ENDPOINT"
aws s3 cp "s3://$S3_BUCKET/$S3_BASE_PREFIX" $W/base --recursive --only-show-errors $EP || fail "could not download s3://$S3_BUCKET/$S3_BASE_PREFIX"
[ -f $W/base/base.tar.gz ] || fail "no base.tar.gz under s3://$S3_BUCKET/$S3_BASE_PREFIX (expected pg_basebackup -Ft -z output)"
RESTORE_CMD="aws s3 cp s3://$S3_BUCKET/$S3_WAL_PREFIX%f %p --only-show-errors $EP"
fi
BACKUP_MB=$(du -sm $W/base | cut -f1)
START=$(date +%s)
tar xzf $W/base/base.tar.gz -C $W/restore || fail "base.tar.gz is not a readable tar.gz archive"
[ -f $W/base/pg_wal.tar.gz ] && tar xzf $W/base/pg_wal.tar.gz -C $W/restore/pg_wal
printf "restore_command = '%s'\nrecovery_target_action = 'promote'\narchive_mode = off\nlisten_addresses = ''\nunix_socket_directories = '$W'\nport = 5433\n" "$RESTORE_CMD" >> $W/restore/postgresql.auto.conf
[ -n "$TARGET_TIME" ] && echo "recovery_target_time = '$TARGET_TIME'" >> $W/restore/postgresql.auto.conf
touch $W/restore/recovery.signal; chown -R postgres $W; chmod 700 $W/restore
if ! pg pg_ctl -D $W/restore -l $W/restore.log -w -t 3000 start >/dev/null; then
tail -n 15 $W/restore.log >&2; fail "PostgreSQL did not start from the backup: $(grep -E 'FATAL|PANIC' $W/restore.log | tail -n 1)"
fi
SQL="psql -h $W -p 5433 -U postgres -qAt -v ON_ERROR_STOP=1"
# pg_ctl returns once the server accepts read-only connections; recovery can still fail after that (a WAL gap)
until [ "$(pg $SQL -c 'SELECT pg_is_in_recovery()' 2>/dev/null)" = f ]; do
pg pg_ctl -D $W/restore status >/dev/null || { tail -n 15 $W/restore.log >&2; fail "recovery stopped: $(grep -E 'FATAL|PANIC' $W/restore.log | tail -n 1 | sed 's/.*\(FATAL\|PANIC\): *//')"; }
sleep 1
done
SECONDS_TAKEN=$(( $(date +%s) - START ))
STOPPED=$(grep -o 'recovery stopping before .*' $W/restore.log | tail -n 1 || true)
LAST=$(grep -o 'last completed transaction was at log time .*' $W/restore.log | tail -n 1 | sed 's/.*log time //' || true)
WAL=$(grep -c 'restored log file' $W/restore.log || true)
echo "recovered in ${SECONDS_TAKEN}s, $WAL WAL segments from the archive. ${STOPPED:-replayed to the end of the archive}"
RESULTS=$(printf '%s\n' "$CHECKS" | while IFS= read -r q; do
[ -z "$q" ] && continue
pg $SQL -c "SELECT coalesce(json_agg(json_build_object('ok', ok, 'detail', detail)), '[]') FROM ($q) c" 2>$W/check.err \
|| echo "[{\"ok\": false, \"detail\": \"check could not run: $(head -n 1 $W/check.err | tr -d '\"\\\\')\"}]"
done | tr '\n' ',' | sed 's/,$//')
# psql only interpolates :'variables' in scripts read from stdin, not in -c
OUT=$(echo "SELECT json_build_object('restore_seconds', :'secs'::int, 'wal_segments', :'wal'::int, 'backup_mb', :'mb'::int, 'target', :'target', 'recovered_to', :'last',
'checks', (SELECT json_agg(e) FROM json_array_elements(:'results'::json) a, json_array_elements(a) e),
'all_ok', coalesce((SELECT bool_and((e->>'ok')::boolean) FROM json_array_elements(:'results'::json) a, json_array_elements(a) e), true))" \
| pg $SQL -v results="[${RESULTS}]" -v secs="$SECONDS_TAKEN" -v wal="$WAL" -v mb="$BACKUP_MB" -v target="${TARGET_TIME:-end of archive}" -v last="${LAST:-unknown}" -f -)
pg pg_ctl -D $W/restore -m fast -w stop >/dev/null
echo "::{\"outputs\":$OUT}::"
- id: verdict
type: io.kestra.plugin.core.flow.If
description: The drill passes only when every check passed and the restore was
faster than the RTO.
condition: "{{ outputs.restore.vars.all_ok == true and
outputs.restore.vars.restore_seconds <= inputs.rto_minutes * 60 }}"
then:
- id: remember_good_restore
type: io.kestra.plugin.core.kv.Set
key: pitr_drill_last_good
kvType: JSON
value: "{{ {'at': execution.startDate, 'mode': inputs.mode, 'restore_seconds':
outputs.restore.vars.restore_seconds, 'target':
outputs.restore.vars.target} | toJson }}"
- id: report_pass
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
PostgreSQL restore drill PASSED ({{ inputs.mode }}): recovered to {{ outputs.restore.vars.target }} in {{ outputs.restore.vars.restore_seconds }} s (RTO {{ inputs.rto_minutes }} min), {{ outputs.restore.vars.wal_segments }} WAL segments replayed from a {{ outputs.restore.vars.backup_mb }} MB base backup.
{% for c in outputs.restore.vars.checks %}- {{ c.ok ? 'ok' : 'FAILED' }}: {{ c.detail }}
{% endfor %}
else:
- id: drill_failed
type: io.kestra.plugin.core.execution.Fail
description: The report goes out once, through `errors`, like every other failure.
errorMessage: |
DRILL FAILED: {% if outputs.restore.vars.restore_seconds > inputs.rto_minutes * 60 %}the restore took {{ outputs.restore.vars.restore_seconds }} s, over the {{ inputs.rto_minutes }} min RTO. {% endif %}{% if not outputs.restore.vars.all_ok %}a check failed on the recovered database. {% endif %}Recovered to {{ outputs.restore.vars.target }} ({{ inputs.mode }} mode).
{% for c in outputs.restore.vars.checks %}- {{ c.ok ? 'ok' : 'FAILED' }}: {{ c.detail }}
{% endfor %}
errors:
- id: unless_deadline
type: io.kestra.plugin.core.flow.If
description: A run stopped by the SLA is reported by afterExecution instead, so
it is never reported twice.
condition: "{{ (labels.drill ?? '') != 'deadline_exceeded' }}"
then:
- id: alert_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
PostgreSQL restore drill FAILED in execution {{ execution.id }}{% if errorLogs() | length > 0 and (errorLogs()[0]['taskId'] ?? '') != '' %} at `{{ errorLogs()[0]['taskId'] }}`{% endif %}: {% if errorLogs() | length > 0 %}{{ errorLogs() | jq('([.[] | select(.message | test("DRILL FAILED"))] + [.[] | select(.message | test("ERROR:|FATAL:"))] + .)[0].message | sub("^DRILL FAILED: "; "")') | first }}{% else %}see the execution logs.{% endif %}
Last good drill: {{ (kv('pitr_drill_last_good', errorOnMissing=false) ?? {}).at ?? 'never' }}
afterExecution:
- id: deadline_alert
type: io.kestra.plugin.core.flow.If
description: afterExecution runs whatever the final state, so a drill the SLA
stopped is always reported, once.
condition: "{{ (labels.drill ?? '') == 'deadline_exceeded' }}"
then:
- id: alert_deadline
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
PostgreSQL restore drill FAILED in execution {{ execution.id }}: stopped by its SLA deadline (1 hour) before the restore finished, for example a download or a WAL fetch that hangs.
Last good drill: {{ (kv('pitr_drill_last_good', errorOnMissing=false) ?? {}).at ?? 'never' }}
outputs:
- id: result
type: JSON
value: "{{ outputs.restore.vars ?? {} }}"