vi /etc/rsyncd.conf uid = root gid = root use chroot = no max connections = 20 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [data0_htdocs] path = /data0/htdocs/ ignore errors read only = no hosts allow = 192.168.146.0/24 hosts deny = 0.0.0.0/32 [php_etc] path = /usr/local/webserver/php/etc/ ignore errors read only = no hosts allow = 192.168.146.0/24 hosts deny = 0.0.0.0/32 [nginx_conf] path = /usr/local/webserver/nginx/conf/ ignore errors read only = no hosts allow = 192.168.146.0/24 hosts deny = 0.0.0.0/32
/usr/bin/rsync --daemon
/usr/bin/nohup /bin/sh /usr/local/webserver/failover/failover.sh 2>&1 > /dev/null &
ps -ef | grep failover.sh
root 15428 1 0 Nov17 ? 00:00:03 /bin/sh /usr/local/webserver/failover/failover.sh root 20123 6878 0 16:16 pts/2 00:00:00 grep failover.sh
kill -9 15428
#!/bin/bash LANG=C date=$(date -d "today" +"%Y-%m-%d %H:%M:%S") #---------------配置信息(開始)--------------- #類型:主機設為master,備機設為slave type="master" #主機、備機切換日志路徑 logfile="/var/log/failover.log" #MySQL可執行文件地址,例如/usr/local/mysql/bin/mysql;MySQL用戶名;密碼;端口 mysql_bin="/usr/local/webserver/mysql/bin/mysql" mysql_username="root" mysql_password="123456" mysql_port="3306" #內網網關 gateway_eth0="192.168.146.1" #主機內網真實IP rip_eth0_master="192.168.146.213" #備機內網真實IP rip_eth0_slave="192.168.146.215" #主機、備機內網共用的虛擬IP vip_eth0_share="192.168.113.214" #外網網關 gateway_eth1="72.249.146.193" #主機外網真實IP rip_eth1_master="72.249.146.213" #備機外網真實IP rip_eth1_slave="72.249.146.215" #主機、備機外網共用的虛擬IP vip_eth1_share="72.249.146.214" #---------------配置信息(結束)--------------- #綁定內、外網虛擬IP function_bind_vip() { /sbin/ifconfig eth0:vip ${vip_eth0_share} broadcast ${vip_eth0_share} netmask 255.255.255.255 up /sbin/route add -host ${vip_eth0_share} dev eth0:vip /sbin/ifconfig eth1:vip ${vip_eth1_share} broadcast ${vip_eth1_share} netmask 255.255.255.255 up /sbin/route add -host ${vip_eth1_share} dev eth1:vip /usr/local/webserver/php/sbin/php-fpm reload kill -USR1 `cat /usr/local/webserver/nginx/logs/nginx.pid` /sbin/service crond start } #解除內、外網虛擬IP function_remove_vip() { /sbin/ifconfig eth0:vip ${vip_eth0_share} broadcast ${vip_eth0_share} netmask 255.255.255.255 down /sbin/ifconfig eth1:vip ${vip_eth1_share} broadcast ${vip_eth1_share} netmask 255.255.255.255 down /sbin/service crond stop } #主機向備機推送文件的函數 function_rsync_master_to_slave() { /usr/bin/rsync -zrtuog /data0/htdocs/ ${rip_eth0_slave}::data0_htdocs/ > /dev/null 2>&1 /usr/bin/rsync -zrtuog /usr/local/webserver/php/etc/ ${rip_eth0_slave}::php_etc/ > /dev/null 2>&1 /usr/bin/rsync -zrtuog /usr/local/webserver/nginx/conf/ ${rip_eth0_slave}::nginx_conf/ > /dev/null 2>&1 } #備機向主機推送文件的函數 function_rsync_slave_to_master() { /usr/bin/rsync -zrtuog /data0/htdocs/ ${rip_eth0_master}::data0_htdocs/ > /dev/null 2>&1 /usr/bin/rsync -zrtuog /usr/local/webserver/php/etc/ ${rip_eth0_master}::php_etc/ > /dev/null 2>&1 /usr/bin/rsync -zrtuog /usr/local/webserver/nginx/conf/ ${rip_eth0_master}::nginx_conf/ > /dev/null 2>&1 } #虛擬IP ARPing function_vip_arping() { /sbin/arping -I eth0 -c 3 -s ${vip_eth0_share} ${gateway_eth0} > /dev/null 2>&1 /sbin/arping -I eth1 -c 3 -s ${vip_eth1_share} ${gateway_eth1} > /dev/null 2>&1 } while true do #用HTTP協議檢查虛擬IP if (curl -m 30 -G http://${vip_eth1_share}/ > /dev/null 2>&1) && (${mysql_bin} -u"${mysql_username}" -p"${mysql_password}" -P"${mysql_port}" -h"${vip_eth0_share}" -e"show slave status\G" > /dev/null 2>&1) then #取得與內網VIP綁定的服務器內網IP eth0_active_server=$(${mysql_bin} -u"${mysql_username}" -p"${mysql_password}" -P"${mysql_port}" -h"${vip_eth0_share}" -e"show slave status\G" | grep "Master_Host" | awk -F ': ' '{printf $2}') #如果內網VIP=主機內網IP(主機MySQL中的Master_Host顯示的是備機的域名或IP),且本機為主機 if [ "${eth0_active_server}" = "${rip_eth0_slave}" ] && [ "${type}" = "master" ] then function_rsync_master_to_slave function_vip_arping #如果內網VIP=備機內網IP(備機MySQL中的Master_Host顯示的是主機的域名或IP) elif [ "${eth0_active_server}" = "${rip_eth0_master}" ] then if (curl -m 30 -G http://${rip_eth1_master}/ > /dev/null 2>&1) && (${mysql_bin} -u"${mysql_username}" -p"${mysql_password}" -P"${mysql_port}" -h"${rip_eth0_master}" -e"show slave status\G" | grep "Seconds_Behind_Master: 0" > /dev/null 2>&1) then #如果主機能夠訪問,數據庫同步無延遲,且本機就是主機,那麼由本機綁定虛擬IP if [ "${type}" = "master" ] then #如果本機為主機 function_bind_vip function_vip_arping echo "${date} 主機已綁定虛擬IP!(Type:1)" >> ${logfile} else #如果本機為備機 function_remove_vip echo "${date} 備機已去除虛擬IP!(Type:2)" >> ${logfile} fi else if [ "${type}" = "slave" ] then #如果本機為備機 function_rsync_slave_to_master function_vip_arping fi fi fi else #虛擬IP無法訪問時,判斷主機能否訪問 if (curl -m 30 -G http://${rip_eth1_master}/ > /dev/null 2>&1) && (${mysql_bin} -u"${mysql_username}" -p"${mysql_password}" -P"${mysql_port}" -h"${rip_eth0_master}" -e"show slave status\G" > /dev/null 2>&1) then #如果主機能夠訪問,且本機就是主機,那麼由本機綁定虛擬IP if [ "${type}" = "master" ] then function_bind_vip function_vip_arping echo "${date} 主機已綁定虛擬IP!(Type:3)" >> ${logfile} else function_remove_vip echo "${date} 備機已去除虛擬IP!(Type:4)" >> ${logfile} fi elif (curl -m 30 -G http://${rip_eth1_slave}/ > /dev/null 2>&1) && (${mysql_bin} -u"${mysql_username}" -p"${mysql_password}" -P"${mysql_port}" -h"${rip_eth0_slave}" -e"show slave status\G" > /dev/null 2>&1) then #如果主機不能訪問而備機能夠訪問,且本機就是備機,那麼由備機綁定虛擬IP if [ "${type}" = "slave" ] then function_bind_vip function_vip_arping echo "${date} 備機已綁定虛擬IP!(Type:5)" >> ${logfile} else function_remove_vip echo "${date} 主機已去除虛擬IP!(Type:6)" >> ${logfile} fi else echo "${date} 主機、備機全部無法訪問!(Type:7)" >> ${logfile} fi fi #每次循環暫停20秒(即間隔20秒檢測一次) sleep 20 done