Sersync-or-Inotify_centos

...

http://www.sky.la/2011/02/732.html

 

项目地址:http://code.google.com/p/sersync/

环境:centos 5.2 32bit , rsync  version 2.6.8 ,sersync2.5beta3_32bit_binary

服务器:3台,分别标识为A,B,C,其中A为客户端,B和C为服务端,sersync安装在A机,监视文件变化,实时推送到B,C服务器上。

A机 172.16.1.179

B机 172.16.1.190

C机  172.16.1.206

配置说明:

1,A、B、C分别开启rsync,以守护进程的方式。

2,A机上安装sersync。

3,A机的sersync是以rsync能正常工作为前提,请测试好了再安装。

安装步骤:

1、分别为A,B,C三机安装 rsync

查看是否装了rsync包
[root@hxqm tongbu]# rpm -q rsync
rsync-2.6.8-3.1
如果没有安装就用rpm 或yum装上。

# yum -y install rsync

2、新建配置文件/etc/rsyncd.conf,然后编辑/etc/rsyncd.conf,添加一下内容到文件里:

motd file = /etc/rsyncd.motd
uid=root
gid=root
max connections = 36000
use chroot=no
log file=/var/log/rsyncd.log
log format =  %t %a %m %f %b
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
timeout = 300
[tongbu]   #定义了一个tongbu的模块
path=/data/htdocs
list=yes
comment = rsync website
ignore errors = yes
read only = no
hosts allow = 172.16.1.179 172.16.1.190 172.16.1.206
hosts deny = *
auth users = rsyncuser #开启认证模块
secrets file = /etc/rsyncd.secrets

3、在server配置了一个名为"[tongbu]“的模块,需要同步的目录为/data/htdocs ,访问用的用户名为test

4、新建/etc/rsyncd.secrets,文件里的内容格式是:
rsyncuser:rsyncuser
注意:第一个字段是用户名,第二个字段是密码,他们之间用":"分开。

在A上只是一个单纯的写了密码的文件

# cat /etc/rsyncd.secrets

rsyncuser

然后都chmod 600 /etc/rsyncd.secrets
5、开启rsync daemon格式
rsync -daemon -config=/etc/rsyncd.conf
检查rsync是否起来:

lsof -i :873

COMMAND PID USER   FD   TYPE DEVICE SIZE NODE NAME
rsync   2625 root    4u IPv6   5662       TCP *:rsync (LISTEN)
rsync   2625 root    5u IPv4   5663       TCP *:rsync (LISTEN)

表示已经起来,返回空,表示没有起来

确保防火墙能使 tcp的873端口通过

如果不行请service iptables stop 关闭iptables
6、把rsync启动加到启动项里
echo “rsync -daemon -config=/etc/rsyncd.conf" >>etc/rc.local

测试:

在A机上推:

rsync -vzrtopg -progress -password-file=/etc/rsyncd.secrets /opt/tongbu    rsyncuser@172.16.1.190::tongbu

在A机上拉:

rsync -vzrtopg -progress root@172.16.1.190::tongbu /opt/tongbu -password-file=/etc/rsyncd.secrets

如果需要重启rsync,执行命令 service xinetd restart.

5、安装sersync

wget http://code.google.com/p/sersync/downloads/detail?name=sersync2.5beta3_32bit_binary.tar.gz

如果使用linux 系统,一般情况下,不需要编译,直接使用bin目录下的可执行文件和xml配置文件即可。 src目录下放的是源码

lib目录下是需要的静态库

bin目录是最后生成的二进制文件

在sersync 目录下执行make命令,就会将生成的2进制文件放入bin目录下。

由于大部分库都是静态编译的,所以在被监控服务器上,修改好配置文件后,直接执行./sersync2.1 即可,只需修改 sersync标签下的内容如下即可

<?xml version="1.0″ encoding="ISO-8859-1″?>
<head version="2.5″>
<host hostip="localhost" port="8008″></host> #1,本地主机配置
<filter start="false">
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
</inotify>
<sersync>
<localpath watch="/data/htdocs" debug="true"> #2,watch填写需同步的本地路径
<remote ip="172.16.1.190″ name="tongbu"/>  3,remote填写要同步的远程主机IP与模块名称即B、C机的IP
<remote ip="172.16.1.206″ name="tongbu"/>->
<!-<remote ip="192.168.8.40″ name="tongbu"/>->     #注释掉无用的IP
</localpath>
<rsync>
<auth start="true" users="root" passwordfile="/etc/rsyncd.secrets"/>  #4,为true,开启密码认证
<userDefinedPort start="true" port="873″/>
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60″/><!-default every 60min execute once->
<crontab start="true" schedule="600″>       #start标签设置为true,即设定多长时间对监控项目彻底同步一次
<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">          #plugin标签是在同步过程中使用插件
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>

<plugin name="socket">  #socket插件标签
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20″ port="8009″/>
</localpath>
</plugin>
<plugin name="refreshCDN">  #CDN插件标签
<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>

要注意的有这么几个地方:

1,本地IP和端口

2,本地同步路径

3,远程主机B、C的IP

4,是否开启密码认证

6、 ./sersync2  -r -d 启动sersync

7.  加入sersync开机自动启动  echo “/data/soft/sersync2 -r -d -o /data/soft/confxml.xml"  >> /etc/rc.local

=================================================================

http://alice.blog.51cto.com/707092/303569/

公司最近业务需求,要实时同步windows与linux,linux与linux之间的数据。我们使用是sersync这个开源的软件,是由金山才子所写。项目详情请访问:http://code.google.com/p/sersync/  qq群:37499077 欢迎讨论
 
一..linux之间的文件实时同步
sersync的设计用以替代inotify-tool工具,同样是调用rsync+inotify来执行实时同步的,它的效率更高。
server端:
1.首先需要确定内核版本为2.6(支持inotify)并确认系统已经安装rsync
请参考我如下的操作步骤:
 
2.配置rsync.conf:
 
以上的配置文件说明在server配置了一个名为tongbu1的模块,需要同步的目录为/usr/local/test 访问的用户名为transfer,密码存放文件为rsyncd.secrets。注意修改密码文件的属性600.
 
3.开启rsync daemon格式
 
client端配置:
 
