rsync+inotify實時同步目錄數據

rsync+inotify實時同步目錄數據html

爲何使用rsync+inotifylinux

  配合rsync實現文件自動同步,利用inofity監聽某個目錄,當此目錄發生變化時,觸發rsync實現文件同步nginx

  單獨rsycn同步數據時,rsync先須要掃描全部文件進行對比,進行差量傳輸。在數據達到百萬級別時,掃描文件時很是耗時的,每每就出現了瓶頸。c++

  rsync不能實時同步數據,並且觸發同步數據時也會有時間差,形成數據不一致等狀況。git

rsync同步命令中經常使用的幾個參數說明:
-a, --archive 歸檔模式,表示以遞歸方式傳輸文件,並保持全部文件屬性,等於-rlptgoD
-v, --verbose 詳細模式輸出
-p, --perms 保持文件權限
-g, --group 保持文件屬組信息
-o, --owner 保持文件屬主信息
-r, --recursive 對子目錄以遞歸模式處理。同步目錄的時候要加上這個參數
-l, --links 保留軟鏈結,加上這個參數,同步過來的文件會保持以前的軟連接屬性不變
-H, --hard-links 保留硬鏈結
-e, --rsh=COMMAND 指定使用rsh、ssh方式進行數據同步
-z, --compress 對備份的文件在傳輸時進行壓縮處理
--stats 給出某些文件的傳輸狀態
--progress 打印同步的過程
--timeout=TIME 同步過程當中,IP超時時間,單位爲秒
--delete 刪除那些目標目錄中有而源目錄中沒有的多餘文件。這個是rsync作增量方式的全備份的最佳選擇方案!!!!!!
--delete-before 接受者在輸出以前進行刪除操做。即先將目標目錄中文件所有刪除,再將源目錄文件拷貝過去。這是rsync保持目標目錄跟源目錄一致的方案!!!
--delete-after 在同步操做以後作比較,刪除那些目標目錄中有而源目錄中沒有的多餘文件
--delete-excluded 刪除目標目錄中那些被該選項指定排除的文件
--ignore-errors 即便出現IO錯誤也進行刪除,忽略錯誤
--exclude 指定同步時須要過濾掉的文件或子目錄(即不須要同步過去的),後面直接跟不須要同步的單個文件名或子目錄(不須要跟路徑) ,過濾多個文件或子目錄,就使用多個--exclude
--exclude-from 指定同步時須要過濾掉的文件或子目錄,後面跟文件(好比/root/exclue.txt),而後將不須要同步的文件和子目錄放到/root/exclue.txt下。
--version 打印版本信息
--port=PORT 指定其餘的rsync服務端口
--log-format=formAT 指定日誌文件格式
--password-file=FILE 從FILE中獲得密碼
--bwlimit=KBPS 限制I/O帶寬,KBytes per second

 

案例說明:github

  機器vim

  client  192.168.2.200centos

  client  192.168.2.202安全

  server  192.168.2.201bash

  須要將192.168.2.200 /data目錄 和 192.168.2.202 /test目錄實時同步到192.168.2.201 /home/backup對應的 data和test目錄

 

1、server部署過程

  一、部署前操做

[root@cc ~]# vim /etc/sysconfig/selinux	#關閉selinux
SELINUX=disabled
[root@cc ~]# setenforce 0
setenforce: SELinux is disabled

#容許22號端口和873端口進來
[root@cc ~]# iptables -I INPUT -s 192.168.2.200 -p tcp --dport 22 -j ACCEPT
[root@cc ~]# iptables -I INPUT -s 192.168.2.200 -p tcp --dport 873 -j ACCEPT
[root@cc ~]# iptables -I INPUT -s 192.168.2.202 -p tcp --dport 22 -j ACCEPT
[root@cc ~]# iptables -I INPUT -s 192.168.2.202 -p tcp --dport 873 -j ACCEPT

   二、安裝部署rsync服務

[root@cc ~]# yum -y install rsync xinetd
[root@cc ~]# systemctl enable rsyncd	#開機自啓
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

