说明:该功能服务端安装sersync2,客户端安装rsync,原理就是服务端主动推送设定目录下的所有更新的文件到各个客户端rsync接收。
rsync大家都知道,是Linux自带的数据同步工具,而sersync2是google大神的开源项目http://code.google.com/p/sersync/
下面给出具体的实现步骤,实现的详细原理大家可以去上面的开源网址,上面说的很详细
- 客户端配置,首先系统安装rsync工具,
[root@yo57 ~]# vi /etc/rsyncd.conf uid=www gid=www max connections=36000 use chroot=no log file=/var/log/rsyncd.log pid file=/var/run/rsyncd.pid lock file=/var/run/rsyncd.lock [yowebtongbu] path=/Data/code/adserver comment = yo web files ignore errors = yes read only = no hosts allow = 192.168.0.0/24 hosts deny = * [root@yo57 ~]# /usr/bin/rsync --daemon --config=/etc/rsyncd.conf [root@yo57 ~]# ps -ef|grep rsyn root 1070 29923 0 17:04 pts/4 00:00:00 grep rsyn root 32069 1 0 16:54 ? 00:00:00 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf - 写入开机启动项
[root@yo57 ~]# vi /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local ulimit -SHn 51200 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d /usr/local/nginx/sbin/nginx /etc/init.d/php_fpm start /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
- 服务器端
[root@10local]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz[root@10local]# cd sersync2.5.4_64[root@10 sersync2.5.4_64]# lsconfxml.xml sersync2修改这一段即可[root@10 sersync2.5.4_64]# vi confxml.xml
<localpathwatch="/Data/code/adserver"><remote ip="192.168.0.27"name="yowebtongbu"/><!--<remote ip="192.168.8.39"name="tongbu"/>--><!--<remote ip="192.168.8.40"name="tongbu"/>-->进行一次完整同步</localpath>
[root@10 sersync2.5.4_64]# ./sersync2 -r写入脚本并放入开机启动项
[root@10 sersync2.5.4_64]# cat /usr/local/sbin/sersync.sh#!/bin/bashPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexportPATHSDATH="/usr/local/sersync2.5.4_64"SSTART="./sersync2 -r -d"SPID=`ps-ef|grep'sersync2'|grep-v'grep'|awk'{print $2}'`function_start(){echo-en" 33[32;49;1mStarting sersync2...... "echo-en" 33[39;49;0m"if[ -t ${SPID} ];thencd${SDATH}${SSTART} >/dev/null2>&1printf"Serync2 is the successful start! "elseprintf"Sersync2 is runing! "exit1fi}function_stop(){echo-en" 33[32;49;1mStoping sersync2...... "echo-en" 33[39;49;0m"if[ -t ${SPID} ];thenprintf"Sersync2 program is not runing! "elsekill${SPID}printf"Sersync2 program is stoped "fi}function_restart(){echo-en" 33[32;49;1mRestart sersync2...... "echo-en" 33[39;49;0m"if[ -t ${SPID} ];thencd${SDATH}${SSTART} >/dev/null2>&1elsekill${SPID}sleep1cd${SDATH}${SSTART} >/dev/null2>&1fiprintf"Sersync2 is the successful restart! "}function_kill(){killall sersync2}function_status(){if!ps-ef|grep'sersync2'|grep-v'grep'>/dev/null2>&1thenprintf"Sersync2 is down!!! "elseprintf"Sersync2 is running now! "fi}if["$1"="start"];thenfunction_startelif["$1"="stop"];thenfunction_stopelif["$1"="restart"];thenfunction_restartelif["$1"="kill"];thenfunction_killelif["$1"="status"];thenfunction_statuselseecho-en" 33[32;49;1m Usage: sersync2 {start|stop|restart|kill|status} "echo-en" 33[39;49;0m"fi
[root@10 sersync2.5.4_64]# vi /etc/rc.local#!/bin/sh## This script will be executed *after* all the other init scripts.# You can put your own initialization stuff in here if you don't# want to do the full Sys V style init stuff.touch/var/lock/subsys/local#/etc/init.d/lemp start/usr/local/nginx/sbin/nginx/etc/init.d/php_fpmstart/usr/local/zabbix/sbin/zabbix_agentd/usr/local/sbin/sersync.sh start
整个实现就这么简单,以后主服务器上面/Data/code/adserver目录下有新建、删除、修改文件或文件夹(包括下层递归)的的数据会自动推送到下面的各个服务端对应目录下,如果临时不需要该功能,kill掉服务端即可,操作完以后在手动开启服务端(此时客户端不用动)