rsync+inotify實時同步

Rsync+Inotify實時同步


Rsync:

rsync是類unix系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync可以遠程同步,支持本地複製,或者與其他SSH、rsync主機同步。


Inotify:

Inotify 是一個 Linux特性,它監控文件系統操作,比如讀取、寫入和創建。Inotify 反應靈敏,用法非常簡單,並且比 cron 任務的繁忙輪詢高效得多。學習如何將 inotify 集成到您的應用程序中,並發現一組可用來進一步自動化系統治理的命令行工具。


rsync+inotify:

隨着應用系統規模的不斷擴大,對數據的安全性和可靠性也提出的更好的要求,rsync在高端業務系統中也逐漸暴露出了很多不足,首先,rsync同步數據時,需要掃描所有文件後進行比對,進行差量傳輸。如果文件數量達到了百萬甚至千萬量級,掃描所有文件將是非常耗時的。而且正在發生變化的往往是其中很少的一部分,這是非常低效的方式。其次,rsync不能實時的去監測、同步數據,雖然它可以通過linux守護進程的方式進行觸發同步,但是兩次觸發動作一定會有時間差,這樣就導致了服務端和客戶端數據可能出現不一致,無法在應用故障時完全的恢復數據。基於以上原因,rsync+inotify組合出現了!


實驗環境:Centos7

源服務器-IP:10.0.0.132 (rsync+inotify)

目標服務器-IP:10.0.0.133 (rsync)


實驗拓撲:

12.PNG



配置前提:(兩臺都執行)

1.關閉selinux

[root@localhost ~]# vim /etc/selinux/config 
SELINUX=disabled


2.關閉防火牆

[root@localhost ~]# systemctl stop firewalld


3.時間同步

[root@localhost ~]# ntpdate cn.pool.ntp.org


操着過程:

配置目標服務器:

1.安裝rsync

[root@dest ~]# yum install -y rsync


2.修改配置文件,添加以下

[root@dest ~]# vim /etc/rsyncd.conf 
#工作中指定用戶(需要指定用戶)
uid = root
gid = root
####相當於黑洞.出錯定位
use chroot = no
#####有多少個客戶端同時傳文件
max connections = 200
#####超時時間
timeout = 300
#####進程號文件
pid file = /var/run/rsyncd.pid
#####日誌文件
lock file = /var/run/rsync.lock
#####日誌文件
log file = /var/log/rsyncd.log
#####模塊開始
[backup]
####需要同步的目錄
path = /test/
####表示出現錯誤忽略錯誤
ignore errors
#####表示網絡權限可寫(本地控制真正可寫)
read only = false
#####這裏設置IP或讓不讓同步
list = false
#####指定允許的網段
hosts allow = 10.0.0.0/24
#####拒絕鏈接的地址,以下表示沒有拒絕的鏈接。
hosts deny = 0.0.0.0/32
#####不要動的東西(默認情況)
#####虛擬用戶
auth users = rsync_backup
#####虛擬用戶的密碼文件
secrets file = /etc/rsync.password


3.創建密碼文件,並賦予權限

[root@dest ~]# cat /etc/rsync.password 
rsync_backup:123456 (用戶爲配置文件裏的虛擬用戶,密碼自定義)
[root@dest ~]# chmod 600 /etc/rsync.password


4.創建同步的目錄

[root@dest ~]# mkdir /test


5.啓動rsync

[root@dest ~]# rsync --daemon
[root@dest ~]# ps -ef |grep rsync
root       2832      1  0 20:06 ?        00:00:00 rsync --daemon
root       2834   2774  0 20:07 pts/1    00:00:00 grep --color=auto rsync


配置源服務器:

1.安裝rsync

[root@source ~]# yum install -y rsync


2.創建同步出去的目錄和密碼文件,並賦予密碼文件權限

[root@source ~]# cat /etc/rsync.password 
123456
[root@source ~]# chmod 600 /etc/rsync.password


3.測試推送

[root@source ~]# cd /test/
[root@source test]# touch {1..3}.txt
[root@source test]# ls
1.txt  2.txt  3.txt
[root@source test]# rsync -vzrtopg --progress /test/ [email protected]::backup  --password-file=/etc/rsync.password 
sending incremental file list
./
1.txt
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)
2.txt
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=1/4)
3.txt
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=0/4)
sent 167 bytes  received 68 bytes  470.00 bytes/sec
total size is 0  speedup is 0.00


4.查看目標服務器是否有文件

[root@dest ~]# cd /test/
[root@dest test]# ls
1.txt  2.txt  3.txt


5.安裝inotify

[root@source src]#  wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@source src]# tar -zxvf inotify-tools-3.14.tar.gz 
[root@source inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@source inotify-tools-3.14]# make && make install


6.編寫監控腳本,並給腳本權限

[root@source src]# cat inotify.sh 
#!/bin/bash
host01=10.0.0.133  #inotify-slave的ip地址
src=/test        #本地監控的目錄
dst=backup         #inotify-slave的rsync服務的模塊名
user=rsync_backup      #inotify-slave的rsync服務的虛擬用戶
rsync_passfile=/etc/rsync.password   #本地調用rsync服務的密碼文件
inotify_home=/usr/local/inotify   #inotify的安裝目錄
#judge
if [ ! -e "$src" ] \
|| [ ! -e "${rsync_passfile}" ] \
|| [ ! -e "${inotify_home}/bin/inotifywait" ] \
|| [ ! -e "/usr/bin/rsync" ];
then
echo "Check File and Folder"
exit 9
fi
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
| while read file
do
#rsync -vzrtopg  --progress --delete /jhonse/back/ [email protected]::backup  --password-file=/etc/rsync.password
#  rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1
cd $src && rsync -vzrtopg --progress  --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
done
exit 0
[root@source src]# chmod +x inotify.sh


7.創建計劃任務,實時同步

[root@source test]# crontab -l
* * * * * /usr/local/src/inotify.sh
[root@source test]# systemctl restart crond


8.測試,看能否實時同步

源服務器

[root@source test]# rm -rf *
[root@source test]# touch {4..6}.txt
[root@source test]# ls
4.txt  5.txt  6.txt

目標服務器

[root@dest test]# ls
4.txt  5.txt  6.txt


實驗成功。。。


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