diff --git a/postgres/bin/pg_recovery b/postgres/bin/pg_recovery index 88addf1bb5a22574b3756aa9f5e78ea8c63368d6..2a4b2d06939e8047c6c110329b8567e6b18cba26 100755 --- a/postgres/bin/pg_recovery +++ b/postgres/bin/pg_recovery @@ -9,6 +9,8 @@ PG_HBA=/var/lib/pgsql/data/pg_hba.conf PG_RECOVERY=$PG_DATA/recovery +FORCE=false + if [[ "$(whoami)" != "root" ]]; then echo "Script must be run as root user" exit 1 @@ -16,17 +18,19 @@ fi function recovery () { # If not found recovery.conf - cluster worked in standby mode - if [[ ! -f $PG_RECOVERY".done" ]]; then + if [[ ! -f $PG_RECOVERY".done" && $FORCE == false ]]; then printf "Current PostgreSQL cluster worked on standby mode. If primary server fail, then no automatic switch standby to primary. Try manual execute \"touch /tmp/postgresql.trigger\"\n" exit 1 fi # Detect old primary cluster ip from recovery.done (property primary_conninfo) - PRIMARY_HOST=$(cat $PG_RECOVERY.done | grep primary_conninfo | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}') + if [[ ! -f $PG_RECOVERY".done" && $FORCE == true ]]; then + PRIMARY_HOST=$(cat $PG_RECOVERY.conf | grep primary_conninfo | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}') + else + PRIMARY_HOST=$(cat $PG_RECOVERY.done | grep primary_conninfo | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}') + fi # Detect standby host (current host) STANDBY_HOST=$(cat $PG_CONF | grep archive_command | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}') - echo "[DEBUG]: Primary host: $PRIMARY_HOST" - echo "[DEBUG]: Standby host: $STANDBY_HOST" if [[ $PRIMARY_HOST == $STANDBY_HOST ]]; then if [[ $# != 2 ]]; then @@ -39,10 +43,21 @@ function recovery () { fi fi - # Stop primary database (not necessary(check)) - su $PG_USER -c "ssh $PRIMARY_HOST 'pg_ctl stop -m fast -D $PG_DATA'" + echo -e "After script done:\nPRIMARY - $STANDBY_HOST\nSTANDBY - $PRIMARY_HOST" + read -r -p "Continue[y/N]?" choice + if ! [[ $choice =~ ^([yY][eE][sS]]|[yY])$ ]]; then + exit 0 + fi + # Stop primary pgpool (is necessary!) su $PG_USER -c "ssh $PRIMARY_HOST 'pgpool -m fast stop'" + # Stop primary database (not necessary(check)) + su $PG_USER -c "ssh $PRIMARY_HOST 'pg_ctl stop -m fast -D $PG_DATA'" + su $PG_USER -c "ssh $PRIMARY_HOST 'rm -rf $PG_WAL/*'" + + while [ ! -f $PG_RECOVERY".done" ]; do + sleep 2 + done # Remove hot_standby from postgresql config sed -i "/hot_standby = on/d" $PG_CONF @@ -81,30 +96,21 @@ function recovery () { done } -usage="$(basename "$0") [-h] [-r] [-f] -- Script recovery filed node of stream replication for PostgreSQL Servers >= 9.2 +usage="$(basename "$0") [-h] [-f] -- Script for recovery database cluster where: - -h show this help text and exit - -f force recovery. This means that the following actions will be done:" - -if [ $# -lt 1 ]; then - recovery - exit 0 -fi - -while getopts ':hrf' option; do + -h show this help text and exit + -f force recovery primary cluster" +while getopts ':hf' option; do case "$option" in h) echo "$usage" - exit - ;; - r) recovery - exit ;; f) FORCE=true - exit ;; \?) printf "illegal option: -%s\n" "$OPTARG" >&2 echo "$usage" >&2 - exit 1 ;; - esac - done + *) echo "$usage" + ;; + esac +done +recovery "$@"