#!/bin/bash

# This script will backup PostgreSQL data dir and sync new from master DB node.
if [ -d /var/lib/postgresql/16 ]; then # Ubuntu 24.04
	DATA=/var/lib/postgresql/16/main
	ARCH=/var/lib/postgresql/16/archive
	LOCK=/var/run/postgresql/.s.PGSQL.5432.lock
elif [ -d /var/lib/postgresql/14 ]; then # Ubuntu 22.04
	DATA=/var/lib/postgresql/14/main
	ARCH=/var/lib/postgresql/14/archive
	LOCK=/var/run/postgresql/.s.PGSQL.5432.lock
elif [ -d /var/lib/postgresql/10 ]; then # Ubuntu 18.04
	DATA=/var/lib/postgresql/10/main
	ARCH=/var/lib/postgresql/10/archive
	LOCK=/var/run/postgresql/.s.PGSQL.5432.lock
elif [ -d /var/lib/postgresql/9.3 ]; then # Ubuntu 14.04
	DATA=/var/lib/postgresql/9.3/main
	ARCH=/var/lib/postgresql/9.3/archive
	LOCK=/var/run/postgresql/.s.PGSQL.5432.lock
elif [ -d /var/lib/pgsql ]; then # RHEL 8
	DATA=/var/lib/pgsql/data
	ARCH=/var/lib/pgsql/archive
	LOCK=/var/run/postgresql/PGSQL.lock
else
	echo "Unable to determine PostgreSQL data directory. Please contact INSOFT."
	exit 1
fi

source /etc/ucs/ucs.conf

# check if all needed parameters are set in /etc/ucs/ucs.conf
echo "Checking required settings in /etc/ucs/ucs.conf..."
variables="NODE1 NODE2"
fail=0
for variable in $variables; do
	if [ "x${!variable}" = "x" ]; then
		fail=1
		echo "Please set $variable in /etc/ucs/ucs.conf"
	fi
done
if [ $fail -ne 0 ]; then
	exit 1
fi

if [ "`hostname`" = "$NODE1" ]; then
    MASTER=$NODE2
else
    MASTER=$NODE1
fi

NOW=`date +%F-%T`
echo
echo "Are you sure to sync data from master node $MASTER?"
echo
echo "This script will:"
echo " * put cluster resource PostgreSQL to unmanaged state"
echo " * stop instance of PostgreSQL on this node"
echo " * rename data dir to $DATA.$NOW"
echo " * rename archive dir to $ARCH.$NOW"
echo " * synchronize PostgreSQL data from $MASTER"
echo " * remove PostgreSQL lock file $LOCK"
echo " * cleanup possible Pacemaker PostgreSQL resource error"
echo " * and put PostgreSQL back into managed state"
echo
read -p"Type YES to stop PostgreSQL and sync data from master node $MASTER: " answer
echo
if [ "$answer" != "YES" ]; then
    echo "Terminating..."
    exit 1
fi

CRM_RUNNING=true
crm status >/dev/null 2>&1
if [ $? -ne 0 ]; then
    CRM_RUNNING=false
    echo
    echo "CRM is not running!"
		echo
		read -p"Type YES if you are just setting up new cluster: " answer
		echo
		if [ "$answer" != "YES" ]; then
		    echo "Terminating..."
		    exit 1
		fi
fi

if $CRM_RUNNING; then
	crm resource unmanage PostgreSQL
fi

if [ -f /etc/init.d/postgresql ]; then
	# Ubuntu
	service postgresql stop
else
	# RHEL
	sudo -u postgres pg_ctl stop -D /var/lib/pgsql/data 2> /dev/null
fi

# backup data dir

mv $DATA $DATA.$NOW
mv $ARCH $ARCH.$NOW

# create new archive dir
mkdir $ARCH
chown postgres:postgres $ARCH

# copy data dir from master
sudo -u postgres pg_basebackup -h $MASTER -U postgres -D $DATA -X stream -P --checkpoint=fast

# cleanup lock
rm -f $LOCK

if $CRM_RUNNING; then
	  # cleanup possible errors in Pacemaker
    crm resource cleanup PostgreSQL `crm_node -n`

    # put postgres into managed state again
    crm resource manage PostgreSQL
fi
