ansible-role-postgresql/templates/pgpool_recovery_stage_1.j2

41 lines
1.4 KiB
Plaintext
Raw Normal View History

2020-06-03 19:53:13 +02:00
#!/bin/bash
# PGpool stage 1 recovery script
# Reference: http://michael.stapelberg.de/Artikel/replicated_postgresql_with_pgpool
#
TS=$(date +%Y-%m-%d_%H-%M-%S)
MASTER_HOST=$(hostname -f)
2021-02-05 16:38:04 +01:00
# $1 is {{ psql_data_dir }} while $3 is {{ psql_data_dir }}
#MASTER_DATA="{{ psql_data_dir }}"
MASTER_DATA="${1}"
2020-06-03 19:53:13 +02:00
RECOVERY_TARGET=${2}
#RECOVERY_DATA="{{ psql_data_dir }}"
RECOVERY_DATA="${3}"
2020-06-03 19:53:13 +02:00
logger "pgpool_recovery_1: MASTER_DATA=$MASTER_DATA"
logger "pgpool_recovery_1: RECOVERY_DATA=$RECOVERY_DATA"
# Ensure that postgres is shut down on the target node
ssh -T $RECOVERY_TARGET sudo /etc/init.d/postgresql stop
# Move the PostgreSQL data directory out of our way.
ssh -T $RECOVERY_TARGET \
"[ -d $RECOVERY_DATA ] && mv $RECOVERY_DATA $RECOVERY_DATA.$TS"
# We only use archived WAL logs during recoveries, so delete all
# logs from the last recovery to limit the growth.
rm $MASTER_DATA/archive_log/*
2020-06-03 19:53:13 +02:00
# With this file present, our archive_command will actually
# archive WAL files.
touch $MASTER_DATA/archive_log/backup_in_progress
# Perform a backup of the database.
ssh -T $RECOVERY_TARGET \
"pg_basebackup -h $MASTER_HOST -D $RECOVERY_DATA --xlog"
2020-06-03 19:53:13 +02:00
# Configure the restore_command to use the archive_log WALs well copy
# over in 2nd_stage.sh.
echo "restore_command = 'cp $RECOVERY_DATA/archive_log/%f %p'" | \
ssh -T $RECOVERY_TARGET "cat > $RECOVERY_DATA/recovery.conf"
2020-06-03 19:53:13 +02:00