ansible-role-postgresql-pgpool/templates/pgpool_recovery_stage_1.j2

41 lines
1.4 KiB
Django/Jinja
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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)
# $1 is {{ psql_data_dir }} while $3 is {{ psql_data_dir }}
#MASTER_DATA="{{ psql_data_dir }}"
MASTER_DATA="${1}"
RECOVERY_TARGET=${2}
#RECOVERY_DATA="{{ psql_data_dir }}"
RECOVERY_DATA="${3}"
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/*
# 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"
# 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"