Another attempt to fix the pgpool recovery scripts.

This commit is contained in:
Andrea Dell'Amico 2021-02-05 19:44:42 +01:00
parent 8583309e0f
commit ebb22e6642
4 changed files with 28 additions and 30 deletions

View File

@ -19,7 +19,8 @@ psql_db_size_c: 170000000
psql_listen_on_ext_int: False
psql_use_alternate_data_dir: False
# Deb/Ubuntu
psql_data_dir: '/var/lib/postgresql/{{ psql_version }}/main'
psql_data_root_dir: '/var/lib/postgresql/{{ psql_version }}'
psql_data_dir: '{{ psql_data_root_dir }}/main'
psql_conf_dir: '/etc/postgresql/{{ psql_version }}/main'
psql_log_dir: /var/log/postgresql

View File

@ -5,17 +5,15 @@
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=$1
MASTER_DATA_BASE_DIR={{ psql_data_dir }}
#MASTER_DATA="{{ psql_data_dir }}"
MASTER_DATA="${1}"
RECOVERY_TARGET=${2}
RECOVERY_DATA=${3}
RECOVERY_DATA_BASE_DIR={{ psql_data_dir }}
#RECOVERY_DATA="{{ psql_data_dir }}"
RECOVERY_DATA="${3}"
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
@ -23,21 +21,20 @@ ssh -T $RECOVERY_TARGET sudo /etc/init.d/postgresql stop
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/*
rm $MASTER_DATA/archive_log/*
# With this file present, our archive_command will actually
# archive WAL files.
touch $archive_dir/backup_in_progress
touch $MASTER_DATA/archive_log/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
# 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"

View File

@ -1,20 +1,20 @@
#!/bin/bash
# Online recovery 2nd stage script
#
MASTER_DATA_BASE_DIR={{ psql_data_dir }}
MASTER_DATA=${1}
#MASTER_DATA="{{ psql_data_dir }}"
MASTER_DATA="${1}"
RECOVERY_TARGET=${2}
RECOVERY_DATA_BASE_DIR={{ psql_data_dir }}
RECOVERY_DATA=${3}
port={{ psql_db_port }} # PostgreSQL port number
#RECOVERY_DATA="{{ psql_data_dir }}"
RECOVERY_DATA="${3}"
archive_dir={{ psql_wal_archiving_log_dir }}
port="{{ psql_db_port }}" # PostgreSQL port number
archive_dir="{{ psql_wal_archiving_log_dir }}"
logger "pgpool_recovery_2: MASTER_DATA=$MASTER_DATA"
logger "pgpool_recovery_2: RECOVERY_DATA=$RECOVERY_DATA"
logger "pgpool_recovery_2: archive_dir=$archive_dir"
# Force to flush current value of sequences to xlog
psql -p $port -t -c 'SELECT datname FROM pg_database WHERE NOT datistemplate AND datallowconn' template1|
while read i
@ -27,11 +27,11 @@ done
# Flush all transactions to disk. Since pgpool stopped all connections,
# there cannot be any data that does not reside on disk until the
# to-be-recovered host is back on line.
psql -p $port -c "SELECT pgpool_switch_xlog('$archive_dir')" template1
psql -p $port -c "SELECT pgpool_switch_xlog('$MASTER_DATA/archive_log')" template1
# Copy over all archive logs at once.
rsync -avx --delete $archive_dir/ \
$RECOVERY_TARGET:$archive_dir/
rsync -avx --delete $MASTER_DATA/archive_log/ \
$RECOVERY_TARGET:$RECOVERY_DATA/archive_log/
# Delete the flag file to disable WAL archiving again.
rm $archive_dir/backup_in_progress
rm $MASTER_DATA/archive_log/backup_in_progress

View File

@ -3,6 +3,6 @@ DEST=$1
DESTDIR=$2
# Deploy a base backup
ssh -T $DEST 'cd {{ psql_data_dir }}; tar zxf pgsql.tar.gz' 2>/dev/null 1>/dev/null < /dev/null
ssh -T $DEST 'cd {{ psql_data_root_dir }}; tar zxf pgsql.tar.gz' 2>/dev/null 1>/dev/null < /dev/null
# Startup PostgreSQL server
ssh -T $DEST sudo /etc/init.d/postgresql start 2>/dev/null 1>/dev/null < /dev/null