-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg-backup-configmap.yaml
26 lines (25 loc) · 1.32 KB
/
pg-backup-configmap.yaml
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
# pg-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: pg-script-config
data:
postgresql.sh: |
# Extract replica name from the pod's hostname (e.g., pg-slave-0)
REPLICA_NAME=$(hostname | sed 's/-/_/g')
rm -rf /var/lib/postgresql/data/* # Ensure the directory is empty
until pg_isready --host=$PG_MASTER_HOST --port=$PG_PORT; do
echo "Waiting for master to be ready..."
sleep 2
done
pg_basebackup -h $PG_MASTER_HOST -D /tmp/pgdata -U postgres -v -P --wal-method=stream
mv /tmp/pgdata/* /var/lib/postgresql/data/
chown -R postgres:postgres /var/lib/postgresql/data
psql -h $PG_MASTER_HOST -U postgres -c "SELECT pg_create_physical_replication_slot('${REPLICA_NAME}');"
echo "primary_slot_name = '${REPLICA_NAME}'" | tee -a /var/lib/postgresql/data/postgresql.conf
echo "primary_conninfo = 'host=$PG_MASTER_HOST port=$PG_PORT user=postgres password=password application_name=${REPLICA_NAME}'" | tee -a /var/lib/postgresql/data/postgresql.conf
touch /var/lib/postgresql/data/standby.signal
chmod 700 /var/lib/postgresql/data/
rm -rf /tmp/pgdata
echo "Starting PostgreSQL..."
exec postgres # Use exec to replace the shell with the PostgreSQL process