#!/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 }}/main while $3 is {{ psql_data_dir }} MASTER_DATA=$1 MASTER_DATA_BASE_DIR={{ psql_data_dir }} RECOVERY_TARGET=${2} RECOVERY_DATA=${3}/main RECOVERY_DATA_BASE_DIR={{ psql_data_dir }} logger "pgpool_recovery_1: MASTER_DATA=$MASTER_DATA" logger "pgpool_recovery_1: RECOVERY_DATA=$RECOVERY_DATA" archive_dir={{ psql_wal_archiving_log_dir }} # 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" # If the archive dir is not empty the backup fails ssh -T $RECOVERY_TARGET \ "[ -d $archive_dir ] && rm $archive_dir/*" # We only use archived WAL logs during recoveries, so delete all # logs from the last recovery to limit the growth. rm $archive_dir/* # With this file present, our archive_command will actually # archive WAL files. touch $archive_dir/backup_in_progress psql -c "select pg_start_backup('pgpool-recovery')" postgres echo "restore_command = 'scp $MASTER_HOST:$archive_dir/%f %p'" > $RECOVERY_DATA/recovery.conf tar -C $MASTER_DATA_BASE_DIR -zcf pgsql.tar.gz main psql -c 'select pg_stop_backup()' postgres scp pgsql.tar.gz $RECOVERY_TARGET:$RECOVERY_DATA_BASE_DIR