sersync:
??sersync類似于inotify,同樣用于監控,但它克服了inotify的缺點.
inotify+rsync 缺點:
??一個操作可能會產生重復的事件,這樣可能會觸發rsync的多次同步,
serync特點:
??基于inotify基礎上開發而來的,
sersync的作用:
??用來替代inotify+resync的腳本檔案,
-
會對對linux系統檔案系統產生的臨時檔案和重復的檔案操作進行過濾,在結合rsync同步的時候,節省了運行時耗和網路資源
-
配置簡單,提供了要給xml組態檔和一個二進制可執行檔案
-
采用多執行緒模式
-
自帶crontab共呢個
sersync專案地址: https://code.google.com/archive/p/sersync/
sersync下載地址: https://code.google.com/archive/p/sersync/downloads
sersync組態檔說明:
<?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"> #不開啟檔案過濾功能,當為true時,以下型別的檔案將不同步
<exclude expression="(.*)\.svn"></exclude> #過濾特定后綴的檔案
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify> # 監控事件,表示inotify的監控事件 sersync背后呼叫的就是inotify,
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/> #默認為false,修改此行為true,檔案屬性變化后也會觸發同步,
<modify start="false"/>
</inotify>
<sersync> # rsync命令的配置段,sersync背后會嗲用rsync工具
<localpath watch="/data/www"> #修改此行,設定需要同步的源目錄或檔案,建議同步目錄 后面會監控這個目錄的變化
<remote ip="10.0.0.12" name="backup"/> #修改此行,指定備份服務器地址和rsync daemon的 共享模塊名(相當于實際目錄的別名) ,如果下面開啟了ssh start,此時name為遠程shell方式運行時的目標目錄
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/> # sersync是基于rsync同步的,這里是指定rsync選項
<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pas"/> #默認為false,表示驗證之后才同步,改此行為true,指定備份服務器的rsync配置的用戶和密碼檔案
<userDefinedPort start="false" port="874"/><!-- port=874 --> #指定rsync的非標準埠號
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/> #默認使用rsync daemon運行rsync命令,true為使用遠程shell模式
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #錯誤重傳及日志檔案路徑
<crontab start="false" schedule="600"><!--600mins--> #不開啟crontab功能
<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>
范例:基于rsync daemon 實作 sersync
前提:
??使用rsync配置備份服務器為rsync服務器
??https://www.cnblogs.com/heyongshen/p/16822630.html
資料服務器的相關配置
#在資料服務器上下載sersync,并拷貝至相應的目錄,設定PATH變數
#下載sersync工具并解壓到指定目錄
[root@data-centos8 ~]#tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@data-centos8 ~]#cp -a GNU-Linux-x86 /usr/local/sersync
[root@data-centos8 ~]#echo 'PATH=/usr/local/sersync:$PATH' > /etc/profile.d/sersync.sh
[root@data-centos8 ~]#source /etc/profile.d/sersync.sh
#sersync目錄只有兩個檔案:一個是二進制程式檔案,一個是xml格式的組態檔
[root@data-centos8 ~]#ls /usr/local/sersync/confxml.xml sersync2
#安裝rsync工具
#備份sersync組態檔
[root@data-centos8 ~]#cp /usr/local/sersync/confxml.xml{,.bak}
#修改sersync組態檔
[root@CentOS8 www]# cat /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="/data/www">
<remote ip="10.0.0.12" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pas"/>
<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>
#創建連接rsynd服務器的用戶密碼檔案,并必須修改權限
[root@data-centos8 ~]#echo magedu > /etc/rsync.pas
[root@data-centos8 ~]#chmod 600 /etc/rsync.pas #必須要修改密碼檔案的權限,不然會報錯
#查看幫助
[root@data-centos8 ~]#sersync2 -h
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
_______________________________________________________
引數-d:啟用守護行程模式
引數-r:在監控前,將監控目錄與遠程主機用rsync命令推送一遍
c引數-n: 指定開啟守護執行緒的數量,默認為10個
引數-o:指定組態檔,默認使用當前作業目錄下的confxml.xml檔案
引數-m:單獨啟用其他模塊,使用 -m refreshCDN 開啟重繪CDN模塊
引數-m:單獨啟用其他模塊,使用 -m socket 開啟socket模塊
引數-m:單獨啟用其他模塊,使用 -m http 開啟http模塊
不加-m引數,則默認執行同步程式
#以后臺方式執行同步
[root@data-centos8 ~]#sersync2 -dro /usr/local/sersync/confxml.xml #d:守護行程模式 r:剛開始就先同步一次 o:指定組態檔的路徑
#sersync支持多實體,也即監控多個目錄時,只需分別配置不同組態檔,然后使用sersync2指定對應配置
檔案運行
基于遠程shell 實作 sersync
??不需要配置rsync daemon,只需要配置基于key驗證的ssh即可
[root@data_server ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:VUxYvaSINtyfERTWH6bpMrrFeP7pfLo6+RwLU/LJvqY [email protected]
The key's randomart image is:
+---[RSA 3072]----+
| *B+ |
| .oo +o |
| . o.. ++o.|
| =.o oo. .|
| .S. o.+ |
| ooB.. |
| ..*+= |
| .+o*.+. |
| ..EO&= |
+----[SHA256]-----+
[root@data_server ~]# ssh-copy-id 10.0.0.12
# 不需要配置rsync daemon,只需要配置基于key驗證的ssh即可
[root@data-centos8 ~]#ssh-keygen
[root@data-centos8 ~]#ssh-copy-id 10.0.0.12
# 下載sersync,并拷貝至相應的目錄,設定PATH變數
# 修改sersync組態檔
[root@data-centos8 ~]#cat /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="true"/> # 修改此行為true,表示保留檔案屬性
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data/www"> # 修改此行,指定源資料目錄
<remote ip="備份服務器IP" name="/data/backup"/> # 修改此行指定備份服務器地址和備份目標目錄 這里需要填寫真實路徑
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/> # 必須修改此行,表示不啟用認證,改為false
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="true"/> # 修改此行為true,使用遠程shell方式的rsync連接方式,無需在目標主機上配置啟動rsync daemon服務
#####################################以下行不需要修改####################################
</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>
# 將中間的行可以洗掉
</head>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/519221.html
標籤:其他
上一篇:使用dism從命令列安裝IIS