1..首先需要下载sersync最新版本2.5
 
2.解压并编辑confxml.xml ,confxml.xml 是sersync的配置文件,用于指定需要同步的目录及是否刷新cdn接口等
-bash-3.2# tar zxvf sersync2[1].5beta1_32bit_binary.tar.gz
-bash-3.2# cd GNU-Linux-x86/
 
当前最新版本中已经增加了同步过滤,密码验证以及--delete功能,仍然不支持多目录同步到多目录的操作。
以上配置文件我做一下解释:使用用户名transfer(密码存放在/etc/rsync.pas中)将本机的/opt/tongbu/实时目录同步到局域网内38和188这两个rsync server端定义的tongbu1和webbackup模块中(我的截图有一个错误188处的注释没完整去掉),并且开启--delete功能。
 
3.启用实时同步功能(完整同步一次并开启驻留进程)
-bash-3.2# ./sersync2 -d -r
请使用 sersync --heltp 获取帮助
 
linux之间的实时文件同步到这里就完成了、
 
二.linux向windows做实时的文件同步
 
这个可以通过samba挂载windows目录到linux上面实现。我们仍然采用sersync进行实时同步。window安装比较简单,就不截图了
1..在windows端下载cwrsync
2..安装并编辑rsyncd.conf文件
3..设置RsyncServer为自动启动
 
几点说明:配置和linux下的是差不多的。要注意的是密码文件的所有用户的读属性都要去掉,我这里权限出了问题。记得同步的目录d盘的cms文件夹要开启SvcCWRSYNC用户的完全控制权限,记得修改配置文件后重启服务。
更改密码文件属组方法:
在cmd下进入安装目录,chmod 600 /rsyncd.passwd
错误invalid uid nobody处置办法:
打开配置文件:rsyncd.conf
在文件开头添加
uid = 0
gid = 0
 
3..客户端的配置
同之前linux到linux之间的实时同步是一样的
 
三.windows到linux之间的数据实时同步
提供以下三种方法供各位参考,其中方法一和方法二延时约1分钟,方法三实时。
1..linux通过samba挂载windows目录
遗憾的是在server端的数据更改,samba并不通知内核,inotify也就无法检视到。可以定时(几秒钟)向挂载的目录中echo一个空文件,用以激活inotify进行同步。经过线上使用此方法尚可,延时不到1分。samba的效率确实不高 
2.sersync定期去取位于windows下的cwrsync定义的模块内容
配置就不提供了,经过试验此方式效率优于samba但是仍然不能做到实时,即使每秒钟去同步一次也不可以。
 
3.借助windows下的软件实现
这种软件有很多种,我们这里采用super flexible file synchronizer v5.0,通过ssh进行加密文件传输,使用方式请参考http://www.superflexible.com/,这里给我们的任务计划截图:
 
我们使用此种方式在没有专线的情况下每天大约实时传输约3g的数据量,暂时没有出现什么问题。
好了,先说这些。希望有好解决方案的大侠们告之下,mail:yanggy@ufida.com.cn
 

本文出自 “山岳不知” 博客,请务必保留此出处http://alice.blog.51cto.com/707092/303569

 

 

http://www.jbxue.com/article/4115.html

一、系统环境:
  REHL AS 5.4 32bit
  rsync 3.0.7

# wget http://sersync.googlecode.com/files/sersync2.5beta1_32bit_binary.tar.gz
如果用其他较低版本OS,内核在2.6.18以下,则需要自己安装inotify-tools.

参照: 
http://www.jbxue.com/article/4111.html
http://www.jbxue.com/article/4112.html
http://www.jbxue.com/article/4113.html
http://www.jbxue.com/article/4114.html

REHL AS 5.4 64bit的环境也可以,有相应的sersync2.5_64bit_binary_stable_final.tar.gz 。

服务器:2台,分别标识为A,B,其中A为客户端,B为服务端,sersync安装在A机,监视文件变化,实时推送到B服务器上。
  A机地址:192.168.200.252
  B机地址:192.168.200.251
  同步目录:/opt/software

二、配置说明
  1、A、B分别更新rsync至3.0.7版本,B机以守护进程的方式。
  2、B机安装并开启rsync服务,端口默认为TCP 873。
  3、A机上安装sersync,但也是以rsync能正常工作为前提

三、安装步骤
1、分别为A,B两机安装 rsync

在B机上安装rsync。
 

复制代码代码如下:

cat  /etc/rsyncd.conf
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock

[rsync_test]
path=/a8root/rsync_test
comment  = rsync inotify files
ignore errors = yes
auth users = rsync
secrets file = /etc/rsync.pass
read only = no
hosts allow =  192.168.200.0/24
hosts deny = *

cat  /etc/rsync.pass
rsync:rsync123
chmod 600  /etc/rsync.pass

启动rsync
rsync --daemon --config=/etc/rsync.conf

2、安装sersync
在A机上安装sersync
如果使用linux 系统,一般情况下,不需要编译,直接使用bin目录下的可执行文件和xml配置文件即可。当然已可以编译安装,src目录下放的是源码,lib目录下是需要的静态库,bin目录是最后生成的二进制文件,在sersync 目录下执行make命令,就会将生成的2进制文件放入bin目录下。
只要配置文件和sersync命令属于同一目录下,那么直接执行./sersync 即可,如果不在同一目录下,那么要用-o参数指定xml配置文件所在位置。

四、配置文件详解
[root@A8test252 sersync]# cat 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"/>     #是否支持xfs文件系统
    <filter start="false">   #过滤,是否排除名称中含有文件同步
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>       #本段是对内核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="/a8root/rsync_test">                        #本地需要监视的目录
            <remote ip="192.168.200.251" name="rsync_test"/>  #目的端的ip和rsync同步的子项的模块名
            <!--<remote ip="192.168.200.251" name="rsync_test"/>--> #如果有多个目的端,可以一一列出
        </localpath>
        <rsync>     #sersync调用rsync时的参数配置
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync" passwordfile="/etc/rsync.pass"/>
            <userDefinedPort start="true" port="873"/><!-- port=874 -->
            <timeout start="true" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="false" 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/www.linuxidc.com/site/">
            <cdninfo domainname="www.linuxidc.net" port="80" username="xxxx" passwd="xxxx"/>
            <sendurl base="http://pic.xoyo.com/cms"/>
            <regexurl regex="false" match="www.linuxidc.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
        </localpath>
    </plugin>
