-#!/bin/bash
+#!/bin/bash -e
##This is meanst to be run on the slave, with the masters ip as the passed variable. ($1)
sourcehost="$1"
-datadir=/var/lib/pgsql/9.6/main
+datadir=/var/lib/pgsql/9.6/data
archivedir=/var/lib/pgsql/9.6/archive
archivedirdest=/var/lib/pgsql/9.6/archive
+cd # if used as root with sudo -u postgres home dir won't be readable and we will get errors
+
#Usage
if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ];
then
#Check if the supposed master is actually a master
CheckIfMasterIsActuallyAMaster () {
- ismaster="$(ssh postgres@"$1" 'if [ -f /var/lib/pgsql/9.6/main/recovery.done ]; then echo "postgres_is_a_master_instance"; else echo "postgres_is_not_master"; fi;')"
+ ismaster="$(ssh postgres@"$1" 'if [ -f /var/lib/pgsql/9.6/data/recovery.done ]; then echo "postgres_is_a_master_instance"; else echo "postgres_is_not_master"; fi;')"
if [[ "$ismaster" = "postgres_is_not_master" ]]
then
fi
#Remove old WAL logs
- rm /var/lib/pgsql/9.6/archive/*
+ rm -f /var/lib/pgsql/9.6/archive/*
}
#Before doing PutMasterIntoBackupMode clean up archive logs (IE rm or mv /var/lib/pgsql/9.6/archive/*). They are not needed since we are effectivly createing a new base backup and then synching it.
PutMasterIntoBackupMode () {
echo "[INFO] Putting postgres master '$1' in backup mode."
- ssh postgres@"$1" "rm /var/lib/pgsql/9.6/archive/*"
+ ssh postgres@"$1" "rm -f /var/lib/pgsql/9.6/archive/*"
ssh postgres@"$1" "psql -c \"SELECT pg_start_backup('Streaming Replication', true)\" postgres"
}
if [ $? == 0 ]
then
echo "[OK] Transfert completed.";
+ ln -sf $datadir/postgresql.conf.slave $datadir/postgresql.conf
else
echo "[ERROR] Error during transfer !";
exit 0;
#stop postgres and copy transactions made during the last two rsync's
StopPostgreSqlAndFinishRsync () {
echo "[INFO] Stopping master node.."
- ssh postgres@"$1" "/etc/init.d/postgresql stop"
+ ssh postgres@"$1" "sudo systemctl stop postgresql-9.6"
echo "[INFO] Transfering xlog files from master... "
rsync -av --delete --progress -e ssh "$sourcehost":"$datadir"/pg_xlog/ "$datadir"/pg_xlog/ > /dev/null
if [ $? == 0 ]
else
echo "[OK] Master started.";
fi
+
+ echo -n "[INFO] Wating for replication to start "
+ while [ "`psql -c 'SELECT * from pg_stat_replication' -h $1 -t postgres postgres | wc -l`" -lt 2 ] ; do
+ echo -n .
+ sleep 1
+ done
+ echo "OK"
+
}
#Execute above operations