Rsync+inotify 實現文件實時同步
背景說明:
1、需要將交換服務器的 /opt/ftp/com 目錄的內容實時同步到內網web服務器的 /opt/www 目錄;
2、需要將內網web服務器的 /opt/www 目錄的內容實時同步到 /opt/ftp/out 目錄。
192.168.232.128 192.168.232.129 交換服務器 內網Web服務器 | | /opt/ftp/out <-------- /opt/www | | | | /opt/ftp/com --------> /opt/www
具體步驟
環境准備:
實驗環境准備:(centos6平台)
兩台服務器均需配置
# service iptables stop //關閉防火牆 # sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux //關閉selinux,重啟生效 # setenforce 0 //關閉selinux,臨時生效 # yum -y install rsync xinetd //安裝rsync xinetd 軟件
1) 交換服務器准備測試文件
[root@exchange ~]# mkdir -p /opt/ftp/{com,out} [root@exchange ~]# touch /opt/ftp/com/com_test{1..10} [root@exchange ~]# touch /opt/ftp/out/out_test{1..10} [root@exchange ~]# ls /opt/ftp/com/ com_test1 com_test10 com_test2 com_test3 com_test4 com_test5 com_test6 com_test7 com_test8 com_test9 [root@exchange ~]# ls /opt/ftp/out/ out_test1 out_test10 out_test2 out_test3 out_test4 out_test5 out_test6 out_test7 out_test8 out_test9
2) web服務器准備測試文件
[root@web ~]# mkdir -p /opt/www [root@web ~]# touch /opt/www/www_test{1..10} [root@web ~]# ls /opt/www/ www_test1 www_test10 www_test2 www_test3 www_test4 www_test5 www_test6 www_test7 www_test8 www_test9
交換服務器配置
3) 配置xinetd配置文件,將disable = yes 改成 no
[root@exchange ~]# vi /etc/xinetd.d/rsync # default: off # description: The rsync server is a good addition to an ftp server, as it \ # allows crc checksumming etc. service rsync { disable = no flags = IPv6 socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID }
4) 編輯rsync配置文件,該文件不存在,需要手動創建
[root@exchange ~]# vi /etc/rsyncd.conf uid = root gid = root [com] path = /opt/ftp/com auth users = com_user secrets file = /etc/rsyncd.secrets read only = false [out] path = /opt/ftp/out auth users = out_user secrets file = /etc/rsyncd.secrets read only = false
5) 編輯賬號認證文件,並給予600權限
[root@exchange ~]# vi /etc/rsyncd.secrets com_user:ComFtpUser@qaz out_user:OutFtpUser@zaq [root@exchange ~]# chmod 600 /etc/rsyncd.secrets
6) 啟動xinetd服務
[root@exchange ~]# service xinetd start
7) web服務器測試
[root@web ~]# rsync -a 192.168.232.128:: //查看共享出來的標簽 com out
web服務器配置
8) 配置web服務器上面的rsync服務(和上面交換服務器上一樣配置方法)
[root@web ~]# vim /etc/rsyncd.conf uid = root gid = root [www] path = /opt/www auth users = www_user secrets file = /etc/rsyncd.secrets read only = false [root@web ~]# vim /etc/xinetd.d/rsync disable = no [root@web ~]# vim /etc/rsyncd.secrets www_user:WebServerUser@qaz [root@web ~]# chmod 600 /etc/rsyncd.secrets [root@web ~]# service xinetd start
9)在交換服務器上測試
[root@exchange ~]# rsync -a 192.168.232.129:: www
整體測試
# 整體測試
1)在web服務器上將 /opt/www目錄下的文件同步到交換服務器的 /opt/ftp/out 目錄
[root@web ~]# rsync -avt /opt/www/ out_user@192.168.232.128::out
2)在交換服務器上將 /opt/ftp/com 目錄下的文件同步到web服務器的/opt/www目錄
[root@exchange ~]# rsync -avt /opt/ftp/com/ www_user@192.168.232.129::www
上面測試成功后接着配置 inotify 實時同步
1)安裝inotify-tools,兩台都需要安裝
下載地址:http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
# tar xf inotify-tools-3.14.tar.gz # cd inotify-tools-3.14 # ./configure # make # make install # cp /usr/local/bin/inotifywa* /usr/sbin/
2) 在web服務器上面創建密碼文件,用於保存交換服務器上面的密碼,(此處是需要同步web服務器的文件到交換服務器的/opt/ftp/out目錄,所以對應是 out_user 用戶的密碼)
[root@web ~]# mkdir -p /home/work/ftppass [root@web ~]# vim /home/work/ftppass/passwd OutFtpUser@zaq [root@web ~]# chmod 600 /home/work/ftppass/passwd // 測試:rsync -avt --password-file=/home/work/ftppass/passwd /opt/www/ out_user@192.168.232.128::out
3) 在交換服務器上面創建密碼文件,用於保存web服務器上面的密碼,(此處是需要同步交換服務器的文件到web服務器的/opt/www目錄,所以對應的是 www_user 用戶的密碼)
[root@exchange ~]# mkdir -p /home/work/ftppass [root@exchange ~]# vim /home/work/ftppass/passwd WebServerUser@qaz [root@exchange ~]# chmod 600 /home/work/ftppass/passwd // 測試:rsync -avt --password-file=/home/work/ftppass/passwd /opt/ftp/com/ www_user@192.168.232.129::www
4) 編輯web服務器上面同步腳本,並放置后台運行
[root@web ~]# vim www_to_out.sh #!/bin/sh #Desc: 用於從web服務器/opt/www目錄中的文件同步到交換器服務器的/opt/ftp/out目錄 #Date: 2019-04-16 #By: Lee-YJ inotifywait -mrq -e modify,create,move,delete,attrib /opt/www |while read events do rsync -avt --password-file=/home/work/ftppass/passwd /opt/www/ out_user@192.168.232.128::out echo "[`date "+%Y-%m-%d %H:%M:%S"`] 出現事件 $events" >> /var/log/sync.log done [root@web ~]# nohup bash www_to_out.sh &
5) 編輯交換服務器上面同步腳本,並放置后台運行
[root@exchange ~]# vim com_to_www.sh #!/bin/sh #Desc: 用於從交換服務器/opt/ftp/com目錄中的文件同步到web服務器的/opt/www目錄 #Date: 2019-04-16 #By: Lee-YJ inotifywait -mrq -e modify,create,move,delete,attrib /opt/ftp/com |while read events do rsync -avt --password-file=/home/work/ftppass/passwd /opt/ftp/com/ www_user@192.168.232.129::www echo "[`date "+%Y-%m-%d %H:%M:%S"`] 出現事件 $events" >> /var/log/sync.log done [root@exchange ~]# nohup bash com_to_www.sh &
6)測試(根據上面架構圖的說明,也就是說現在我們在交換服務器上面的 /opt/ftp/com 目錄中創建了文件,那么會自動同步到web服務器上面的 /opt/www 目錄中,並且 web服務器又會自動同步到 交換服務器的 /opt/ftp/out 目錄里面即為成功)
先查看當前兩個服務器的文件
// 交換服務器 [root@exchange ~]# ls /opt/ftp/com/ com_test1 com_test2 com_test4 com_test6 com_test8 com_test10 com_test3 com_test5 com_test7 com_test9 [root@exchange ~]# [root@exchange ~]# ls /opt/ftp/out/ out_test1 out_test2 out_test4 out_test6 out_test8 out_test10 out_test3 out_test5 out_test7 out_test9 // web服務器 [root@web ~]# ls /opt/www/ www_test1 www_test2 www_test4 www_test6 www_test8 www_test10 www_test3 www_test5 www_test7 www_test9
在交換服務的 /opt/ftp/com 目錄創建文件
[root@exchange ~]# touch /opt/ftp/com/inotify{1..10}
等待幾秒再次查看 web 服務器的 /opt/www 目錄 和 交換服務器的 /opt/ftp/out 目錄
// web服務器 [root@web ~]# ls /opt/www/ com_test1 com_test4 com_test8 inotify2 inotify6 www_test1 www_test4 www_test8 com_test10 com_test5 com_test9 inotify3 inotify7 www_test10 www_test5 www_test9 com_test2 com_test6 inotify1 inotify4 inotify8 www_test2 www_test6 com_test3 com_test7 inotify10 inotify5 inotify9 www_test3 www_test7 // 交換服務器 [root@exchange ~]# ls /opt/ftp/out/ com_test1 com_test5 inotify1 inotify5 out_test1 out_test5 www_test1 www_test5 com_test10 com_test6 inotify10 inotify6 out_test10 out_test6 www_test10 www_test6 com_test2 com_test7 inotify2 inotify7 out_test2 out_test7 www_test2 www_test7 com_test3 com_test8 inotify3 inotify8 out_test3 out_test8 www_test3 www_test8 com_test4 com_test9 inotify4 inotify9 out_test4 out_test9 www_test4 www_test9
通過上面測試結果可以看出,已經實現了自動同步。