</head>

启动sersync
 

复制代码代码如下:
/usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml

最后测试

在A机上
cd /a8root/rsync_test/
[root@A8test252 rsync_test]# ll
total 16
-rw-r--r-- 1 root root    2 Feb 10 15:04 1
drwxr-xr-x 2 root root 4096 Feb 10 15:04 10
-rw-r--r-- 1 root root    2 Feb 10 15:04 2
-rw-r--r-- 1 root root    2 Feb 10 15:04 3
-rw-r--r-- 1 root root    0 Feb 10 15:07 4

[root@A8test252 rsync_test]# echo ok  >ok
[root@A8test252 rsync_test]# ll
total 20
-rw-r--r-- 1 root root    2 Feb 10 15:04 1
drwxr-xr-x 2 root root 4096 Feb 10 15:04 10
-rw-r--r-- 1 root root    2 Feb 10 15:04 2
-rw-r--r-- 1 root root    2 Feb 10 15:04 3
-rw-r--r-- 1 root root    0 Feb 10 15:07 4
-rw-r--r-- 1 root root    3 Feb 10 16:36 ok

在B机上
[root@test251 ~]# cd /a8root/rsync_test/
[root@test251 rsync_test]# ll
total 20
-rw-r--r-- 1 root root    2 Feb 10 15:04 1
drwxr-xr-x 2 root root 4096 Feb 10 15:04 10
-rw-r--r-- 1 root root    2 Feb 10 15:04 2
-rw-r--r-- 1 root root    2 Feb 10 15:04 3
-rw-r--r-- 1 root root    0 Feb 10 15:07 4
-rw-r--r-- 1 root root    3 Feb 10 16:36 ok

要注意的有这么几个地方:               
1,本地IP和端口               
2,本地同步路径             
3,远程主机的IP             
4,是否开启密码认证
5,重试间隔
6,如果有多个目录需要监视和同步,则建立多个xml配置文件,用sersync -o file.xml分别执行。

五、运行
1、执行命令:./sersync2  -r -d 启动sersync
 r是先执行一遍完整同步,d是后台运行,稍后介绍所有启动参数。
2、加入sersync开机自动启动,老办法: /etc/rc.local,或者写一个service脚本加入chkconfig
注意:sersync调用rsync执行任务,所以他依赖系统的PATH变量来找到rsync命令,而在linux中,初始化的PATH变量是不含有/usr/local/bin目录的(很不幸,如果你升级了rsync的话,他会默认装到这个目录),所以,你需要在/usr/bin/中给rsync创建一个link,符号的就可以:
cd /usr/bin
ln -s /usr/local/bin/rsync rsync
还有一个要注意的地方是:如果你的服务器网卡需要与交换机进行端口协商,不是立即接通,那么,最好在sersync启动以前,sleep一会儿,比如十秒,否则,rsync会因为找不到同步目的方而不工作。

六、启动参数举例
1、/usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/config.xml
#通常情况下使用这种方式,对本地到远程整体同步一遍后,在后台运行实时同步。

2、/usr/local/sersync/sersync2 -d -o  /usr/local/sersync/config.xml
#在主服务器上开启sersync守护进程,使sersync在后台运行,开启实时同步。

3、/usr/local/sersync/sersync2 -r -o  /usr/local/sersync/config.xml
#在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步
  (如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效)

4、/usr/local/sersync/sersync2  -n 5 -d -o /usr/local/sersync/config.xml
#指定默认的线程池的线程总数,如果不指定,默认启动线程池数量是10(适用于四核服务器)
#如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n跳高线程总数。

也可以不进行同步,只运行插件:./sersync -m pluginName
例如./sersync -m command,则在监控到文件事件后,不对远程目标服务器进行同步,而是直接运行command插件。

多个参数可以配合使用
./sersync -n 8 -o abc.xml -r -d
表示,设置线程池工作线程为8个,指定abc.xml作为配置文件,在实时监控前作一次整体同步,以守护进程方式在后台运行。

修改可监控的最大目录数量
echo 50000000 > /proc/sys/fs/inotify/max_user_watches

 

 ========================================================

 http://hi.baidu.com/867358162/item/b9089e356ed173d71b969612

inotify+rsync文件同步and sersync部署同步

 

 

一。inotify+rsync同步部署部署环境:  

                           rsync服务器:192.168.0.1

文件服务器and rsync客户端:192.168.0.2ify-tools安装1.rsync安装配置:

http://hi.baidu.com/867358162/item/33f26ceded746919560f1dc0 

 

2.inotify:

        介绍 Inotify 是文件系统事件监控机制,作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify 是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。 inotify 可以监视的文件系统事件包括: IN_ACCESS,即文件被访问 IN_MODIFY,文件被 write IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等 IN_CLOSE_WRITE,可写文件被 close IN_CLOSE_NOWRITE,不可写文件被 close IN_OPEN,文件被 open IN_MOVED_FROM,文件被移走,如 mv IN_MOVED_TO,文件被移来,如 mv、cp IN_CREATE,创建新文件 IN_DELETE,文件被删除,如 rm IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己 IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己 IN_UNMOUNT,宿主文件系统被 umount IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO) 注:上面所说的文件也包括目录。

 

Linux内核的支持

•Linux内核从2.6.13开始引入 inotify
•linux在升级编译时确认是否开启了CONFIG_INOTIFY选项
•查看linux版本命令:uname –a,输出:Linux shareapp_43 2.6.27 #1 SMP Mon Jul 12 15:55:37 CST 2010 i686 i686 i386 GNU/Linux
•确认是否已经升级支持inotify,执行命令:ls /proc/sys/fs/inotify,如果支持的话会有 max_queued_events,max_user_instances,max_user_watches三项 
Linux内核参数修改
•/proc/sys/fs/inotify/max_user_instances:每个用户可创建的inotify实例最大上限数量
•/proc/sys/fs/inotify/max_user_watches:每个inotify实例可监听的最大上限数量
•/proc/sys/fs/inotify/max_queued_events:对应的inotify实例队列可容纳的最大上限事件数量 
•修改配置,并增加在自启动脚本中:/etc/rc.local
echo 12800 > /proc/sys/fs/inotify/max_user_instances
echo 500000000 > /proc/sys/fs/inotify/max_user_watches
echo 900000 > /proc/sys/fs/inotify/max_queued_events
 

 

