cleanup docs, extract scripts for postgresql
[edozvola] / pg-scripts / create-pgsql-slave.sh
diff --git a/pg-scripts/create-pgsql-slave.sh b/pg-scripts/create-pgsql-slave.sh
new file mode 100755 (executable)
index 0000000..e2703e7
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/sh -e
+
+# WARNING: this script depends that two database nodes have IPs which and in x1 and x2
+# tr 12 21 does swap from currnet node number/ip to other one!
+
+test "`whoami`" != "postgres" && echo "$0 needs to be run as postgres user!" && exit 1
+
+here=`hostname -s`
+there=`hostname -s | tr 12 21`
+
+my_ip=`ip addr | grep 10.200.1. | sed 's/^.*\(10\.200\.1\.[0-9]*\)\/.*$/\1/'`
+other_ip=`echo $my_ip | tr 12 21`
+
+echo "Create slave on $here $my_ip using data from master on $there $other_ip"
+echo "[enter] to start"
+read
+
+#systemctl stop postgresql-9.6.service || true
+/usr/pgsql-9.6/bin/pg_ctl --pgdata=/var/lib/pgsql/9.6/data stop || true
+
+echo "select pg_start_backup('slave')" | ssh $there psql
+
+rsync -rav --delete --exclude pg_xlog $there:/var/lib/pgsql/9.6/data/ /var/lib/pgsql/9.6/data/
+
+echo "select pg_stop_backup()" | ssh $there psql
+
+test -e /var/lib/pgsql/9.6/data/recovery.done || ( echo "ERROR: no recovery.done !" && exit 1 )
+cat /var/lib/pgsql/9.6/data/recovery.done | sed -e s/$my_ip/$other_ip/ > /var/lib/pgsql/9.6/data/recovery.conf
+
+