场景需求:php
应用程序会在机器192.168.2.2 /usr/local/news目录中生成一些数据文件,如今须要实时同步到主机192.168.3.3/usr/local/www/cn/news中,同时传送的文件过去的权限必须是tomcat权限正则表达式
client端:192.168.2.2express
server端:192.168.3.3vim
操做系统:centos6centos
使用工具sersync rsyncapi
sersync是基于inotify开发的,相似于inotify-tools的工具,Sersync能够记录下被监听目录中发生变化的(包括增长、删除、修改)具体某一个文件或者某一个目录的名字,而后使用rsync同步的时候,只同步发生变化的文件或者目录,所以效率更高。tomcat
搭建开始:dom
1.安装rsyncssh
yum -y install rsync
2.修改配置socket
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
1.安装rsync
yum -y install rsync
2.建立密码文件
vim /etc/rsyncd.password 123456
3.修改密码文件权限
chmod 600 /etc/rsyncd.password
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