sersync+rsync實現服務器文件實時同步
一、為什么要用rsync+sersync架構?
1、sersync是基於inotify開發的,類似於inotify-tools的工具 2、sersync可以記錄下被監聽目錄中發生變化的(包括增加、刪除、修改)具體某一個文件或者某一個目錄的名字,然后使用rsync同步的時候,只同步發生變化的文件或者目錄
二、rsync+inotify-tools與rsync+sersync架構的區別?
1、rsync+inotify-tools a、inotify只能記錄下被監聽的目錄發生了變化(增,刪,改)並沒有把具體是哪個文件或者哪個目錄發生了變化記錄下來; b、rsync在同步的時候,並不知道具體是哪個文件或目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步非常耗時(rsync要對整個目錄遍歷查找對比文件),因此效率很低 2、rsync+sersync a、sersync可以記錄被監聽目錄中發生變化的(增,刪,改)具體某個文件或目錄的名字; b、rsync在同步時,只同步發生變化的文件或目錄(每次發生變化的數據相對整個同步目錄數據來說很小,rsync在遍歷查找對比文件時,速度很快),因此效率很高。 總結: 當同步的目錄數據量不大時,建議使用rsync+inotify 當同步的目錄數據量很大時(幾百G甚至1T以上)文件很多時,建議使用rsync+sersync
架構:
1.部署rsync服務(rsync-server服務器上配置)
yum install rsync -y #安裝rsync,如果嫌yum版本過低也可以源碼安裝
2.vim /etc/rsyncd.conf #默認rsync沒有配置文件,創建一個,文件中#和漢字僅為注釋,使用中請將所有注釋清除
#Rsync server uid = root gid = root use chroot = no # 安全相關 max connections = 2000 # 並發連接數 timeout = 600 # 超時時間(秒) pid file =/var/run/rsyncd.pid # 指定rsync的pid目錄 lock file =/var/run/rsync.lock # 指定rsync的鎖文件【重要】 log file = /var/log/rsyncd.log # 指定rsync的日志目錄 ignore errors #忽略一些I/O錯誤 read only = false #設置rsync服務端文件為讀寫權限 list = false #不顯示rsync服務端資源列表 hosts allow = 172.16.0.0/16 #允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開 hosts deny = 0.0.0.0/32 #禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開 auth users = rsync_backup #執行數據同步的用戶名,可以設置多個,用英文狀態下逗號隔開 secrets file =/etc/rsync.password #用戶認證配置文件,里面保存用戶名稱和密碼 ################################################# [www] # 模塊 comment = www path = /data/www/ ################################################# [bbs] comment = bbs path = /data/bbs/ ################################################# [blog] comment = blog path = /data/blog/ #rsync_config____________end :wq! #保存,退出
3、創建用戶認證文件
echo "rsync_backup:123456">/etc/rsync.password #配置文件,添加以下內容
4、設置文件權限
chmod 600 /etc/rsync.password
5.啟動守護進程,並寫入開機自啟動
rsync --daemon #可以使用--config= 指定非標准路徑下的配置文件 vim /etc/rc.local # rsync server progress /usr/bin/rsync --daemon
6.創建相關待同步的目錄
mkdir -p /data/{www,bbs,blog}
#rsync-client客戶端配置
1.安裝rsync,方法同上
2.創建rsync配置文件,客戶端創建即可,無需內容
touch /etc/rsyncd.conf
3.配置rsync客戶端相關權限認證:
echo "123456">/etc/rsync.password chmod 600 /etc/rsync.password
4.創建待同步數據,在客戶端創建一些數據
mkdir -p /data/{www,bbs,blog} touch /data/www/www.log /data/bbs/bbs.log /data/blog/blog.log
5.測試rsync是否同步
[root@rsync-client www]# touch {1..3}.txt [root@rsync-client www]# rsync -avzP /data/www/ rsync_backup@172.16.150.131::www/ --password-file=/etc/rsync.password #/data/www/表示本地需要同步的數據目錄 rsync_backup@172.16.150.131::www表示服務端的指定名稱的模塊下 本條命令執行的操作為:將第一個路徑參數下的文件同步到第二個路徑參數下 即:推模式 調換路徑則為:拉模式 sending incremental file list ./ 1.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=2/4) 2.txt 0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=1/4) 3.txt 0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=0/4) sent 207 bytes received 84 bytes 582.00 bytes/sec total size is 0 speedup is 0.00 #rsync -avzP /data/www rsync://rsync_backup@172.16.150.131/data/www --password-file=/etc/rsync.password 第二種方法,直接指定路徑 #rsync -avzP /data/www rsync://rsync_backup@172.16.150.131/data/www/ --password-file=/etc/rsync.password #test為服務器上的目錄 參數: --delete 無差異同步 --bwlimit=KB/S 限速 --exclude=PATTERN exclude files matching PATTERN --exclude-from=FILE read exclude patterns from FILE --include=PATTERN don’t exclude files matching PATTERN --include-from=FILE read include patterns from FILE #此步驟必須成功才能進行下一步
6.開始部署sersync服務(rsync-server服務器上)
tar fxzsersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/ #百度網盤鏈接: https://pan.baidu.com/s/11yL6HtZsblkZR8vSSIvzrw 提取碼: tdam cd /usr/local/ mv GNU-Linux-x86 sersync
7.配置sersync
cp sersync/confxml.xml sersync/confxml.xml-bak vim sersync/confxml.xml 修改24--28行 24 <localpath watch="/opt/tongbu">
25 <remote ip="127.0.0.1" name="tongbu1"/>
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath> 修改后的內容為: 24 <localpath watch="/data/www">
25 <remote ip="10.1.20.109" name="www"/>
26 </localpath> 修改29--35行,認證部分(rsync密碼認證) 29 <rsync>
30 <commonParams params="-artuz"/>
31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="false" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync> 修改后的內容如下: 27 <rsync>
28 <commonParams params="-artuz"/>
29 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
30 <userDefinedPort start="false" port="874"/><!-- port=874 -->
31 <timeout start="true" time="100"/><!-- timeout=100 -->
32 <ssh start="false"/>
33 </rsync>
8.開啟sersync守護進程同步數據
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml 配置sersync環境變量 echo"PATH=$PATH:/usr/local/sersync/">>/etc/profile source /etc/profile
啟動命令后返回結果如下為正常:
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: ./confxml.xml daemon thread num: 10 parse xml config file XML Parsing error inside file './confxml.xml'. Error: File not found At line 0, column 0.
同步測試
[root@panwenbin1-sa GNU-Linux-x86]# ./sersync2 -d -r -o ./confxml-www.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: ./confxml-www.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/www && rsync -artuz -R --delete ./ --timeout=100 rsync_backup@10.1.20.109::www --password-file=/etc/rsync.password >/dev/null 2>&1 run the sersync: watch path is: /data/www
9.多實例情況
1、配置多個confxml.xml文件(比如:www、bbs、blog....等等) confxml-bbs.xml confxml-blog.xml confxml-www.xml(按照單個實例配置即可) 2、根據不同的需求同步對應的實例文件 rsync -avzP /data/www/ rsync_backup@10.1.20.109::www/ --password-file=/etc/rsync.password rsync -avzP /data/bbs/ rsync_backup@10.1.20.109::bbs/ --password-file=/etc/rsync.password rsync -avzP /data/test/ rsync_backup@10.1.20.109::blog/ --password-file=/etc/rsync.passwor 分別啟動即可
rsync缺點:
1.大量小文件同步時,比對時間長,有時候rsync進程會停止
2.同步大文件,10G這樣的大文件有時也會有問題,會中斷。未完整同步之前是隱藏文件,可通過參數續傳