while true #!/bin/bash while true do let c++ echo $c ip1=1.1.1.1
ip2=1.1.1.2 ip3=1.1.1.3 vip=1.1.1.77 a=`ip a | grep -oP '(\d{1,3}\.){3}\d{1,3}' | awk -F "." '{print $4}' | tr '\n' ' ' ` for var in ${a[@]} do if [ $var -eq 77 ];then if [ $c -eq 2 ];then echo "The machine is now mysql main write server" a1=`mysql -ud -pd -h $vip -e "show master status" | sed -n '/mysql/p'|awk '{print $1}'` a2=`mysql -ud -pd -h $vip -e "show master status" | sed -n '/mysql/p'|awk '{print $2}'` echo $a1 $a2 for i in $ip1 $ip2 $ip3 do mysql -ud -pd -h$i -e "slave stop; change master to master_host='$vip',master_user='d',master_password='d', master_log_file='$a1',master_log_pos=$a2; slave start;" done fi else cc=`echo $a | awk '/77/{print "yes"}' ` if [ "$cc" != "yes" ] ;then c=0 echo "Native backup machine" fi fi done sleep 3 done