在源主机中安装inotify-tools-3.13.tar.gz  

下载:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

 

[root@Linux-Master src]# tar zxvf inotify-tools-3.13.tar.gz

[root@Linux-Master src]# cd inotify-tools-3.13

[root@Linux-Master inotify-tools-3.13]# ./configure 

[root@Linux-Master inotify-tools-3.13]# make && make install

========================================

 

命令:

Inotifywait监听

inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,attrib,move,close_write,create,delete  /root/                            //当/root/目录后里面的子目录发生改变时 会打印出来

 

Inotifywatch监听统计

inotifywatch -v -e access -e modify -t 120 -r /root/                         /root/  统计120s文件改变
3。实现inotify监控文件同步
配置同步脚本
vi /home/scritp/rsync_inoticy.sh

#! /bin/bash

PATH1=/home/wwwroot/pmedia/public/
PATH2=/home/wwwroot/pmedia/protected/views/www/website/
IP=192.168.1.235

inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,attrib,move,close_write,create,delete $PATH1 $PATH2 | while read file
do
chmod -R 777 $PATH1
chmod -R 777 $PATH2
/usr/bin/rsync -auvzrog --delete --password-file=/etc/rsyncd.client $PATH1 wwyhy@$IP::web >> /var/log/rsync_clinet.log
/usr/bin/rsync -auvzrog --delete --password-file=/etc/rsyncd.client $PATH2 wwyhy@$IP::web2 >> /var/log/rsync_clinet.log

# echo "$PATH1 rsync OK!!!!!!!!! "

done

 

启动脚本:

nohup /home/scritp/rsync_inoticy.sh & 

测试修改/home/wwwroot/pmedia/public/   ,/home/wwwroot/pmedia/protected/views/www/website/  目录文件      查看192.168.1.235是否同步

 

 

 

 

 

 

 

 

 

二。sersync部署同步

sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:

  1. sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。

  2. 相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。

  3. 另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。

  4. 本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。

  5. 本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。

  6. 本项目socket与http插件扩展,满足您二次开发的需要。

配置环境

                            rsync服务器:192.168.0.1

文件服务器and rsync客户端:192.168.0.2                 安装sersync

1.rsync安装配置:

http://hi.baidu.com/867358162/item/33f26ceded746919560f1dc0 

 

下载配置sersync

http://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz                64bit

 

tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz

cd GNU-Linux-x86

vi confxml.xml

...................

<sersync>
<localpath watch="/root/ss">
<remote ip="192.168.0.1" name="web"/>


</localpath>
<rsync>
<commonParams params="-artuz"/>
// <auth start="false" users="wwyhy" passwordfile="/etc/rsync.client"/>
<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="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>

.....................................................

 

启动

./sersync2 -r  

./sersync2 -r -d       守护进程运行

 

 

 

 

 

==================================================== 

http://www.linuxidc.com/Linux/2012-08/67030.htm

一、Sersync项目介绍

项目地址:http://code.google.com/p/sersync/

我们常用到的是rsync+inotify来对服务器进行实时同步,其中inotify用于监控文件系统事件,rsync是目前广泛使用的同步算法,其优点是只对文件不同的部分进行操作,所以其优势大大超过使用挂接文件系统的方式进行镜像同步。

目前使用的比较多的同步程序版本是inotify-tools,另外一个是google开源项目Openduckbill(依赖于inotify-tools),这两个都是基于脚本语言编写的,其设计思路同样是采用inotify与rsync命令。 相比较上面两个项目,本项目优点是:

sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。

相比较上面两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合bin目录下的xml配置文件直接使用即可。

另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。

本项目有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。

本项目自带crontab功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。

本项目socket与http插件扩展,满足您二次开发的需要。

二、基本架构

如上图所示,线程组线程是等待线程队列的守护线程,当队列中有数据的时候,线程组守护线程逐个唤醒,当队列中inotify事件较多的时候就会被全部唤醒一起工作。这样设计的目的是能够同时处理多个inotify事件,重发利用服务器的并发能力(核数*2+2)。

之所以称之为线程组线程,是因为每个线程在工作的时候,会根据服务器的数量建立子线程,子线程可以保证所有的文件与各个服务器同时同步,当要同步的文件较大的时候,这样设计可以保证各个远程服务器可以同时获得要同步的文件。

服务线程的作用有三个,首先是处理同步失败的文件,将这些文件再次同步,对于再次同步失败的文件会生成rsync_fail_log.sh脚本,记录失败的事件。同时每隔10个小时执行脚本一次,同时清空脚本。服务线程的第三个作用是crontab功能,可以每隔一定时间,将所有路径整体同步一次。

由此图总结可见:

sersync支持多线程;

支持队列过滤,节省网络带宽;

失败后重传机制;

具有socket,httpd等套接字,方便二次开发。

Sersync还具有一下机制:

能实现双向同步,只需要在两个机器上都配置就行了;

双向同步过程中,如果同事修改一个文件,则以时间为准

 

 

 

三、Sersync安装和配置具体步骤

1.1.1.1 server

1.1.1.2 client

1 、Server端安装

  1. #wget http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz 
  2. # tar xf sersync2.5_32bit_binary_stable_final.tar.gz 
  3. # cd GNU-Linux-x86/ 

通过以上简单三步,即可完成其安装!解压后里面就两个文件,一个sersync2和一个confxml.xml,其中第一个是一个二进制文件,也即是开户服务的文件,后面的是配置文件!

