cleanup docs, extract scripts for postgresql
[edozvola] / pg-scripts / create-pgsql-slave.sh
1 #!/bin/sh -e
2
3 # WARNING: this script depends that two database nodes have IPs which and in x1 and x2
4 # tr 12 21 does swap from currnet node number/ip to other one!
5
6 test "`whoami`" != "postgres" && echo "$0 needs to be run as postgres user!" && exit 1
7
8 here=`hostname -s`
9 there=`hostname -s | tr 12 21`
10
11 my_ip=`ip addr | grep 10.200.1. | sed 's/^.*\(10\.200\.1\.[0-9]*\)\/.*$/\1/'`
12 other_ip=`echo $my_ip | tr 12 21`
13
14 echo "Create slave on $here $my_ip using data from master on $there $other_ip"
15 echo "[enter] to start"
16 read
17
18 #systemctl stop postgresql-9.6.service || true
19 /usr/pgsql-9.6/bin/pg_ctl --pgdata=/var/lib/pgsql/9.6/data stop || true
20
21 echo "select pg_start_backup('slave')" | ssh $there psql
22
23 rsync -rav --delete --exclude pg_xlog $there:/var/lib/pgsql/9.6/data/ /var/lib/pgsql/9.6/data/
24
25 echo "select pg_stop_backup()" | ssh $there psql
26
27 test -e /var/lib/pgsql/9.6/data/recovery.done || ( echo "ERROR: no recovery.done !" && exit 1 )
28 cat /var/lib/pgsql/9.6/data/recovery.done | sed -e s/$my_ip/$other_ip/ > /var/lib/pgsql/9.6/data/recovery.conf
29
30