PG集群实现自动切换(非repmgrd模式)

#!/bin/bash

whichday=`date "+%w"`
v_param=$#

#判断参数个数
if [ $v_param -ge 3 ];then
        echo "Invalid argument!! E.g sh pg-cluster-switchover.sh 'paas|bi' vlnx191001.xxx.cn (pg-slave-node)"
        exit 1
fi

v_wthich_type=$1
v_slave_hostname=$2
v_valid=''
v_short_hostname=''
v_short_hostname=`echo $v_slave_hostname | awk -F '.' '{print $1}'`
v_p_s=''
v_p_s=`echo $v_slave_hostname | awk -F '0' '{print $2}'`

        echo Server: $v_slave_hostname

        v_primary_hostname=''
        v_version=''
        log_path=''

#获取Postgres服务版本
        v_version_str=`psql -h$v_slave_hostname -Uzhaowz -d postgres -c "select version()" |grep "PostgreSQL 9"`


        if [ -n "$v_version_str" ];then
                v_version=9.6
                log_path=pg_log
        fi

        if [ -z "$v_version" ];then
               v_version_str=`psql -h$v_slave_hostname -Uzhaowz -d postgres -c "select version()" |grep "PostgreSQL 10"`
                if [ -n "$v_version_str" ];then
                        v_version=10
                        log_path=log
                fi
        fi


#校验服务器域名地址和业务机器类别是否正确
        if [ "${v_wthich_type}" == 'paas' ];then
                if [ "${v_version}" == '9.6' ];then
                        v_valid=`ssh $v_slave_hostname /usr/pgsql-$v_version/bin/repmgr -f /etc/repmgr/$v_version/repmgr.conf cluster show | grep master | grep $v_short_hostname`
                elif [ "${v_version}" == '10' ];then
                        v_valid=`ssh $v_slave_hostname /usr/pgsql-$v_version/bin/repmgr -f /etc/repmgr/$v_version/repmgr.conf cluster show | grep primary | grep $v_short_hostname`
                fi

                        if [ -n "$v_valid" ];then
                                echo "Invalid server $v_slave_hostname"
                                exit 1
                        else
                                if [ "${v_p_s}" == '191' ];then
                                        v_primary_hostname=`echo $v_slave_hostname | sed 's/vlnx191/vlnx192/g'`
                                elif [ "${v_p_s}" == '192' ];then
                                        v_primary_hostname=`echo $v_slave_hostname | sed 's/vlnx192/vlnx191/g'`
                                else
                                        echo "Business machine category error!"
                                        exit 1
                                fi
                        fi
        elif [ "${v_wthich_type}" == 'bi' ];then
                if [ "${v_version}" == '9.6' ];then
                        v_valid=`ssh $v_slave_hostname /usr/pgsql-$v_version/bin/repmgr -f /etc/repmgr/$v_version/repmgr.conf cluster show | grep master | grep $v_short_hostname`
                elif [ "${v_version}" == '10' ];then
                        v_valid=`ssh $v_slave_hostname /usr/pgsql-$v_version/bin/repmgr -f /etc/repmgr/$v_version/repmgr.conf cluster show | grep primary | grep $v_short_hostname`
                fi

                        if [ -n "$v_valid" ];then
                                echo "Invalid server $v_slave_hostname"
                                exit 1
                        else
                                if [ "${v_p_s}" == '193' ];then
                                        v_primary_hostname=`echo $v_slave_hostname | sed 's/vlnx193/vlnx194/g'`
                                elif [ "${v_p_s}" == '194' ];then
                                        v_primary_hostname=`echo $v_slave_hostname | sed 's/vlnx194/vlnx193/g'`
                                else
                                        echo "Business machine category error!"
                                        exit 1
                                fi

                        fi
        else
                echo "Business machine category error!"
                exit 1
        fi


#获取当前周几
        if [ "${whichday}" == '1' ];then
                filename=postgresql-Mon.log
        elif [ "${whichday}" == '2' ];then
                filename=postgresql-Tue.log
        elif [ "${whichday}" == '3' ];then
                filename=postgresql-Wed.log
        elif [ "${whichday}" == '4' ];then
                filename=postgresql-Thu.log
        elif [ "${whichday}" == '5' ];then
                filename=postgresql-Fri.log
        elif [ "${whichday}" == '6' ];then
                filename=postgresql-Sat.log
        elif [ "${whichday}" == '0' ];then
                filename=postgresql-Sun.log
        fi

