upgrade pgpool to 3.6.6
[pgpool-online-recovery] / streaming-replication.sh
index ebe20bc..f3ca1e0 100755 (executable)
@@ -1,11 +1,13 @@
-#!/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
@@ -36,7 +38,7 @@ CheckIfPostgresIsRunningOnRemoteHost () {
 
 #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
@@ -68,7 +70,7 @@ PrepareLocalServer () {
     fi 
 
     #Remove old WAL logs
-    rm /var/lib/pgsql/9.6/archive/*
+    rm -f /var/lib/pgsql/9.6/archive/*
 }
 
 
@@ -87,7 +89,7 @@ CheckForRecoveryConfig () {
 #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"
 }
 
@@ -98,6 +100,7 @@ RsyncWhileLive () {
     if [ $? == 0 ]
     then
         echo "[OK] Transfert completed.";
+               ln -sf $datadir/postgresql.conf.slave $datadir/postgresql.conf
     else
        echo "[ERROR] Error during transfer !";
        exit 0;
@@ -123,7 +126,7 @@ StopBackupModeAndArchiveIntoWallLog () {
 #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 ]
@@ -153,6 +156,14 @@ StartLocalAndThenRemotePostGreSql () {
     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