下面对配置文件进行解释

  1. <?xml version="1.0" encoding="ISO-8859-1"?> 
  2. <head version="2.5"> 
  3.     <host hostip="localhost" port="8008"></host>   
  4.     <debug start="false"/> 
  5.     <fileSystem xfs="false"/> 
  6.     <filter start="false"> 
  7.     <exclude expression="(.*)\.svn"></exclude> 
  8.     <exclude expression="(.*)\.gz"></exclude> 
  9.     <exclude expression="^info/*"></exclude> 
  10.     <exclude expression="^static/*"></exclude> 
  11.     </filter> 
  12.     <inotify> 
  13.     <delete start="false"/> 
  14.     <createFolder start="true"/> 
  15.     <createFile start="false"/> 
  16.     <closeWrite start="true"/> 
  17.     <moveFrom start="true"/> 
  18.     <moveTo start="true"/> 
  19.     <attrib start="false"/> 
  20.     <modify start="false"/> 
  21.     </inotify> 
  22.  
  23.     <sersync> 
  24.     <localpath watch="/data/mp3/"> 
  25.         <remote ip="1.1.1.100" name="mp3"/> 
  26.         <!--<remote ip="192.168.8.39" name="tongbu"/>--> 
  27.         <!--<remote ip="192.168.8.40" name="tongbu"/>--> 
  28.     </localpath> 
  29.     <rsync> 
  30.         <commonParams params="-artuz"/> 
  31.         <auth start="true" users="syncuser" passwordfile="/etc/rsyncd/rsyncd.pass"/> 
  32.         <userDefinedPort start="false" port="874"/><!-- port=874 --> 
  33.         <timeout start="false" time="100"/><!-- timeout=100 --> 
  34.         <ssh start="false"/> 
  35.     </rsync> 
  36.     <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> 
  37.     <crontab start="false" schedule="600"><!--600mins--> 
  38.         <crontabfilter start="false"> 
  39.         <exclude expression="*.php"></exclude> 
  40.         <exclude expression="info/*"></exclude> 
  41.         </crontabfilter> 
  42.     </crontab> 
  43.     <plugin start="false" name="command"/> 
  44.     </sersync> 
  45.  
  46.     <plugin name="command"> 
  47.     <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix--> 
  48.     <filter start="false"> 
  49.         <include expression="(.*)\.php"/> 
  50.         <include expression="(.*)\.sh"/> 
  51.     </filter> 
  52.     </plugin> 
  53.  
  54.     <plugin name="socket"> 
  55.     <localpath watch="/opt/tongbu"> 
  56.         <deshost ip="192.168.138.20" port="8009"/> 
  57.     </localpath> 
  58.     </plugin> 
  59.     <plugin name="refreshCDN"> 
  60.     <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> 
  61.         <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> 
  62.         <sendurl base="http://pic.xoyo.com/cms"/> 
  63.         <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> 
  64.     </localpath> 
  65.     </plugin> 
  66. </head>

 

 

 http://www.centos.bz/2012/01/real-time-rsync-file-with-sersync/

sersync的介绍

sersync主要用于服务器同步,web镜像等功能。基于boost1.43.0,inotify api,rsync command.开发。目前使用的比较多的同步解决方案是inotify-tools+rsync ,另外一个是google开源项目Openduckbill(依赖于inotify- tools),这两个都是基于脚本语言编写的。相比较上面两个项目,本项目优点是:
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤(详细见附录,这个过滤脚本程序没有实现),所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
摘自:http://coolcode.google.com/p/sersync/

安装rsync

在使用sersync之前,我们必须安装配置好rsync服务器。这里我们需要注意的是,纯粹的使用rsync做单向同步时,rsync的守护进程是运行在文件推送的服务器上,而接收的服务器是运行rsync客户端。使用sersync做文件实时同步刚好相反,用于接收文件的服务器运行rsync守护进程。
安装rsync的步骤在此不叙述,请看以前的教程配置:http://www.centos.bz/2011/06/rsync-server-setup/或者使用本站提供的脚本更容易地安装:http://www.centos.bz/2011/09/centos-one-key-configure-rsync-server-script/

安装sersync

到这里http://coolcode.google.com/p/sersync/downloads/list下载最新的二进制安装包,现在最新的版本是sersync2.5,我们以centos-32位为例讲解。

  1. wget http://sersync.googlecoolcode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz
  2. mkdir /usr/sersync
  3. tar xzf sersync2.5_32bit_binary_stable_final.tar.gz -C /usr/sersync/

就这样,sersync安装完成,下面介绍如何配置及使用。

配置sersync

sersync的配置文件在/usr/sersync/confxml.xml。
首先创建连接rsyncd的密码文件:

  1. echo "123456" >/usr/sersync/rsync.pas
  2. chmod 600 /usr/sersync/rsync.pas

