分类: LINUX
2010-03-24 11:34:04
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
- #!/bin/sh
- 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 {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 {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 {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 {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