一、sersync是基於inotify開發的,相似於inotify-tools的工具php
二、sersync能夠記錄下被監聽目錄中發生變化的(包括增長、刪除、修改)具體某一個文件或者某一個目錄的名字,而後使用rsync同步的時候,只同步發生變化的文件或者目錄 shell
一、rsync+inotify-tools
a、inotify只能記錄下被監聽的目錄發生了變化(增,刪,改)並無把具體是哪一個文件或者哪一個目錄發生了變化記錄下來;
b、rsync在同步的時候,並不知道具體是哪一個文件或目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步很是耗時(rsync要對整個目錄遍歷查找對比文件),所以效率很低
二、rsync+sersync
a、sersync能夠記錄被監聽目錄中發生變化的(增,刪,改)具體某個文件或目錄的名字;
b、rsync在同步時,只同步發生變化的文件或目錄(每次發生變化的數據相對整個同步目錄數據來講很小,rsync在遍歷查找對比文件時,速度很快),所以效率很高。
同步過程:
1. 在同步服務器上開啓sersync服務,sersync負責監控配置路徑中的文件系統事件變化;
2. 調用rsync命令把更新的文件同步到目標服務器;
3. 須要在主服務器配置sersync,在同步目標服務器配置rsync server(注意:是rsync服務)
同步過程和原理:
1. 用戶實時的往sersync服務器上寫入更新文件數據;
2. 此時須要在同步主服務器上配置sersync服務;
3. 在另外一臺服務器開啓rsync守護進程服務,以同步拉取來自sersync服務器上的數據;
經過rsync的守護進程服務後能夠發現,實際上sersync就是監控本地的數據寫入或更新事件;而後,在調用rsync客戶端的命令,將寫入或更新事件對應的文件經過rsync推送到目標服務器express
sersync是須要備份的服務器,備份目錄/data,IP:192.168.5.80vim
rsync服務器做爲備份服務器,存儲目錄/backup,IP:192.168.5.82bash
3.1 在192.168.5.82上對rsync服務進行安裝配置服務器
# yum install rsync -y # useradd rsync -s /sbin/nologin -M # mkdir /backup # chown -R rsync /backup # echo "rsync_backup:123456" > /etc/rsync.password # chmod 600 /etc/rsync.password # cat /etc/rsyncd.conf uid=rsync gid=rsync port=873 use chroot=no max connections= 10 ignore errors pid file=/var/run/rsyncd.pid lock file=/var/run/rsync.lock log file=/var/log/rsyncd.log list = false auth users=rsync_backup # 不是系統用戶,不須要提早建立 只是用來同步數據 secrets file = /etc/rsync.password [backup] path=/backup read only=false hosts allow = 192.168.5.0/24 hosts deny = 0.0.0.0/32 # systemctl start rsyncd # systemctl enable rsyncd
3.2 在192.168.5.80上對sersync服務進行安裝配置架構
# yum install rsync -y 上傳軟件包,可經過此地址下載 # tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/ # mv /usr/local/GNU-Linux-x86 /usr/local/sersync # echo '123456' >/etc/rsync.password # chmod 600 /etc/rsync.password # vim /usr/local/sersync/confxml.xml <?xml version="1.0" encoding="ISO-8859-1"?> <head version="2.5"> <host hostip="localhost" port="8008"></host> # 設置本地IP和端口 <debug start="false"/> # 是否開啓調試模式,下面全部出現false和true的地方都分別表示關閉和開啓的開關 <fileSystem xfs="false"/> # 監控的是不是xfs文件系統 <filter start="false"> # 是否啓用監控的篩選功能,篩選的文件將不被監控,默認關閉 <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude> <exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> <inotify> # 設置監控的事件,默認監控的是delete/close_write/moved_from/moved_to/create folder <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> # rsync命令的配置段 <localpath watch="/data"> # 本地監控同步的目錄或文件,同inotify+rsync同樣,建議同步目錄 <remote ip="192.168.5.82" name="backup"/> # 目標地址和rsync daemon的模塊名,因此遠端要以daemon模式先運行好rsync <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> # 指定rsync選項 <commonParams params="-artuz"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> # 指定rsync密碼認證選項 <userDefinedPort start="false" port="874"/><!-- port=874 --> # 設置rsync遠程服務端口 <timeout start="false" time="100"/><!-- timeout=100 --> # 設置超時時間 <ssh start="false"/> # 是否使用遠程shell模式而非rsync daemon運行rsync命令 </rsync> <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> # 錯誤重傳。sersync傳輸失敗日誌腳本路徑,每隔60會從新執行該腳本,執行完畢會自動清空。 <crontab start="false" schedule="600"><!--600mins--> <crontabfilter start="false"> # 是否開啓crontab功能,默認關閉 <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> # 啓動服務,啓動正常後文件直接同步 # echo '/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml' >> /etc/rc.local # chmod +x /etc/rc.local # /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml set the system param execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events parse the command param option: -d run as a daemon option: -r rsync all the local files to the remote servers before the sersync work option: -o config xml name: /usr/local/sersync/confxml.xml daemon thread num: 10 parse xml config file host ip : localhost host port: 8008 daemon start,sersync run behind the console use rsync password-file : user is rsync_backup passwordfile is /etc/rsync.password config xml parse success please set /etc/rsyncd.conf max connections=0 Manually sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) please according your cpu ,use -n param to adjust the cpu rate ------------------------------------------ rsync the directory recursivly to the remote servers once working please wait... execute command: cd /data && rsync -artuz -R --delete ./ rsync_backup@192.168.5.82::backup --password-file=/etc/rsync.password >/dev/null 2>&1 run the sersync: watch path is: /data
3.3 本地監控腳本dom
# cat /usr/local/sersync/check_sersync.sh #!/bin/sh sersync="/usr/local/sersync/sersync2" confxml="/usr/local/sersync/confxml.xml" status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l) if [ $status -eq 0 ]; then $sersync -dro $confxml & else exit 0; fi # echo "*/5 * * * * /bin/bash /usr/local/sersync/check_sersync.sh 2>&1 >/dev/null" > /var/spool/cron/root