#Switchover
        v_switchover_succ=''
        pv_vip=''
        if [ "${v_version}" == '10' ];then
                ssh $v_slave_hostname " sudo su - postgres -c 'cat /dev/null > /var/log/repmgr/repmgr.log'"
                ssh $v_primary_hostname " sudo su - postgres -c 'cat /dev/null > /var/log/repmgr/repmgr.log'"

                pv_vip=`ssh $v_primary_hostname sudo ifconfig eth3 | grep inet | awk '{print $2}' | cut -d':' -f2`

                ssh $v_slave_hostname "sudo su - postgres -c '/usr/pgsql-10/bin/repmgr -t --log-to-file -f /etc/repmgr/10/repmgr.conf standby switchover'"
                v_switchover_succ=`ssh $v_slave_hostname sudo cat /var/log/repmgr/repmgr.log | grep 'STANDBY SWITCHOVER has completed successfully'`
                ssh $v_slave_hostname /usr/pgsql-10/bin/repmgr -f /etc/repmgr/10/repmgr.conf cluster show

        elif [ "${v_version}" == '9.6' ];then
                ssh $v_slave_hostname " sudo su - postgres -c 'cat /dev/null > /var/log/repmgr/repmgr-9.6.log'"
                ssh $v_primary_hostname " sudo su - postgres -c 'cat /dev/null > /var/log/repmgr/repmgr-9.6.log'"

                pv_vip=`ssh $v_primary_hostname sudo ifconfig eth3 | grep inet | awk '{print $2}' | cut -d':' -f2`

                ssh $v_slave_hostname "sudo su - postgres -c '/usr/pgsql-9.6/bin/repmgr -t --log-to-file -f /etc/repmgr/9.6/repmgr.conf standby switchover'"
                v_switchover_succ=`ssh $v_slave_hostname sudo cat /var/log/repmgr/repmgr-9.6.log | grep 'switchover was successful'`
                ssh $v_slave_hostname /usr/pgsql-9.6/bin/repmgr -f /etc/repmgr/9.6/repmgr.conf cluster show
        fi

        if [ -z "$v_switchover_succ" ];then
                if [ "${v_version}" == '10' ];then
                        echo `ssh $v_slave_hostname sudo cat /var/log/repmgr/repmgr.log `
                elif [ "${v_version}" == '9.6' ];then
                        echo `ssh $v_slave_hostname sudo cat /var/log/repmgr/repmgr-9.6.log`
                fi
                exit
        fi

        if [ -n "$v_switchover_succ" ];then
                v_true_false=''
                sv_vip=''
                s_vip=''
                v_networkmanager=''
                v_ethx=''

                pv_device=''
                sv_device=''
                pv_device=`ssh $v_primary_hostname sudo nmcli connection show  'Wired\ connection\ 3' | grep 'connection.interface-name:' | awk '{print $2}'`
                sv_device=`ssh $v_slave_hostname sudo nmcli connection show  'Wired\ connection\ 3' | grep 'connection.interface-name:' | awk '{print $2}'`
                if [ "${pv_device}" != 'eth3' ];then
                        pv_device=`ssh $v_primary_hostname sudo nmcli connection show  'Wired\ connection\ 3' | grep 'DEVICES:' | awk '{print $2}'`
                                if [ "${pv_device}" != 'eth3' ];then
                                        pv_device='eth3'
                                fi
                fi

                if [ "${sv_device}" != 'eth3' ];then
                        sv_device=`ssh $v_slave_hostname sudo nmcli connection show  'Wired\ connection\ 3' | grep 'DEVICES:' | awk '{print $2}'`
                                if [ "${sv_device}" != 'eth3' ];then
                                        sv_device='eth3'
                                fi
                fi

                pv_device_name='Wired\ connection\ 3'
                sv_device_name='Wired\ connection\ 3'
                p_dev_name='Wired connection 3'
                s_dev_name='Wired connection 3'

#主库禁止eth3自动激活,关闭VIP           
                if [ -n "$pv_device" ];then
                        ssh $v_primary_hostname "sudo nmcli connection modify '$p_dev_name' connection.autoconnect no"

                        v_true_false=`ssh $v_primary_hostname sudo nmcli con show 'Wired\ connection\ 3' |grep 'connection.autoconnect:' | grep no`
                        echo 1234

                        if [ -n "$v_true_false" ];then
                                echo 9999
                                ssh $v_primary_hostname "sudo nmcli connection down '$p_dev_name'"
                        fi

                fi

#从库激活VIP(临时)
                if [ -n "$sv_device" ];then
                        ssh $v_slave_hostname "sudo nmcli connection modify '$s_dev_name' connection.autoconnect yes"
                        sv_vip=`ssh $v_slave_hostname sudo nmcli con show  'Wired\ connection\ 3' | grep 'ipv4.addresses:' | awk '{print $2}'`
                        s_vip=`ssh $v_slave_hostname sudo nmcli con show  'Wired\ connection\ 3' | grep 'ipv4.addresses:' | awk '{print $2}' | awk -F '/' '{print $1}'`

                        ssh $v_slave_hostname "sudo nmcli connection up '$s_dev_name'"
                        echo 2345

                        v_ethx=`ssh $v_slave_hostname sudo ifconfig | grep $sv_device | awk -F ':' '{print $1}'`

                        if [ -n "$v_ethx" ];then
                                echo 3456
                                        ssh $v_slave_hostname "sudo nmcli connection modify '$s_dev_name' ipv4.addresses $sv_vip  ipv4.method manual"
                        else
                                if [ -n "$s_vip" ];then
                                echo 4567
                                        ssh $v_slave_hostname sudo ifconfig $sv_device up
                                        ssh $v_slave_hostname sudo ifconfig $sv_device  $s_vip  netmask 255.255.0.0 up
                                else
                                echo 5678       
                                        ssh $v_slave_hostname sudo ifconfig $sv_device up
                                        ssh $v_slave_hostname sudo ifconfig $sv_device  $pv_vip  netmask 255.255.0.0 up
                                fi
                        fi

                        v_networkmanager=`ssh $v_slave_hostname sudo systemctl status NetworkManager |grep 'Active: active (running)'`
                        if [ -z "$v_networkmanager" ];then
                                ssh $v_slave_hostname sudo systemctl start NetworkManager
                        fi
                fi
        fi

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值