Rsync+Inotify-tools与Rsync+sersync这两种架构有什么区别
1.Rsync+Inotify-tools
(1):Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;
(2):rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。
2.Rsync+sersync
(1):sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;
(2):rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。
小结:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。
一、rsync的配置方法。
http://www.cnblogs.com/lei0213/p/8595112.html(这里面除了inotify其它的都需要安装验证,把inotify换成了sersync)
二、准备环境
1、查看服务器内核是否支持inotify
ll /proc/sys/fs/inotify
2、修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值:
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events= 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches= 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances= 128
修改参数:
#sysctl -w fs.inotify.max_queued_events="99999999"
#sysctl -w fs.inotify.max_user_watches="99999999"
#sysctl -w fs.inotify.max_user_instances="65535"
#vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
:wq #保存退出
参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event QueueOverflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home/Sync-type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/Sync为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值
三、安装sersync
1、下载sersync安装包
#cd /root
wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
#tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
#mv GNU-Linux-x86 /usr/local/sersync
其它下载地方http://blog.csdn.net/orangleliu/article/details/46698795
2、进入配置文件目录,留一份备份。
#cd /usr/local/sersync #cp confxml.xml confxml.xml-bak #vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data/www">
<remote ip="192.168.1.239" name="msone"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsynclsl" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="true" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
配置说明:
localpath watch="/data/www/":#源服务器同步目录 name="msone": #目标服务器rsync同步目录模块名称 users="rsynclsl": #目标服务器rsync同步用户名 passwordfile="/etc/rsync.password": #目标服务器rsync同步用户的密码在源服务器的存放路径 remote ip="192.168.1.239": #目标服务器ip,每行一个 failLog path="/tmp/rsync_fail_log.sh" #脚本运行失败日志记录 start="true" #设置为true,每隔600分钟执行一次全盘同步 <auth start="true" #设置true。
3、配置完配置文件的时候千万记得,先不要执行下面的脚本,首先执行下面的命令,验证同步。
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
如果没有出现下面这个文件夹,而且指定目录下的数据也没有同步到对端服务器,下面的配置也就不复存在。
4、如果你上面的配置和验证都没问题,可以下面的配置了。设置开机自启动
#vi /etc/rc.d/rc.local /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #设置开机自动运行脚本 # chmod +x /etc/rc.d/rc.local
5、添加脚本监控sersync是否正常
#cd /root #touch check_sersync.sh #chmod 755 check.sersync.sh #vim check_sersync.sh
#!/bin/sh sersync="/usr/local/sersync/sersync2" confxml="/usr/local/sersync/confxml.xml" status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l) if [ $status -eq 0 ]; then $sersync -d -r -o $confxml & else exit 0; fi