#!/bin/bash
# Online recovery 2nd stage script

set -o xtrace

MAIN_NODE_PGDATA="$1"              # main node dabatase cluster path
DEST_NODE_HOST="$2"                # hostname of the DB node to be recovered
DEST_NODE_PGDATA="$3"              # database cluster path of the DB node to be recovered
MAIN_NODE_PORT="$4"                # main node port number
DEST_NODE_ID="$5"                  # node id of the DB node to be recovered
DEST_NODE_PORT="$6"                # port number of the DB node to be recovered
MAIN_NODE_HOST="$7"                # main node hostname


PGHOME=/usr/pgsql-14
ARCHIVEDIR=/var/lib/pgsql/archivedir  # archive log directory
POSTGRESQL_STARTUP_USER=postgres
SSH_KEY_FILE=id_rsa_pgpool
SSH_OPTIONS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/${SSH_KEY_FILE}"

# Force to flush current value of sequences to xlog
${PGHOME}/bin/psql -h $MAIN_NODE_HOST -p $MAIN_NODE_PORT -t -c 'SELECT datname FROM pg_database WHERE NOT datistemplate AND datallowconn' template1|
while read i
do
  if [ "$i" != "" ]; then
    psql -h $MAIN_NODE_HOST -p $MAIN_NODE_PORT -c "SELECT setval(oid, nextval(oid)) FROM pg_class WHERE relkind = 'S'" $i
  fi
done

if [ $? -ne 0 ]; then
    echo ERROR: recovery_2nd_stage: failed.
    exit 1
fi

psql -h $MAIN_NODE_HOST -p $MAIN_NODE_PORT -c "SELECT pgpool_switch_xlog('$ARCHIVEDIR')" template1

if [ $? -ne 0 ]; then
    echo ERROR: recovery_2nd_stage: pgpool_switch_xlog failed.
    exit 1
fi

# start target server as a streaming replication standby server
ssh -T ${SSH_OPTIONS} ${POSTGRESQL_STARTUP_USER}@$DEST_NODE_HOST "
        $PGHOME/bin/pg_ctl -l /dev/null -w -D $DEST_NODE_PGDATA promote
"

if [ $? -ne 0 ]; then
    echo ERROR: recovery_2nd_stage: promote failed.
    exit 1
fi

echo recovery_2nd_stage: end: recovery_2nd_stage is completed successfully 
exit 0
