码迷,mamicode.com
首页 > 其他好文 > 详细

Sersync实现触发式文件同步 替代inotify和rsync

时间:2016-02-14 00:24:06      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

Sersync实现触发式文件同步 替代inotify和rsync

 

参考文章

http://blog.chinaunix.net/uid-20639775-id-3011124.html

http://www.apelearn.com/bbs/forum.php?mod=viewthread&tid=10738&page=1&extra=#pid113148

http://zyan.cc/sersync/

mrsync https://sourceforge.net/projects/mrsync/

 


服务器端
1、安装rsync  两边都要做
yum install -y rsync
vi /etc/rsyncd.conf #服务器端
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
#address=192.168.0.10
max connections=10
timeout = 300
[webbak]
path=/tmp/www/wordpress
use chroot=true
read only=no
list=false
uid=root
gid=root
#auth users=test
#secrets file=/etc/rsyncd.passwd
hosts allow=192.168.1.109
#hosts deny = 0.0.0.0/32

echo "rsync --daemon" >>/etc/rc.local
#启动服务
rsync --daemon


2、安装Inotify-tools工具,实时触发rsync进行同步
两边机器都要做
安装inotify-tools
yum install -y make gcc gcc-c++
cd /mydata/download/
wget -c http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
mkdir -p /usr/local/inotify
./configure --prefix=/usr/local/inotify
make
make install
设置系统环境变量,添加软连接
echo "PATH=$PATH:/usr/local/inotify/bin" >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh #使设置立即生效
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf && ldconfig
ln -s /usr/local/inotify/include /usr/include/inotify
修改inotify默认参数(inotify默认内核参数值太小)
vi /etc/sysctl.conf
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
/sbin/sysctl -p

 


 

客户端

3、安装配置sersync
cd /download
wget -c http://files.cnblogs.com/files/MYSQLZOUQI/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
vi /usr/local/sersync/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="(.*)\wp-config.php"></exclude>
<!-- <exclude expression="(.*)\.gz"></exclude>-->
<!-- <exclude expression="^info/*"></exclude>-->
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>

<sersync>
<localpath watch="/tmp/www/wordpress">
<remote ip="192.168.1.106" name="webbak"/>
<!--<remote ip="192.168.1.12" name="tongbu"/>-->
<!--<remote ip="192.168.1.13" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/sersync_fail_log.sh" timeToExecute="60"/><!--default every 60min execute once-->
<crontab start="true" schedule="720"><!--720mins-->
<crontabfilter start="true">
<exclude expression="wp-config.php"></exclude>
<!--<exclude expression="*.gz"></exclude>-->
<!--<exclude expression="info/*"></exclude>-->
</crontabfilter>
</crontab>

</sersync>
</head>


将命令添加进/etc/rc.local,以后重启系统以后才能正常同步
vi /etc/rc.local
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

先测试一下rsync同步是否正常
rsync -av /tmp/yum_save_tx-2015-12-31-13-13x5anWV.yumtx 192.168.1.106::webbak

 

4、脚本监控sersync
因为有的时候sersync脚本会自动关掉,因此需要写一个脚本自动的去检测该进程是否存在,不存在就启动,脚本内容如下:
vi /root/check_sersync.sh
#!/bin/bash
#Purpose: Check sersync whether it is alive
#Author: steven
SERSYNC="/usr/local/sersync/sersync2"
CONF_FILE="/usr/local/sersync/confxml.xml"
STATUS=$(ps aux |grep ‘sersync2‘|grep -v ‘grep‘|wc -l)
if [ $STATUS -eq 0 ];
then
$SERSYNC -d -r -o $CONF_FILE &
else
exit 0;
fi

脚本写好以后,添加到计划任务中去
crontab -e
*/1 * * * * /bin/sh /root/check_sersync.sh > /dev/null 2>&1

Sersync实现触发式文件同步 替代inotify和rsync

标签:

原文地址:http://www.cnblogs.com/MYSQLZOUQI/p/5188407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!