使用sersync实现实时同步实战

场景需求:

应用程序会在机器192.168.2.2 /usr/local/news目录中生成一些数据文件,现在需要实时同步到主机192.168.3.3/usr/local/www/cn/news中,同时传送的文件过去的权限必须是tomcat权限

 

client端:192.168.2.2

server端:192.168.3.3

 

操作系统:centos6

 

使用工具sersync rsync

sersync是基于inotify开发的,类似于inotify-tools的工具,Sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录,因此效率更高。

搭建开始:

  •  server端192.168.3.3配置

1.安装rsync

 

yum  -y install  rsync

 

 

 

2.修改配置

 

vim  /etc/rsyncd.conf

uid=tomcat
gid=tomcat
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock

[news]
path=/usr/local/www/cn/news
ignore errors
read only = no
hosts allow = 192.168.2.2/32
hosts deny = *

auth users = tomcat
secrets file = /etc/rsyncd.password               
~                                             

 

 

 

3.创建密码文件

 

vim  /etc/rsyncd.password

tomcat:123456

 

说明:定义格式 用户:密码

4.修改密码文件权限

chmod 600 /etc/rsyncd.password

 

 

5. 创建数据存放目录

mkdir -p  /usr/local/www/cn/news
chown tomcat:tomcat /usr/local/www/cn/news

 

6.启动服务

 rsync --daemon

 

 

说明:如果配置文件路径不是/etc/rsyncd.conf,启动是需要使用“–config” 参数指定配置文件。

7.查看服务是否启动

 ps  axu |grep rsync

netstat -nlp |grep 873

 

 

 

  • client端192.168.2.2配置

1.安装rsync

yum  -y install  rsync

 

2.创建密码文件

vim  /etc/rsyncd.password

123456

 

3.修改密码文件权限

 

chmod 600 /etc/rsyncd.password

 

 

 

4、安装配置sersync

4.1.下载地址

wget  https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz

 

4.2.解压软件包

tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz

 

4.3.创建目录结构

mkdir /usr/local/sersync

mkdir /usr/local/sersync/conf

mkdir /usr/local/sersync/logs

mkdir /usr/local/sersync/bin

 

4.4.移动文件

mv  GNU-Linux-x86/sersync2 /usr/local/sersync/bin/

mv  GNU-Linux-x86/confxml.xml /usr/local/sersync/conf

 

4.5.配置环境变量

source /etc/profile

 

添加配置:

SERSYNC2_HOME=/usr/local/sersync
PATH=$SERSYNC2_HOME/bin:$PATH
export PATH=$PATH:/usr/local/bin

source /etc/profile

 

4.6.配置sersync

cd  /usr/local/sersync/conf

cp confxml.xml confxml.xml.bak

vim  confxml.xml

 

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
   # 设置本地IP和端口
    <host hostip="localhost" port="8008"></host>
   # 开启DUBUG模式 
    <debug start="false"/>
  # 开启xfs文件系统
    <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="false"/>
        <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=" /usr/local/news/"> 
#定义同步Server ip和模块
            <remote ip="192.168.3.3" name="news"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
     <rsync>
# rsync指令参数
            <commonParams params="-artuz"/>
   # rsync同步认证
            <auth start="true" users="tomcat" passwordfile="/etc/rsyncd.password"/>
  # 设置rsync远程服务端口,非默认端口需要打开自定义(若开启rsync+ssh, 则这里需定义SSH端口)
            <userDefinedPort start="true" port="873"/><!-- port=874 -->
# 设置超时时间
            <timeout start="ture" time="100"/><!-- timeout=100 -->
   # 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书
            <ssh start="false"/>
        </rsync>
 # sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。
        <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
   # 设置rsync定时传输,默认关闭
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
   # 设置sersync传输后调用name指定的插件脚本,默认关闭
        <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>
---------------------

4.7 启动sersync

sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml

 4.8 测试

在192.168.2.2 /usr/local/news上用tomcat用户创建测试文件,然后在192.168.3.3/usr/local/www/cn/news中查看是否有该文件同步

 

注意:客户端到server端的873端口必须要通

测试rsync是否生效可用下面命令测试

在客户端使用rsync同步命令,先在192.168.2.2 /usr/local/news目录下创建test.txt文件然后执行命令

rsync -avzP test.txt  tomcat@192.168.3.3::news --password-file=/etc/rsyncd.password

 

posted @ 2018-11-01 11:03  日出东海,我心向西  阅读(408)  评论(0编辑  收藏  举报