[root@cc ~]# systemctl restart rsyncd
[root@cc ~]# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log  #日誌存放路徑,無需手動建立
pidfile = /var/run/rsyncd.pid   #pid文件存放路徑
lock file = /var/run/rsyncd.lock        #支持max connections參數的鎖文件
secrets file = /etc/rsync.pass  #用戶認證配置文件,裏面保存用戶名稱和密碼,後面須要建立
motd file = /etc/rsyncd.Motd    #rsync啓動時歡迎信息頁面配置文件(內容本身定義)

[data]  #名稱自定義
path = /home/backup/data        #rsync服務端數據目錄路徑,即同步到目標目錄後存放路徑
comment = data  #模塊名稱和[data]自定義名稱同樣
uid = nginx     #設置rsync運行的uid權限,要保證同步到目標目錄後的權限和源目錄一致,即都是nginx
gid = nginx     #gid權限
port = 873      #默認rsnyc端口
use chroot = no #默認爲ture,改成no或者false,增長對目錄文件軟鏈接的備份
read only = no  #設置rsync服務器文件爲讀寫權限
list = no       #不顯示rsnyc服務器資源列表
max connections = 200   #最大連接數
timeout = 600   #設置超時時間
auth users = RSYNC_USER #執行數據同步的用戶名,後面手動設置。能夠設置多個,用,隔開
hosts allow = 192.168.2.200     #容許進行數據同步的客戶端ip地址,能夠設置多個,用,隔開
#hosts deny = 192.168.2.200     #禁止數據同步的客戶端ip地址,能夠設置多個,用,隔開,若是沒有,則不用設置此行

[test]
path = /home/backup/test	###centos6能夠不寫後面的test,若是寫了到時候同步的路徑爲/home/backup/test/test
comment = test
uid = root
gid = root
port = 873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = RSYNC_USER
hosts allow = 192.168.2.202

   三、設置密碼文件以及修改權限

[root@cc ~]# cat /etc/rsync.pass 	#用戶認證文件
RSYNC_USER:123456@rsync

[root@cc ~]# chmod 600 /etc/rsyncd.conf	#設置文件權限
[root@cc ~]# chmod 600 /etc/rsync.pass

   四、建立同步過來目錄

[root@cc ~]# systemctl restart rsyncd	#重啓服務
[root@cc ~]# netstat -tunlp | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      6429/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      6429/rsync

[root@cc ~]# cd /home/backup/	#建立同步過來後的目標目錄
[root@cc backup]# mkdir test
[root@cc backup]# mkdir data
[root@cc backup]# chown nginx:nginx data -R
[root@cc backup]# chown test:test test -R

 

2、server部署過程

  2臺機器操做同樣

  一、部署前準備工做

[root@1ogg2zbl ~]# vim /etc/sysconfig/selinux	#關閉selinux
SELINUX=disabled
[root@1ogg2zbl ~]# setenforce 0
setenforce: SELinux is disabled

[root@1ogg2zbl ~]# yum -y install rsync xinetd

 

   二、啓動rsync以及配置認證密碼

[root@1ogg2zbl ~]# vim /etc/xinetd.d/rsync
disable = no	#由默認的yes改成no,設置開機啓動rsync

[root@1ogg2zbl ~]# /etc/init.d/xinetd restart
Stopping xinetd:                                           [FAILED]
Starting xinetd:                                           [  OK  ]
[root@1ogg2zbl ~]# netstat -tunlp | grep 873
tcp        0      0 :::873                      :::*                        LISTEN      1959/xinetd

[root@1ogg2zbl ~]# vim /etc/rsync.pass	#認證文件
123456@rsync
[root@1ogg2zbl ~]# chmod 600 /etc/rsync.pass	#設置權限

  三、查看機器是否支持inofity

[root@1ogg2zbl ~]# ll /proc/sys/fs/inotify/	#出現下列內容,說明服務器內核支持inofity 注:linux下inofity內核最小爲2.6.13
total 0
-rw-r--r-- 1 root root 0 Nov 21 14:52 max_queued_events
-rw-r--r-- 1 root root 0 Nov 21 14:52 max_user_instances
-rw-r--r-- 1 root root 0 Nov 21 14:52 max_user_watches

   四、部署inofity

