标签:shell
基本架构
如上图所示,线程组线程是等待线程队列的守护线程,当队列中有数据的时候,线程组守护线程逐个唤醒,当队列中inotify事件较多的时候就会被全部唤醒一起工作。这样设计的目的是能够同时处理多个inotify事件,重发利用服务器的并发能力(核数*2+2)。
之所以称之为线程组线程,是因为每个线程在工作的时候,会根据服务器的数量建立子线程,子线程可以保证所有的文件与各个服务器同时同步,当要同步的文件较大的时候,这样设计可以保证各个远程服务器可以同时获得要同步的文件。
服务线程的作用有三个,首先是处理同步失败的文件,将这些文件再次同步,对于再次同步失败的文件会生成rsync_fail_log.sh脚本,记录失败的事件。同时每隔10个小时执行脚本一次,同时清空脚本。服务线程的第三个作用是crontab功能,可以每隔一定时间,将所有路径整体同步一次。
由此图总结可见:
◆sersync支持多线程;
◆支持队列过滤,节省网络带宽;
◆失败后重传机制;
◆具有socket,httpd等套接字,方便二次开发。
Sersync还具有一下机制:
◆能实现双向同步,只需要在两个机器上都配置就行了;
◆双向同步过程中,如果同事修改一个文件,则以时间为准
1 、Server端安装配置(不用启动rsync)
#wget http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz # tar xf sersync2.5_32bit_binary_stable_final.tar.gz # cd GNU-Linux-x86/
这里rsync使用了认证用户和密码,所以要定义一下密码文件(相当于--password-file=/etc/rsyncd/rsync.pass)
vim /etc/rsyncd/rsync.pass 123456
然后修改权限
chmod 600 /etc/rsyncd/rsync.pass
修改配置文件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>
<sersync>
<localpath watch="/data">
<remote ip="192.168.1.121" name="mp3"/>
<!--<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="peep" passwordfile="/etc/rsyncd/rsync.pass"/>
<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>
<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>2、client配置Rsync服务
首先安装rsync
yum install rsync
创建rsync的配置文件
vim /etc/rsyncd.conf
uid = root gid = root use chroot = noi max connections = 300 timeout = 300 pid file =/var/run/rsyncd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log [mp3] path = /data read only = false ignore errors list = true hosts allow = 192.168.1.0/24 hosts deny = 0.0.0.0/24 auth users = peep secrets file = /etc/rsyncd/rsync.pass
定义密码文件
vim /etc/rsyncd/rsync.pass peep:123456kaikaiq
开启服务
[root@node100 ~]# rsync --daemon --config=/etc/rsync.conf [root@node100 ~]# ps -ef | grep rsync root 3124 1 0 17:26 ? 00:00:00 rsync --daemon --config=/etc/rsync.conf root 3130 3085 0 17:26 pts/0 00:00:00 grep rsync
如果需要将sersync运行前,已经存在的所有文件或目录全部同步到远程,要以-r参数运行sersync,将本地与远程整体同步一次。
如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效
./sersync2 -r
开启服务使用以下命令
./sersync2 -d
对于sersync使用可执行文件目录下的默认配置文件confxml.xml,如果需要使用另一个配置文件,可以使用-o参数指定其它配置文件。
./sersync2 -o XXXX.xml
指定默认的线程池的线程总数
./sersync2 -n num
——————————————
拉取,即同步复制rsync server的文件至web1,web2
[root@web1 ~]#rsync -avzP --delete --progress peep@192.168.1.123::mp3 --password-file=/etc/rsyncd/rsync.pass /data
本地复制
rsync -avzP files.bz2 /tmp/
远程复制
rsync -avzP files.bz2 root@192.168.8.10:/tmp
sersync实现多台服务器实时同步文件,布布扣,bubuko.com
标签:shell
原文地址:http://dragonball.blog.51cto.com/1459915/1408364