forked from KCEE0901/trustchain-escrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore_pitr.sh
More file actions
executable file
·84 lines (67 loc) · 2.66 KB
/
Copy pathrestore_pitr.sh
File metadata and controls
executable file
·84 lines (67 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
# restore_pitr.sh — Prepare Point-In-Time Recovery from base backup + WAL archive
# Usage:
# bash scripts/restore_pitr.sh <basebackup.tar.gz> <target_time>
# Example: bash scripts/restore_pitr.sh /var/backups/stellar-trust/basebackup_20260328T020000Z.tar.gz "2026-03-28 01:15:00 UTC"
set -euo pipefail
BASEBACKUP_FILE="${1:-}"
RECOVERY_TARGET_TIME="${2:-}"
TARGET_PGDATA="${TARGET_PGDATA:-/var/lib/postgresql/pitr_data}"
WAL_ARCHIVE_S3_BUCKET="${WAL_ARCHIVE_S3_BUCKET:-}"
S3_SSE_ALGORITHM="${S3_SSE_ALGORITHM:-AES256}"
if [[ -z "$BASEBACKUP_FILE" ]]; then
echo "ERROR: basebackup file path required"
echo "Usage: $0 <basebackup.tar.gz> [recovery_target_time]"
exit 1
fi
if [[ -z "$WAL_ARCHIVE_S3_BUCKET" ]]; then
echo "ERROR: WAL_ARCHIVE_S3_BUCKET is required for WAL replay"
exit 1
fi
if [[ ! -f "$BASEBACKUP_FILE" ]]; then
echo "ERROR: basebackup file '$BASEBACKUP_FILE' not found"
exit 1
fi
if pgrep -f "postgres" >/dev/null 2>&1; then
echo "ERROR: Please stop PostgreSQL before running PITR restore preparation."
exit 1
fi
mkdir -p "$TARGET_PGDATA"
rm -rf "${TARGET_PGDATA:?}"/*
echo "Unpacking base backup into $TARGET_PGDATA"
tar -xzf "$BASEBACKUP_FILE" -C "$TARGET_PGDATA"
cat > "$TARGET_PGDATA/recovery.conf" <<EOF
restore_command = 'aws s3 cp "${WAL_ARCHIVE_S3_BUCKET}/wal/%f" "%p" --sse ${S3_SSE_ALGORITHM} --quiet'
recovery_target_timeline = 'latest'
EOF
if [[ -n "$RECOVERY_TARGET_TIME" ]]; then
cat >> "$TARGET_PGDATA/recovery.conf" <<EOF
recovery_target_time = '${RECOVERY_TARGET_TIME}'
EOF
echo "Configured recovery_target_time=${RECOVERY_TARGET_TIME}"
fi
# For PostgreSQL 12+ can ensure recovery.signal file exists and remove standby.signal
if [[ -n "$(find "$TARGET_PGDATA" -maxdepth 1 -name 'postgresql.conf' -print -quit)" ]]; then
touch "$TARGET_PGDATA/recovery.signal"
fi
cat >> "$TARGET_PGDATA/postgresql.auto.conf" <<EOF
restore_command = 'aws s3 cp "${WAL_ARCHIVE_S3_BUCKET}/wal/%f" "%p" --sse ${S3_SSE_ALGORITHM} --quiet'
recovery_target_timeline = 'latest'
EOF
if [[ -n "$RECOVERY_TARGET_TIME" ]]; then
cat >> "$TARGET_PGDATA/postgresql.auto.conf" <<EOF
recovery_target_time = '${RECOVERY_TARGET_TIME}'
EOF
fi
cat > "$TARGET_PGDATA/pitr_instructions.txt" <<EOF
PITR prepared at: $(date -u +"%Y-%m-%dT%H:%M:%SZ")
* data directory: $TARGET_PGDATA
* using base backup: $BASEBACKUP_FILE
* WAL archive source: $WAL_ARCHIVE_S3_BUCKET/wal
* recovery_target_time: ${RECOVERY_TARGET_TIME:-latest}
Start PostgreSQL from this directory:
export PGDATA=$TARGET_PGDATA
pg_ctl -D "$TARGET_PGDATA" start
Monitor logs for recovery progress in pg_wal/recovery.done.
EOF
echo "PITR setup complete. PostgreSQL can be started from $TARGET_PGDATA"