postgresql 9.1 基于 async stream 的 master/salve 快速切换脚本

vm: Oracle VM VirtualBox 5.2.4 r119785 (Qt5.6.2)
os:Debian 8.10
postgresql: 9.1.24

机器情况
nodea:192.168.56.100/192.168.165.100 目前是master
nodeb:192.168.56.200/192.168.165.200 目前是slave

vip:192.168.56.101/192.168.165.101

本次操作是将nodeb提升为master后,再次提升nodea为master,分为两个步骤操作。

step 1

nodea 192.168.56.100 目前是master,需要变更为slave
nodeb 192.168.56.200 目前是slave,需要变更为master

1、192.168.56.100 上 root 执行脚本

#!/bin/bash

echo "########## postgresql status"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl status -D /var/lib/postgresql/9.1/main"

echo "########## postgresql stop"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl stop -m fast -D /var/lib/postgresql/9.1/main"
sleep 5

echo "########## os copy pg_hba.conf.read"
su - postgres -c "rm -f /etc/postgresql/9.1/main/pg_hba.conf"
su - postgres -c "cp /etc/postgresql/9.1/main/pg_hba.conf.read /etc/postgresql/9.1/main/pg_hba.conf"

echo "########## postgresql start"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl start -D /var/lib/postgresql/9.1/main -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf""
sleep 5

echo "########## postgresql stop"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl stop -m fast -D /var/lib/postgresql/9.1/main"
sleep 5

echo "########## postgresql mv recovery.done"
su - postgres -c "mv /var/lib/postgresql/9.1/main/recovery.done /var/lib/postgresql/9.1/main/recovery.conf"


echo "########## os remove vip"
ip addr del 192.168.56.101/24 dev bond0 label bond0:1
ip addr del 192.168.165.101/24 dev bond1 label bond1:1

echo "########## os remove phy ip"
#ip addr del 192.168.56.100/24 dev bond0
#ip addr del 192.168.165.100/24 dev bond1

echo "########## end"

2、192.168.56.200 上root 执行脚本

#!/bin/bash

echo "########## postgresql status"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl status -D /var/lib/postgresql/9.1/main"

echo "########## postgresql promote"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl promote -D /var/lib/postgresql/9.1/main"
sleep 5

echo "########## postgresql stop"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl stop -m fast -D /var/lib/postgresql/9.1/main"
sleep 5

echo "########## copy pg_hba.conf.prod"
su - postgres -c "rm -f /etc/postgresql/9.1/main/pg_hba.conf"
su - postgres -c "cp /etc/postgresql/9.1/main/pg_hba.conf.prod /etc/postgresql/9.1/main/pg_hba.conf"

echo "########## os add vip"
ip addr add 192.168.56.101/24 brd 192.168.56.255 dev bond0 label bond0:1
ip addr add 192.168.165.101/24 brd 192.168.165.255 dev bond1 label bond1:1

echo "########## os add route"
route add -net 192.168.0.0/16 gw 192.168.56.254

echo "########## os arping"
arping -q -A -c 1 -I bond0:1 192.168.56.101
arping -q -A -c 1 -I bond1:1 192.168.165.101

echo "########## postgresql start"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl start -D /var/lib/postgresql/9.1/main -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf""
sleep 5

echo "########## end"

3、192.168.56.100 上操作
获取生成的 timeline 文件,成为slave

$ /usr/lib/postgresql/9.1/bin/pg_ctl start -D /var/lib/postgresql/9.1/main -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf"

step 2

nodea 192.168.56.100 目前是slave,需要变更为master
nodeb 192.168.56.200 目前是master,需要变更为slave

1、192.168.56.200 上 root 执行脚本

#!/bin/bash

echo "########## postgresql status"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl status -D /var/lib/postgresql/9.1/main"

echo "########## postgresql stop"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl stop -m fast -D /var/lib/postgresql/9.1/main"
sleep 5

echo "########## os copy pg_hba.conf.read"
su - postgres -c "rm -f /etc/postgresql/9.1/main/pg_hba.conf"
su - postgres -c "cp /etc/postgresql/9.1/main/pg_hba.conf.read /etc/postgresql/9.1/main/pg_hba.conf"

echo "########## postgresql start"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl start -D /var/lib/postgresql/9.1/main -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf""
sleep 5

echo "########## postgresql stop"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl stop -m fast -D /var/lib/postgresql/9.1/main"
sleep 5

echo "########## postgresql mv recovery.done"
su - postgres -c "mv /var/lib/postgresql/9.1/main/recovery.done /var/lib/postgresql/9.1/main/recovery.conf"


echo "########## os remove vip"
ip addr del 192.168.56.101/24 dev bond0 label bond0:1
ip addr del 192.168.165.101/24 dev bond1 label bond1:1

echo "########## os remove phy ip"
#ip addr del 192.168.56.200/24 dev bond0
#ip addr del 192.168.165.200/24 dev bond1

echo "########## end"

2、192.168.56.100 上root 执行脚本

#!/bin/bash

echo "########## postgresql status"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl status -D /var/lib/postgresql/9.1/main"

echo "########## postgresql promote"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl promote -D /var/lib/postgresql/9.1/main"
sleep 5

echo "########## postgresql stop"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl stop -m fast -D /var/lib/postgresql/9.1/main"
sleep 5

echo "########## copy pg_hba.conf.prod"
su - postgres -c "rm -f /etc/postgresql/9.1/main/pg_hba.conf"
su - postgres -c "cp /etc/postgresql/9.1/main/pg_hba.conf.prod /etc/postgresql/9.1/main/pg_hba.conf"

echo "########## os add vip"
ip addr add 192.168.56.101/24 brd 192.168.56.255 dev bond0 label bond0:1
ip addr add 192.168.165.101/24 brd 192.168.165.255 dev bond1 label bond1:1

echo "########## os add route"
route add -net 192.168.0.0/16 gw 192.168.56.254

echo "########## os arping"
arping -q -A -c 1 -I bond0:1 192.168.56.101
arping -q -A -c 1 -I bond1:1 192.168.165.101

echo "########## postgresql start"
su - postgres -c "/usr/lib/postgresql/9.1/bin/pg_ctl start -D /var/lib/postgresql/9.1/main -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf""
sleep 5

echo "########## end"

3、192.168.56.200 上操作
传输生成的 timeline 文件,再次slave

$ /usr/lib/postgresql/9.1/bin/pg_ctl start -D /var/lib/postgresql/9.1/main -o "-c config_file=/etc/postgresql/9.1/main/postgresql.conf"
原文地址:https://www.cnblogs.com/ctypyb2002/p/9793048.html