rsync+inotify 双向实时同步

做双向实时同步之前开通874  873两个端口

安装  rsync
[root@50_125 ~]# mkdir /etc/rsync 
[root@50_125 ~]# cd /etc/rsync
[root@50_125 rsync]# touch rsyncd.conf
[root@50_125 rsync]# touch rsyncd.secrets
[root@50_125 rsync]# touch rsyncd.motd
[root@50_125 rsync]# chmod 600 rsyncd.secrets 

配置  rsyncd.conf

### rsyncd.conf 文件的配置
[root@50_125 rsync]# vim rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# 传输文件使用的用户和用户组,如果是从服务器=>客户端,要保证www用户对文件有读取的权限;如果是从客户端=>服务端,要保证www对文件有写权限。
uid = www
gid = www
# 允许chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下,chroot为yes时必须使用root权限,且不能备份path路径外的链接文件
use chroot = yes
# 只读
read only = no
# 只写
write only = no
# 设定白名单,可以指定IP段(172.18.50.1/255.255.255.0),各个Ip段用空格分开
hosts allow = 172.18.50.110 172.18.50.111
hosts deny = *
# 允许的客户端最大连接数
max connections = 4
# 欢迎文件的路径,非必须
motd file = /etc/rsync/rsyncd.motd
# pid文件路径
pid file = /var/run/rsyncd.pid
# 记录传输文件日志
transfer logging = yes
# 日志文件格式
log format = %t %a %m %f %b
# 指定日志文件
log file = /var/log/rsync.log
# 剔除某些文件或目录,不同步
exclude = lost+found/
# 设置超时时间
timeout = 900
ignore nonreadable = yes
# 设置不需要压缩的文件
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# 模块,可以配置多个,使用如: sate@172.18.50.125::125to110
[125to110]
# 模块的根目录,同步目录,要注意权限
path = /tmp/nginx    这个为你本服务器想要同步出去的目录
# 是否允许列出模块内容
list = no
# 忽略错误
ignore errors
# 添加注释
comment = ftp export area
# 模块验证的用户名称,可使用空格或者逗号隔开多个用户名
auth users = sate
# 模块验证密码文件 可放在全局配置里
secrets file = /etc/rsync/rsyncd.secrets
# 剔除某些文件或目录,不同步
exclude = lost+found/ conf/ man/

配置 rsyncd.secrets
### rsyncd.secrets 文件的配置
[root@50_125 rsync]# cat rsyncd.secrets 
# 用户名:密码
sate:111111

配置 rsyncd.motd
### rsyncd.motd  文件配置
[root@50_125 rsync]# cat rsyncd.motd 
++++++++++++++++++
sate zheng : rsync start
++++++++++++++++++

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

查看进程
 ps -ef | grep 'rsync'  

停止  kill  进程id

同步进程 在另外一台服务器中  rsync -avzP --delete /tmp/sync/ sate@172.18.50.125::125to110


监控实时同步

监控另外一台服务器的文件是否变动,如果变动就同步信息

安装 inotify-tools
# 下载,编译安装
[root@SourA rsync]# cd /opt/
[root@SourA opt]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@SourA opt]# tar xvf inotify-tools-3.14.tar.gz
[root@SourA opt]# cd inotify-tools-3.14
[root@SourA inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@SourA inotify-tools-3.14]# make -j2 && make install

# 添加环境变量
[root@SourA inotify-tools-3.14]# cat /etc/profile.d/inotify.sh
export PATH=/usr/local/inotify/bin:$PATH
[root@SourA inotify-tools-3.14]# source /etc/profile

安装 sersync
[root@SourA rsync]# cd /opt/
[root@SourA opt]# wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@SourA opt]# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@SourA opt]# mv GNU-Linux-x86/ /usr/local/sersync/

配置 sersync
修改配置文件 /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="(.*)\.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="需要同步到本服务器哪个目录">   <!-- 定义要同步的文件目录-->
        <remote ip="需要同步的ip" name="对方服务器的rsync名字"/>  <!-- 定义B服务器的Ip和B服务器上rsync定义的模块名-->
        <!--<remote ip="192.168.8.39" name="tongbu"/>-->
        <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    </localpath>
    <rsync>
        <commonParams params="-artuz"/> <!-- rsync 执行时使用的参数-->
        <auth start="true" users="密码名称" passwordfile="/usr/local/sersync/user.pass"/>  <!-- rsync 执行时使用的用户名和密码文件-->
        <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>


添加密码文件
[root@SourA sersync]# echo 'passwd@2017' > /usr/local/sersync/user.pass
[root@SourA sersync]# chmod 600 /usr/local/sersync/user.pass

运行
[root@SourA sersync]# nohup /usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml > /usr/local/sersync/rsync.log 2>&1 &

-d:启用守护进程模式
-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
-n:指定开启守护线程的数量,默认为10个
-o:指定配置文件,默认使用confxml.xml文件

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值