rsync与inotify实现数据实时同步
Rsync與Inotify
單一的rsync只可以進行數據同步,單一的inotify只可以實時監控文件,兩者結合使用剛好滿足數據實時同步的需求,下面就用一個數據發布服務器和兩個web服務器實例解析rsync+inotify實現實時同步。
數據發布服務器 192.168.1.5 (rsync+inotify)
web服務器 192.168.1.6 192.168.1.7 (rsync)
首先在web服務器上部署rsync
192.168.1.6配置
[root@localhost~]# yum install -y rsync
[root@localhost~]# mkdir -p /var/www/001
[root@localhost~]# chmod 660 /var/www/001
[root@localhost~]# chown nobody.nobody /var/www/001
[root@localhost~]# vim /etc/rsync.conf
transfer logging = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
uid = nobody
gid = nobody
user chroot = no
ignore errors
read only = no
[web1]
comment = Web comment
path = /var/www/001
auth users = tom
secrets file = /etc/rsyncd.passwd
hosts allow=192.168.1.5
hosts deny=*
list = false
[root@localhost~]# echo "tom:123456" > /etc/rsyncd.passwd
[root@localhost~]# chmod 600?/etc/rsyncd.passwd
[root@localhost~]# rsync --daemon
[root@localhost~]# echo "rsync --daemon" >> /etc/rc.local
[root@localhost~]# iptables -I INPUT -p tcp --dport 873 -j ACCEPT
[root@localhost~]# service iptables save
192.168.1.7配置
[root@localhost~]# yum install -y rsync
[root@localhost~]# mkdir -p /var/www/002
[root@localhost~]# chmod 660 /var/www/002
[root@localhost~]# chown nobody.nobody /var/www/002
[root@localhost~]# vim /etc/rsync.conf
transfer logging = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
uid = nobody
gid = nobody
user chroot = no
ignore errors
read only = no
[web2]
comment = Web comment
path = /var/www/002
auth users = tom
secrets file = /etc/rsyncd.passwd
hosts allow=192.168.1.5
hosts deny=*
list = false
[root@localhost~]# echo "tom:123456" > /etc/rsyncd.passwd
[root@localhost~]# chmod 600?/etc/rsyncd.passwd
[root@localhost~]# rsync --daemon
[root@localhost~]# echo "rsync --daemon" >> /etc/rc.local
[root@localhost~]# iptables -I INPUT -p tcp --dport 873 -j ACCEPT
[root@localhost~]# service iptables save
接著在數據發布服務器下載inotify-tool,安裝rsync和inotify (192.168.1.5)
[root@localhost~]# yum install -y rsync
[root@localhost~]# yum install -y automake libtool
[root@localhost~]# cd /home/soft/inotify-tools-master
[root@localhost inotify-tools-master~]# ./configure
[root@localhost inotify-tools-master~]# make && make install
[root@localhost~]# echo "123456" > /root/rsync.pass
[root@localhost~]# chmod 600 /root/rsync.pass
[root@localhost~]# vim rsync_notify.sh
#!/bin/bash
export PATH=/bin:/usr/bin:/usr/local/bin
SRC=/home/webdata/
DEST1=web1
DEST2=web2
Client1=192.168.1.6
Client2=192.168.1.7
User=tom
Passfile=/root/rsync.pass
[ ! -e $Passfile ] && exit 2
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w%f %e' --event modify,create,delete,attrib $SRC|while read line
do
echo "$line" > /var/log/inotify_web 2>&1
/usr/bin/rsync -avz --delete --progress --password-file=$Passfile $SRC ${User}@$Client1::$DEST1 >> /var/log/sync_web1 2>&1
/usr/bin/rsync -avz --delete --progress --password-file=$Passfile $SRC ${User}@$Client2::$DEST2 >> /var/log/sync_web2 2>&1
done &
[root@localhost~]# chmod a+x rsync_notify.sh
[root@localhost~]# ./rsync_notify.sh
[root@localhost~]# echo "/root/rsync_notify.sh" /etc/rc.local
inotifywait用法
inotifywait [-hcmrq] [-e?] [-t?] [--format?] [--timefmt?]?[ ... ]
參數:
-h,–help
輸出幫助信息
@
排除不需要監視的文件,可以是相對路徑,也可以是絕對路徑。
–fromfile?
從文件讀取需要監視的文件或排除的文件,一個文件一行,排除的文件以@開頭。
-m, –monitor
接收到一個事情而不退出,無限期地執行。默認的行為是接收到一個事情后立即退出。
-d, –daemon
跟–monitor一樣,除了是在后臺運行,需要指定–outfile把事情輸出到一個文件。也意味著使用了–syslog。
-o, –outfile?
輸出事情到一個文件而不是標準輸出。
-s, –syslog
輸出錯誤信息到系統日志
-r, –recursive
監視一個目錄下的所有子目錄。
-q, –quiet
指定一次,不會輸出詳細信息,指定二次,除了致命錯誤,不會輸出任何信息。
–exclude?
正則匹配需要排除的文件,大小寫敏感。
–excludei?
正則匹配需要排除的文件,忽略大小寫。
-t?, –timeout?
設置超時時間,如果為0,則無限期地執行下去。
-e?, –event?
指定監視的事件。
-c, –csv
輸出csv格式。
–timefmt?
指定時間格式,用于–format選項中的%T格式。
–format?
指定輸出格式。
%w 表示發生事件的目錄
%f 表示發生事件的文件
%e 表示發生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定義的時間格式
本文轉自super李導51CTO博客,原文鏈接:http://blog.51cto.com/superleedo/1889742?,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的rsync与inotify实现数据实时同步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: lvs-nat负载均衡模式
- 下一篇: Linux系统中网络配置详解