sersync 配合rsync實時同步備份
title: sersync 配合rsync實時同步備份
tags: sersync,rsync,同步
grammar_cjkRuby: true
sersync 配合rsync實時同步備份
1. sersync 介紹
sersync是基於Inotify開發的,類似於Inotify-tools的工具,基本上Inotify能實現的功能,sersync也具備,因此,sersync 也可以實時監聽目錄中發生變化的(包括增加、刪除、修改)
但是為什麽我們要使用sersync+rsync呢,它相對於inotify具備什麽優勢呢?Inotify-tools只能記錄下被監聽的目錄發生了變化(包括增加、刪除、修改),並沒有把具體是哪個文件或者哪個目錄發生了變化記錄下來,rsync在同步的時候,並不知道具體是哪個文件或者哪個目錄發生了變化,每次都是對整個目錄進行同步,當數據量很大時,整個目錄同步非常耗時(rsync要對整個目錄遍歷查找對比文件),因此,效率比較低。而sersync可以記錄下被監聽目錄中發生變化的(包括增加、刪除、修改)具體某一個文件或某一個目錄的名字;rsync在同步的時候,只同步發生變化的這個文件或者這個目錄(每次發生變化的數據相對整個同步目錄數據來說是很小的,rsync在遍歷查找比對文件時,速度很快),因此,效率很高。所以當同步的目錄數據量不大時,建議使用Rsync+Inotify-tools;當數據量很大(幾百G甚至1T以上)、文件很多時,建議使用Rsync+sersync。
2. 部署rsync daemon
如圖所示,我們在備份服務器裏搭建一個rsync daemon,並創建/backup用來專門與NFS存儲的數據目錄作為實時同步的遠端目錄,而在對應的NFS存儲中安裝sersync,用來對/data目錄進行動態監控,一旦發現/data目錄有創建,刪除,修改後立刻觸發rsync進行同步推送到備份服務器的/backup裏,從而保證兩端的數據一致性,增強NFS存儲的安全性。
- 創建rsync的配置文件,並將相關參數寫入配置文件,如下所示
[root@backup ~]# touch /etc/rsyncd.conf [root@backup ~]# cat /etc/rsyncd.conf # rsyncd-conf start uid = rsync gid = rsync use chroot = no max connections = 2000 timeout = 600 pid file= /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore = errors read only = false list = false hosts allow = 192.168.50.0/24 hosts deny = 0.0.0.0/32 auth users = rsync-backup secrets file = /etc/rsync.password ##################################### [backup] comment = backup path = /backup
- 創建密碼文件,並設置相關權限,用來和rsync客戶端匹配密碼的。
[root@backup ~]# touch /etc/rsync.password [root@backup ~]# echo "rsync-backup:root" >/etc/rsync.password [root@backup ~]# cat /etc/rsync.password rsync-backup:root [root@backup ~]# chmod 600 /etc/rsync.password [root@backup ~]# ls -l /etc/rsync.password -rw-------. 1 root root 18 May 21 06:23 /etc/rsync.password 這裏需要註意密碼文件必須和配置文件裏的相對應
- 創建rsync用戶,設置為不需要登錄,組也是rsync,需要註意的是這個用戶要和配置文件裏的uid=用戶對應,而auth users = rsync-backup是個虛擬的用戶可以不用管,這可以在一定程度上提高安全性,因為rsync的用戶名不一定需要使用rsync,只要與配置文件裏的uid=XX,對應上就好了。
[root@backup ~]# useradd -g rsync -M -s /sbin/nologin rsync [root@backup ~]# grep rsync /etc/passwd rsync:x:506:506::/home/rsync:/sbin/nologin
- 啟動rsync daemon
[root@backup ~]# rsync --daemon
- 在對應的NFS服務器上也創建密碼文件,用於來和服務端匹配密碼如下:
[root@server data]# echo "root" > /etc/rsync.password [root@server data]# chmod 600 /etc/rsync.password [root@server data]# ls -l /etc/rsync.password -rw-------. 1 root root 5 May 14 20:44 /etc/rsync.password
- 在NFS服務器上推送文件看看是否能成功推送
[root@server data]# echo "hello" >file1 [root@server data]# rsync file1 [email protected]::backup/ --password-file=/etc/rsync.password 在備份服務器查看是否推送成功 [root@backup ~]# cat /backup/file1 hello [root@backup /]# cd /backup &&ls backup file1 [root@backup backup]#
3. 安裝sersync
- 確認linux內核是否支持inotify,如存在以下3個文件說明支持,一般只要內核版本號達到2.6.13的就支持,如:
[root@server ~]# ls -l /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 root root 0 May 21 11:59 max_queued_events -rw-r--r-- 1 root root 0 May 21 11:59 max_user_instances -rw-r--r-- 1 root root 0 May 21 11:59 max_user_watches [root@server ~]# [root@server ~]# uname -r 2.6.32-696.23.1.el6.x86_64
- 查看並優化inotify各項參數
[root@server ~]# cd /proc/sys/fs/inotify/ [root@server inotify]# ls max_queued_events max_user_instances max_user_watches [root@server inotify]# cat max_queued_events 16384 [root@server inotify]# cat max_user_instances 128 [root@server inotify]# cat max_user_watches 8192 [root@server inotify]# [root@server inotify]# echo "50000000" >max_user_instances [root@server inotify]# echo "50000000" >max_user_watches [root@server inotify]# echo "50000000" >max_queued_events [root@server inotify]# ls max_queued_events max_user_instances max_user_watches [root@server inotify]# find ./ -type f |xargs cat 50000000 50000000 50000000 [root@server inotify]# 將參數寫入配置文件/etc/sysctl.conf,如下: [root@server inotify]# echo "fs.inotify.max_queued_events=50000000" >>/etc/sysctl.conf [root@server inotify]# echo "fs.inotify.max_user_watches=50000000" >>/etc/sysctl.conf [root@server inotify]# echo "fs.inotify.max_user_instances=50000000" >>/etc/sysctl.conf [root@server inotify]# tail -3 /etc/sysctl.conf fs.inotify.max_queued_events=50000000 fs.inotify.max_user_watches=50000000 fs.inotify.max_user_instances=50000000 [root@server inotify]#
- 創建/usr/local/src目錄,一般為了規範處理,把所有源安裝包統一放在這個目錄
[root@server ~]# mkdir -p /usr/local/src 上傳sersync2.5.4_64bit_binary_stable_final.tar.gz到/usr/local/src目錄下 [root@server src]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz [root@server local]# mkdir -p /usr/local/sersync/conf [root@server local]# mkdir -p /usr/local/sersync/log [root@server local]# mkdir -p /usr/local/sersync/bin [root@server src]# cd GNU-Linux-x86/ [root@server GNU-Linux-x86]# ls confxml.xml sersync2 [root@server GNU-Linux-x86]# cp -a confxml.xml /usr/local/sersync/conf/ [root@server GNU-Linux-x86]# cp -a sersync2 /usr/local/sersync/bin/
-
修改sersync配置文件
[root@server conf]# cp confxml.xml confxml.xml.bak [root@server conf]# vim 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="true"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="false"/> <modify start="true"/> </inotify> <sersync> <localpath watch="/data" /> <remote ip="192.168.50.4" name="backup"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-avz"/> <auth start="true" users="rsync-backup" passwordfile="/etc/rsync.password"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="true" time="100"/><!-- timeout=100 --> <ssh start="false"/> [6n/rsync> <failLog path="/usr/local/sersync/log/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> 重點修改如下: 1 <sersync> <localpath watch="/data" /> <remote ip="192.168.50.4" name="backup"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> 2 <rsync> <commonParams params="-avz"/> <auth start="true" users="rsync-backup" passwordfile="/etc/rsync.password"/ 3 <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
- 關於sersync的參數說明,以及啟動sersync:
參數-d:啟用守護進程模式
參數-r:在監控前,將監控目錄與遠程主機用rsync命令推送一遍
參數-n: 指定開啟守護線程的數量,默認為10個
參數-o:指定配置文件,默認使用confxml.xml文件
參數-m:單獨啟用其他模塊,使用 -m refreshCDN 開啟刷新CDN模塊
參數-m:單獨啟用其他模塊,使用 -m socket 開啟socket模塊
參數-m:單獨啟用其他模塊,使用 -m http 開啟http模塊
不加-m參數,則默認執行同步程序[root@server conf]# /usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml [root@server conf]# ps -ef |grep sersync2 root 1938 1 0 13:26 ? 00:00:00 /usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
-
對server與backup服務器分別進行驗證,並寫入開機啟動
NFS server端: [root@server data]# for n in `seq 5`;do echo ‘ hello world !‘ >file$n ;done [root@server data]# [root@server data]# ls file1 file2 file3 file4 file5 [root@server data]# find ./ -type f |xargs cat hello world ! hello world ! hello world ! hello world ! hello world ! backup 端: [root@backup backup]# ls file1 file2 file3 file4 file5 [root@backup backup]# find ./ -type f |xargs cat hello world ! hello world ! hello world ! hello world ! hello world ! 將sersync寫入開機啟動: [root@server conf]# echo "/usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml" >>/etc/rc.local [root@server conf]# tail -1 /etc/rc.local /usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
- 添加腳本監控sersync是否正常運行
[root@server scripts]# vim check_sersync.sh #!/usr/bin/env bash ############################################################## # File Name: check_sersync.sh # Version: V1.0 # Author: OuYoung # Blog: http://blog.51cto.com/ouyangtao # Created Time : 2018-05-21 13:50:38 # Description: ############################################################## sersync="/usr/local/sersync/bin/sersync2" config="/usr/local/sersync/conf/confxml.xml" status=$(ps aux |grep "sersync2" |grep -v "grep" |wc -l) if [ $status -eq 0 ]; then $sersync -d -r -o $config >/dev/null else exit 0 ; fi
- 將監控腳本寫入crontab裏,並設定5分鐘執行一次
[root@server scripts]# echo "*/5 * * * * sh /service/scripts/check_sersync.sh >/dev/null" >>/var/spool/cron/root [root@server scripts]# cat /var/spool/cron/root #定時時鐘同步每20分鐘同步一次 */20 * * * * ntpdate 1.cn.pool.ntp.org # * * * * * bash */5 * * * * sh /service/scripts/check_sersync.sh >/dev/null
- 重新載入crond服務,並先把sersync2進程kill掉,待5分鐘後看服務是否重新起來,如:
[root@server scripts]# /etc/init.d/crond reload 重新載入 crond: [root@server scripts]# pkill sersync2 && date 2018年 05月 21日 星期一 14:22:20 CST [root@server scripts]# ps -ef |grep sersync2 && uptime root 2352 1801 0 14:24 pts/0 00:00:00 grep --color=auto sersync2 14:24:33 up 2:27, 2 users, load average: 0.00, 0.00, 0.00 [root@server scripts]# ps -ef |grep sersync2 && uptime root 2366 1 0 14:25 ? 00:00:00 /usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml root 2382 1801 0 14:25 pts/0 00:00:00 grep --color=auto sersync2 14:25:04 up 2:28, 2 users, load average: 0.00, 0.00, 0.00 [root@server scripts]#
sersync 配合rsync實時同步備份