[root@1ogg2zbl src]# cd /usr/local/src/
[root@1ogg2zbl src]# yum -y install make g1ogg2zbl g1ogg2zbl-c++ wget
[root@1ogg2zbl src]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@1ogg2zbl src]# tar -xf inotify-tools-3.14.tar.gz 
[root@1ogg2zbl src]# cd inotify-tools-3.14
[root@1ogg2zbl inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@1ogg2zbl inotify-tools-3.14]# make && make install

[root@1ogg2zbl inotify-tools-3.14]# vim /etc/profile	#設置環境變量
export export PATH=$PATH:/usr/local/inotify/bin
[root@1ogg2zbl inotify-tools-3.14]# source  /etc/profile
[root@1ogg2zbl inotify-tools-3.14]# vim /etc/ld.so.conf
/usr/local/inotify/lib
[root@1ogg2zbl inotify-tools-3.14]# ldconfig

[root@1ogg2zbl inotify-tools-3.14]# sysctl -a | grep max_queued_events	#查看以及修改默認inofity參數大小
fs.inotify.max_queued_events = 16384
[root@1ogg2zbl inotify-tools-3.14]# sysctl -a | grep max_user_watches
fs.inotify.max_user_watches = 8192
fs.epoll.max_user_watches = 798556
[root@1ogg2zbl inotify-tools-3.14]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128
[root@1ogg2zbl inotify-tools-3.14]# sysctl -w fs.inotify.max_queued_events="99999999"	#inotify隊列最大長度,若是值過小,會出現"** Event Queue Overflow **"錯誤,致使監控文件不許確
fs.inotify.max_queued_events = 99999999
[root@1ogg2zbl inotify-tools-3.14]# sysctl -w fs.inotify.max_user_watches="99999999"	#要同步的文件包含多少目錄,能夠用:find /data -type d | wc -l 統計這些源目錄下的目錄數,必須保證max_user_watches值大於統計結果(這裏/data爲同步的源文件目錄)
fs.inotify.max_user_watches = 99999999
[root@1ogg2zbl inotify-tools-3.14]# sysctl -w fs.inotify.max_user_instances="65535"	#每一個用戶建立inotify實例最大值
fs.inotify.max_user_instances = 65535

   五、開始同步

##第一次執行rsync全量同步(加上--delete參數,保持目標目錄和源目錄文件一致)
[root@cc /]# rsync -avH --port=873 --progress --delete /test RSYNC_USER@192.168.2.201::test --password-file=/etc/rsync.pass

sending incremental file list
test/
test/aa
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/2)

sent 105 bytes  received 31 bytes  272.00 bytes/sec
total size is 0  speedup is 0.00

##接下來使用rsync+inofity實時同步腳本操做
##實時同步腳本里添加的是--delete-before參數,而不是--delete參數(第一次全量同步時rsync用的參數),兩者區別:
##	--delete參數:表示rsync同步前,暴力刪除目標目錄中的全部文件,而後再執行同步操做。
##	--delete-before參數:表示rsync同步前,會先對目標目錄進行一次掃描檢索,刪除目標目錄中對比源目錄的多餘文件,而後再執行同步操做。顯然比--delete參數安全些。

[root@cc /]# useradd rsync
[root@cc /]# cd /home/rsync/
[root@cc rsync]# ll
total 0
[root@cc rsync]# cat rsync_test_inotify.sh 
#!/bin/bash

SRCDIR=/$1
USER=RSYNC_USER
IP=192.168.2.201
DESTDIR=$1

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $SRCDIR | while read file
do
	/usr/bin/rsync -avH --port=873 --progress --delete-before /${SRCDIR} RSYNC_USER@192.168.2.201::${DESTDIR} --password-file=/etc/rsync.pass
	echo "${file} was rsyncd" >> /tmp/rsync.log 2>&1
done

[root@cc test]# nohup sh /home/rsync/rsync_test_inotify.sh test &	#後臺執行

[root@cc test]# su - test	#測試
su: warning: cannot change directory to /home/test: Permission denied
-bash: /home/test/.bash_profile: Permission denied
-bash-4.1$ cd /test/
-bash-4.1$ mkdir ggg
-bash-4.1$ touch nnn

[root@cc backup]# ll /home/backup/test/test/
total 48
drwxrwxr-x 2 test test  4096 Nov 21 16:02 ggg
-rw-rw-r-- 1 test test     0 Nov 21 16:02 nnn
相關文章
相關標籤/搜索