rsync+inotify實現數據同步

rsync:Remote Sync,是類Unix系統下的數據鏡像備份工具。通過rsync可以解決對實時性要求不高的數據進行備份需求;例如:指定的備份文件服務器數據到指定的遠端服務器,對本地磁盤定期做數據鏡像等。

inotify:inotify是一種文件變化通知機制;通過inotify可以在監控文件系統中添加、刪除、修改、移動等各種操作


準備環境:

主服務器(inotify-Master)
IP:172.18.42.201
從服務器(inotify-Slave)
IP:172.18.42.200


一、部署inotify-Slave

1、安裝rsync程序

[root@inotify-slave ~]# yum install rsync

2、創建rsync用戶

[root@inotify-slave ~]# useradd -s /bin/nologin -M rsync

3、創建rsync工作模塊的目錄

[root@inotify-slave ~]# mkdir /mydata/   ##創建rsync工作模式的模塊目錄
[root@inotify-slave ~]# chown rsync.rsync /mydata/   ##更改屬主、主組;使rsync用戶有權限更改數據
[root@inotify-slave ~]# ls -ld /mydata/
drwxr-xr-x 2 rsync rsync 6 May 19 21:02 /mydata/

4、配置虛擬用戶的密碼文件/etc/rsync.password

[root@inotify-slave ~]# vim /etc/rsync.password
lweim:linux   ##“lweim”爲虛擬用戶用戶名;“linux”爲虛擬用戶密碼
[root@inotify-slave ~]# chmod 600 /etc/rsync.password    ##爲密碼文件增加安全性
[root@inotify-slave ~]# ll /etc/rsync.password 
-rw------- 1 root root 12 May 18 21:54 /etc/rsync.password

5、配置rsync配置文件/etc/rsyncd.conf

[root@inotify-slave ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
user chroot = no
max connections = 200
timeout = 300
read only = no
[rsync]    ##定義模塊名,名字可隨意
path = /mydata   ##指定rsync用戶的模塊目錄
auth users = lweim   ##指定虛擬用戶名
secrets file = /etc/rsync.password   ##指定虛擬用戶的密碼文件路徑
ignore errors   ##表示忽略錯誤

6、啓動rsync服務

[root@inotify-slave ~]# rsync --daemon --config=/etc/rsyncd.conf    ##rsync監聽在tcp協議的873端口
[root@inotify-slave ~]# ps aux | grep rsync
root       1330  0.0  0.0 114640   328 ?        Ss   21:13   0:00 rsync --daemon --config=/etc/rsyncd.conf
root       1338  0.0  0.1 112644   952 pts/0    R+   21:13   0:00 grep --color=auto rsync


二、配置inotify-master的密碼文件

1、配置虛擬用戶的密碼文件

[root@inotify-master ~]# echo "linux" > /etc/rsync.password    
[root@inotify-master ~]# cat /etc/rsync.password
linux     ##注意:只需要寫出虛擬用戶的密碼;不用寫出用戶名
[root@inotify-master ~]# chmod 600 /etc/rsync.password    
[root@inotify-master ~]# ls -ld /etc/rsync.password
-rw------- 1 root root 6 May 19 21:18 /etc/rsync.password


三、通過inotify-master測試推送文件

[root@inotify-master ~]# echo "Hello Word" > lweim.txt
[root@inotify-master ~]# rsync -avz lweim.txt [email protected]::rsync --password-file=/etc/rsync.password
sending incremental file list
lweim.txt

sent 81 bytes  received 27 bytes  216.00 bytes/sec
total size is 11  speedup is 0.10


四、檢查inotify-slave的工作模塊目錄

[root@inotify-slave ~]# ll /mydata/
total 4
-rw-r--r-- 1 rsync rsync 11 May 19 21:21 lweim.txt
[root@inotify-slave ~]# cat /mydata/lweim.txt 
Hello Word    ##推送成功



五、部署rsync-master

1、通過inofity源碼包編譯安裝

[root@inotify-master ~]# tar -xf inotify-tools-3.13.tar
[root@inotify-master ~]# cd inotify-tools-3.13/
[root@inotify-master inotify-tools-3.13]# ./ configure --prefix=/usr/local/inotify   ##指明安裝路徑
[root@inotify-master ~]# make -j 4 && make install

2、編寫監控腳本

[root@inotify-master ~]# vim inotify.sh
#!/bin/bash

host=172.18.42.200   ##指明inotify-slave的ip地址
src=/www/lweim    ##本機監控的目錄,可隨意定義
dst=rsync       ##inotify-slave的rsync用戶的模塊目錄
user=lweim      ##虛擬用戶的用戶名
passfile=/etc/rsync.password   ##調用本地的密碼文件
inotify_home=/usr/local/inotify   ##指明inotify的安裝目錄

if [ ! -e "$src" ] || [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ] || [ ! -e "/etc/rsync.password" ]; then
#if  [ ! -e "${inotify_home}/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync" ] || [ ! -e "/etc/rsync.password" ]; then
echo "Check File and Folder"
exit 1
fi


${inotify_home}/bin/inotifywait -mrq -e close_write,delete,create,attrib $src | while read file 
do
cd $src && rsync -arvz -P ./ --timeout=100 $user@$host::$dst --password-file=$passfile &>/dev/null
done
exit 0


六、執行inotify-master上的inotify.sh腳本

[root@inotify-master ~]# bash -x inotify.sh   ##運行腳本
+ host=172.18.42.200
+ src=/web/lweim/
+ dst=rsync
+ user=lweim  
+ passfile=/etc/rsync.password
+ inotify_home=/usr/local/inotify
+ '[' '!' -e /web/lweim/ ']'
+ '[' '!' -e /usr/local/inotify/bin/inotifywait ']'
+ '[' '!' -e /usr/bin/rsync ']'
+ '[' '!' -e /etc/rsync.password ']'
+ read file
+ /usr/local/inotify/bin/inotifywait -mrq -e close_write,delete,create,attrib /web/lweim/


七、在inotify-master本機監控的目錄下創建文件

[root@inotify-master lweim]# pwd
/www/lweim
[root@inotify-master lweim]# touch a aa aaa aaaa
[root@inotify-master lweim]# ll
total 0
-rw-r--r-- 1 root root 0 May 19 22:54 a
-rw-r--r-- 1 root root 0 May 19 22:54 aa
-rw-r--r-- 1 root root 0 May 19 22:54 aaa
-rw-r--r-- 1 root root 0 May 19 22:54 aaaa


八、在inotify-slave的rsync工作模塊目錄下查看

[root@inotify-slave mydata]# ll
total 0
-rw-r--r-- 1 rsync rsync 0 May 19 22:54 a
-rw-r--r-- 1 rsync rsync 0 May 19 22:54 aa
-rw-r--r-- 1 rsync rsync 0 May 19 22:54 aaa
-rw-r--r-- 1 rsync rsync 0 May 19 22:54 aaaa
[root@inotify-slave mydata]# pwd
/mydata


附件爲配置rsync常見問題!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章