場景需求: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