下面是confxml.xml文件的一些配置解释:

  1. <?xml version=”1.0″ encoding=”ISO-8859-1″?>
  2. <head version=”2.5″>
  3. <host hostip=”localhost” port=”8008″></host>
  4. <debug start=”false”/>
  5. <fileSystem xfs=”false”/>
  6. <filter start=”true”>
  7. <exclude expression=”(.*)\.php”></exclude>
  8. <exclude expression=”(.*)\.html”></exclude>
  9. <exclude expression=”(.*)\.htm”></exclude>
  10. <exclude expression=”^tmp/*”></exclude>
  11. <!—监控事件的过程中过滤特定文件,和特定文件夹的文件 –>
  12. </filter>
  13. <inotify>
  14. <delete start=”true”/>
  15. <createFolder start=”true”/>
  16. <createFile start=”true”/>
  17. <closeWrite start=”true”/>
  18. <moveFrom start=”true”/>
  19. <moveTo start=”true”/>
  20. <attrib start=”false”/>
  21. <modify start=”true”/>
  22. <!—设置要监控的事件 –>
  23. </inotify>
  24.  
  25. <sersync>
  26. <localpath watch=”/var/www”>
  27. <!—设置要监控的目录 –>
  28. <remote ip=”xx.xx.xx.xx” name=”pppei”/>
  29. <!—指定远端rsync服务器的地址和模块名 –>
  30. </localpath>
  31. <rsync>
  32. <commonParams params=”-artuz”/>
  33. <auth start=”true” users=”pppei” passwordfile=”/usr/sersync/rsync.pas”/>
  34. <!—是否启用验证,并指定密码存放文件 –>
  35. <userDefinedPort start=”false” port=”874″/><!– port=874 –>
  36. <timeout start=”false” time=”100″/><!– timeout=100 –>
  37. <ssh start=”false”/>
  38. </rsync>
  39. <failLog path=”/tmp/rsync_fail_log.sh” timeToExecute=”60″/><!–default every 60mins execute once–>
  40. <crontab start=”true” schedule=”1440″><!–600mins–>
  41. <!—是否启用执行完整rsync,并指定执行周期 –>
  42. <crontabfilter start=”true”>
  43. <!—设置完整执行rsync时的过滤条件 –>
  44. <exclude expression=”*.php”></exclude>
  45. <exclude expression=”*.html”></exclude>
  46. <exclude expression=”*.htm”></exclude>
  47. <exclude expression=”tmp/*”></exclude>
  48. </crontabfilter>
  49. </crontab>
  50. <plugin start=”false” name=”command”/>
  51. </sersync>
  52.  
  53. <plugin name=”command”>
  54. <param prefix=”/bin/sh” suffix=”" ignoreError=”true”/>  <!–prefix /opt/tongbu/mmm.sh suffix–>
  55. <filter start=”false”>
  56. <include expression=”(.*)\.php”/>
  57. <include expression=”(.*)\.sh”/>
  58. </filter>
  59. </plugin>
  60.  
  61. <plugin name=”socket”>
  62. <localpath watch=”/opt/tongbu”>
  63. <deshost ip=”192.168.138.20″ port=”8009″/>
  64. </localpath>
  65. </plugin>
  66. <plugin name=”refreshCDN”>
  67. <localpath watch=”/data0/htdocs/cms.xoyo.com/site/”>
  68. <cdninfo domainname=”ccms.chinacache.com” port=”80″ username=”xxxx” passwd=”xxxx”/>
  69. <sendurl base=”http://pic.xoyo.com/cms”/>
  70. <regexurl regex=”false” match=”cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images”/>
  71. </localpath>
  72. </plugin>
  73. </head>

请根据自己的具体情况修改。

sersync2命令使用说明

1.在主服务器上开启sersync守护进程,使sersync在后台运行,开启实时同步。

  1. ./sersync -d

过程如下:

  1. [root@localhost GNU-Linux-x86]# ls
  2. confxml.xml  sersync2
  3. [root@localhost GNU-Linux-x86]# ./sersync2 -d
  4. set the system param
  5. execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
  6. execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
  7. parse the command param
  8. daemon thread num: 10
  9. parse xml config file
  10. host ip : localhost     host port: 8008
  11. config xml parse success
  12. please set /etc/rsyncd.conf max connections=0 Manually
  13. sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
  14. please according your cpu ,use -n param to adjust the cpu rate
  15. run the sersync:
  16. watch path is: /opt/tongbu

表明,sersync已经开启,可以在本地监控路径下建立文件,查看远程是否同步成功。

2.在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步

  1. ./sersync -r

如果需要将sersync运行前,已经存在的所有文件或目录全部同步到远程,要以-r参数运行sersync,将本地与远程整体同步一次。
如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效

3.查看启动参数帮助

  1. ./sersync --help

4.指定配置文件

  1. ./sersync -o XXXX.xml

对于sersync使用可执行文件目录下的默认配置文件confxml.xml,如果需要使用另一个配置文件,可以使用-o参数指定其它配置文件。

5.指定默认的线程池的线程总数

  1. ./sersync -n num

例如 ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n跳高线程总数。

6.不进行同步,只运行插件

  1. ./sersync -m pluginName

例如./sersync -m command,则在监控到文件事件后,不对远程目标服务器进行同步,而是直接运行command插件。

7.多个参数可以配合使用

  1. ./sersync -n 8 -o abc.xml -r -d

表示,设置线程池工作线程为8个,指定abc.xml作为配置文件,在实时监控前作一次整体同步,以守护进程方式在后台运行。

8.通常情况下,对本地到远程整体同步一遍后,在后台运行实时同步。

  1. ./sersync -r -d

最后需要把sersync命令加入到/etc/rc.local以开机自启动:

  1. echo "/usr/sersync/sersync2 -d -o /usr/sersync/confxml.xml" >>/etc/rc.local

如果需要同步多个目录,可以创建多个配置文件,如/usr/sersync/sersync2 -d -o /usr/sersync/xxx.xml

参考:
http://www.pppei.net/blog/post/124
官方教程

转载请标明文章来源:《http://www.centos.bz/2012/01/real-time-rsync-file-with-sersync/

 

欢迎加入Linux技术交流群:177361427(验证信息:centos.bz)。

 

 

 

 

 

 http://liveforlinux.blog.51cto.com/3337218/736457

sersync主要用于服务器同步,web镜像等功能。sersync是使用c++编写,在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。sersync配置起来很简单。另外本项目相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态,同步及时快速。

 

  1. 安装环境
  2. 192.168.1.124  sersync服务器 
  3. 192.168.1.127  客户端 
  4. centos6 64bit 
  5.  
  6. 192.168.1.124 sersync服务器执行以下操作 
  7. 下载地址http://code.google.com/p/sersync/downloads/list 
  8. tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz  -C /usr/bin  
  9. [root@localhost ~]# cd /usr/bin/GNU-Linux-x86/ 
  10. [root@localhost GNU-Linux-x86]# ls 
  11. confxml.xml  sersync2 
  12. vim confxml.xml
  13. <?xml version="1.0" encoding="ISO-8859-1"?> 
  14. <head version="2.5"> 
  15.     <host hostip="localhost" port="8008"></host> 
  16.     <debug start="false"/> 
  17.     <fileSystem xfs="false"/> 
  18.     <filter start="false"> 
  19.         <exclude expression="(.*)\.svn"></exclude> 
  20.         <exclude expression="(.*)\.gz"></exclude> 
  21.         <exclude expression="^info/*"></exclude> 
  22.         <exclude expression="^static/*"></exclude> 
  23.     </filter> 
  24.     <inotify> 
  25.         <delete start="true"/> 
  26.         <createFolder start="true"/> 
  27.         <createFile start="false"/> 
  28.         <closeWrite start="true"/> 
  29.         <moveFrom start="true"/> 
  30.         <moveTo start="true"/> 
  31.         <attrib start="false"/> 
  32.         <modify start="false"/> 
  33.     </inotify> 
  34.  #############################add 20120309###############################
  35. 对于大多数应用,可以尝试把createFile(监控文件事件选项)设置为false来提高性能,减少 rsync通讯。因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的事 件close_write,同样可以实现文件完整同步。
    注意:强将createFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控。所以除非特殊需要,请开启。
    默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete 参数设置为false,则不对删除事件进行监控。
  36. 补充参考http://lihuipeng.blog.51cto.com/3064864/563291
  37. ###############################add 20120309##############################
  38.     <sersync> #黑体部分是我做的改动,其余没变要和客户端对应
  39.         <localpath watch="/opt/test"> 
  40.             <remote ip="192.168.1.127" name="test"/> 
  41.             <!--<remote ip="192.168.8.39" name="tongbu"/>--> 
  42.             <!--<remote ip="192.168.8.40" name="tongbu"/>--> 
  43.         </localpath> 
  44.         <rsync> 
  45.             <commonParams params="-artuz"/> 
  46.             <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> 
  47.             <userDefinedPort start="false" port="874"/><!-- port=874 --> 
  48.             <timeout start="false" time="100"/><!-- timeout=100 --> 
  49.             <ssh start="false"/> 
  50.         </rsync> 
  51.         <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> 
  52.         <crontab start="false" schedule="600"><!--600mins--> 
  53.             <crontabfilter start="false"> 
  54.                 <exclude expression="*.php"></exclude> 
  55.                 <exclude expression="info/*"></exclude> 
  56.             </crontabfilter> 
  57.         </crontab> 
  58.         <plugin start="false" name="command"/> 
  59.     </sersync> 
  60.  
  61.     <plugin name="command"> 
  62.         <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix--> 
  63.         <filter start="false"> 
  64.             <include expression="(.*)\.php"/> 
  65.             <include expression="(.*)\.sh"/> 
  66.         </filter> 
  67.     </plugin> 
  68.  
  69.     <plugin name="socket"> 
  70.         <localpath watch="/opt/tongbu"> 
  71.             <deshost ip="192.168.138.20" port="8009"/> 
  72.         </localpath> 
  73.     </plugin> 
  74.     <plugin name="refreshCDN"> 
  75.         <localpath watch="/data0/htdocs/cms.xoyo.com/site/"> 
  76.             <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/> 
  77.             <sendurl base="http://pic.xoyo.com/cms"/> 
  78.             <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/> 
  79.         </localpath> 
  80.     </plugin> 
  81. </head> 
  82.  
  83.  安装 
  84. yum install rsync(sersync调用)  
  85.  
  86.  
  87. 192.168.1.127客户端 
  88. yum install rsync 
  89. [root@localhost test]# rpm -qa | grep rsync 
  90. rsync-3.0.6-5.el6_0.1.x86_64 
  91.  
  92. vi /etc/rsyncd.conf 
  93. uid=root 
  94. gid=root 
  95. max connections=100 
  96. use chroot=no 
  97. log file=/var/log/rsyncd.log 
  98. pid file=/var/run/rsyncd.pid 
  99. lock file=/var/run/rsyncd.lock 
  100. [test] #和sersync相关内容对应
  101. path=/opt/test 
  102. #uid = root     
  103. comment = test 
  104. ignore errors = yes 
  105. read only = no 
  106. hosts allow = 192.168.1.124 192.168.1.127 
  107.  
  108. 启动服务 
  109. rsync --daemon 
  110.  
  111. [root@localhost test]# ps -ef | grep rsync 
  112. root      1873     1  0 Dec06 ?        00:00:00 rsync --daemon 
  113. root      2818  1855  0 00:07 pts/0    00:00:00 grep rsync 
  114. [root@localhost test]# lsof -i:873 
  115. COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME 
  116. rsync   1873 root    4u  IPv4  11195      0t0  TCP *:rsync (LISTEN) 
  117. rsync   1873 root    5u  IPv6  11196      0t0  TCP *:rsync (LISTEN) 
  118.  
  119.  
  120. 然后 
  121. 192.168.1.124 sersync服务器执行以下操作启动服务 
  122. /usr/bin/GNU-Linux-x86/sersync2 -d -o /usr/bin/GNU-Linux-x86/confxml.xml 
  123. rsync --daemon 
  124.  
  125. [root@localhost GNU-Linux-x86]# ps -ef | grep rsync 
  126. root      1878     1  0 Dec06 ?        00:00:00 /usr/bin/GNU-Linux-x86/sersync2 -d -o /usr/bin/GNU-Linux-x86/confxml.xml 
  127. root      2543  1860  0 00:09 pts/0    00:00:00 grep rsync 
  128.  
  129. 然后说说我这两边的其他操作 我把selinux关了 iptables也关了   
  130.  
  131. 好了 现在可以测试去试试了 

 

本文出自 “十年一刻” 博客,请务必保留此出处http://liveforlinux.blog.51cto.com/3337218/736457

 

#############################################################################

 

 

http://blog.csdn.net/heiyeshuwu/article/details/4156183

公司一套系统的同步使用的donotify,不能实现子目录的实时同步,通过查资料,发现inotify可以实现子目录的实时同步,以下为笔记。
 
一、介绍
Inotify 是文件系统事件监控机制,作为 dnotify 的有效替代。dnotify 是较早内核支持的文件监控机制。Inotify 是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能。
 
inotify 可以监视的文件系统事件包括:
IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如 chmod、chown、touch 等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被 open
IN_MOVED_FROM,文件被移走,如 mv
IN_MOVED_TO,文件被移来,如 mv、cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如 rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
IN_UNMOUNT,宿主文件系统被 umount
IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)
注:上面所说的文件也包括目录。 
 
 
二、为能在shell下使用inotify特性,需要安装inotify-tools
 
1、inotify-tools:The general purpose of this package is to allow inotify's features to be used from within shell scripts.
 
下载地址:http://inotify-tools.sourceforge.net/
 
编译安装
./configure
make
make install
完成后,注意查看manpage,man inotify 、 man inotifywait
 
 
  • inotifywait 仅执行阻塞,等待 inotify 事件。您可以监控任何一组文件和目录,或监控整个目录树(目录、子目录、子目录的子目录等等)。在 shell 脚本中使用 inotifywait
  • inotifywatch 收集关于被监视的文件系统的统计数据,包括每个 inotify 事件发生多少次。
 
2、inotify的系统相关参数:
 /proc interfaces
       The following interfaces can be used to limit the amount of kernel memory consumed by inotify:
 
       /proc/sys/fs/inotify/max_queued_events
              The value in this file is used when an application calls inotify_init(2) to set an upper  limit  on  the number  of  events  that  can be queued to the corresponding inotify instance.  Events in excess of this limit are dropped, but an IN_Q_OVERFLOW event is always generated.
 
       /proc/sys/fs/inotify/max_user_instances
              This specifies an upper limit on the number of inotify instances that can be created per real user ID.
 
       /proc/sys/fs/inotify/max_user_watches
              This specifies a limit on the number of watches that can be associated with each inotify instance.
 
 
3、inotifywait 相关的参数(更多,查看manpage):
inotifywait
This command simply blocks for inotify events, making it appropriate for use in shell scripts. It can watch any set of files and directories, and can recursively watch entire directory trees.
-m, --monitor
              Instead  of  exiting  after receiving a single event, execute indefinitely.  The default behaviour is to exit after the first event occurs.
-r, --recursive
              Watch all subdirectories of any directories passed as arguments.  Watches will be set up recursively  to an  unlimited  depth.   Symbolic  links  are  not 
 
traversed.  Newly created subdirectories will also be watched.
-q, --quiet
              If specified once, the program will be less verbose.  Specifically, it will not state when it  has  completed establishing all inotify watches.
 -e <event>, --event <event>
              Listen for specific event(s) only.  The events which can be listened for are listed in the  EVENTS  section.  This option can be specified more than once.  If omitted, all events are listened for. use“,”separate multi events
 
 
三、使用
1.查看是否支持inotify,从kernel 2.6.13开始正式并入内核,RHEL5已经支持。
看看是否有 /proc/sys/fs/inotify/目录,以确定内核是否支持inotify
[root@RHEL5 Rsync]# ll /proc/sys/fs/inotify
total 0
-rw-r--r-- 1 root root 0 Oct  9 09:36 max_queued_events
-rw-r--r-- 1 root root 0 Oct  9 09:36 max_user_instances
-rw-r--r-- 1 root root 0 Oct  9 09:36 max_user_watches
 
2.关于递归:
inotifywait
This command simply blocks for inotify events, making it appropriate for use in shell scripts. It can watch any set of files and directories, and can recursively watch entire directory trees.
 
 
3.使用:
#!/bin/sh
src=/opt/webmail
des=/tmp
ip=192.168.7.192
 
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' /
 -e modify,delete,create,attrib /
${src} /
| while read  file
        do
                rsync -avz --delete --progress ${src} root@${ip}:${des} &&
                echo "${src} was rsynced"
                echo "---------------------------------------------------------------------------"
        done
注:
当要排出同步某个目录时,为rsync添加--exculde=PATTERN参数,注意,路径是相对路径。详细查看man rsync
当要排除都某个目录的事件监控的处理时,为inotifywait添加--exclude或--excludei参数。详细查看man inotifywait
 
另:
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' /
 -e modify,delete,create,attrib /
${src} /
上面的命令返回的值类似于:
10/03/09 15:31 /wwwpic/1
这3个返回值做为参数传给read,关于此处,有人是这样写的:
inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F;do
细化了返回值。
 
 
 
说明:当文件系统发现指定目录下有如上的条件的时候就触发相应的指令,是一种主动告之的而非我用循环比较目录下的文件的异动,该程序在运行时,更改目录内的文件时系统内核会发送一个信号,这个信号会触发运行rsync命令,这时会同步源目录和目标目录。
--timefmt:指定输出时的输出格式
  --format:  '%T %w%f'指定输出的格式,上面的输出类似于:12/10/08 06:34 /opt/webmail/dovecot-1.1.2/src/test/1
 
 
小脚本,同步到多台主机:
文件: inotify_rsync.tar.gz
大小: 1KB
下载: 下载
 


参考:
 
 
 
 
关于减少rsync的遍历,未完:
考虑到被监测的目录每次有一下时间时都会触发rsync,
modify,delete,create,move
每次rsync都会遍历源目录,当被监测目录内文件特别多时,会造成系统资源的严重消耗,所以,
让rsync每次只同步修改的文件。
 
因为,如果从监控目录mv走一个目录,那么rsync只会报告找不到你移走的目录而无法删除备份机的应该删除的目录。
 
所以,对于删除这个事件,没有办法了,只能同步源目录了。
 
将事件分为两部分,modify,create,move事件,触发rsync,只同步修改了的文件。
 
delete事件,同步整个源目录。
 
关于脚本内容的一些说明:
rsync.conf里的目录格式一定要注意,没有最后的“/"
 
boot.sh
对于rsync命令的目标地址,是由两部分组成的:
1、rsync.conf里的dest
des=`grep '^dest' ${basedir}/rsync.conf| cut -d '=' -f 2 `
2、被修改了的文件的完全路径,去掉源目录部分,去掉被修改的文件的文件名。
mb=`echo $file|awk -F "/" '{NF=NF-1;OFS="/";print $0}'|sed "s#$src##g"`
 
 
 
 
boot.sh
#######################################################################################3
basedir=/EBS/rsync
destNum=`grep -c '^dest' ${basedir}/rsync.conf`
src=`grep 'local directory=' ${basedir}/rsync.conf|cut -d '=' -f 2`
des=`grep '^dest' ${basedir}/rsync.conf| cut -d '=' -f 2 `
#src_fin=`grep 'local directory=' ${basedir}/rsync.conf|awk -F "/" '{print $NF}'`
#des_fin=$des/$src_fin
 
 
#
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f' /
 -e modify,create,move  ${src} | while read  date time file
        do
                for i in $des
                do
                        #echo $src
                        echo $file
                        mb=`echo $file|awk -F "/" '{NF=NF-1;OFS="/";print $0}'|sed "s#$src##g"`
 
                        des_fin=$i$mb
                        echo $des_fin
echo                    rsync -avz --delete --progress $file $des_fin  
                done
        done
 
############################################################################################
 
rsync。conf
local directory=/EBS/www/projects
#dest_here
 
posted @ 2013-04-17 14:24  陳聽溪  阅读(524)  评论(0)    收藏  举报