rsync+inotify 實時文件同步

 

Web主機

1.rsync使用的是xinet作爲其守護進程,我們首先配置其爲開啓

# vim /etc/xinetd.d/rsync
service rsync
{
disable = no    //改爲no
flags           = IPv6
socket_type     = stream
wait            = no
user            = root
server          = /usr/bin/rsync
server_args     = --daemon
log_on_failure  += USERID
}

2,配置rsync服務

編輯文件/etc/rsyncd.conf

uid = nobody
gid = nobody
use chroot = no
max connections = 3
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[web1]
path = /www/htdocs
ignore errors = yes
read only = no
write only = no
hosts allow = 172.16.0.0/16 //允許可以同步過來的主機
hosts deny = *
list = false
uid = root
gid = root
auth users = cyb  //認證的用戶名
secrets file = /etc/rsync.passwd //使用的祕鑰文件所在的地方

 

3,創建密碼文件

 

#vim /etc/rsync.passwd
cyb:123     //同步需要的用戶名和密碼
# chmod 600 /etc/rsync.passwd

 

啓動rsync

 

# service xinetd start

 

兩臺web 主機配置內容近似

 

SOURCE主機:

這邊也安裝rsync ,rsync 以服務器模式運行

安裝inotify-tools inotify-devel

# yum install inotify*

書寫密碼文件

# vim /etc/rsync_client.pwd
123
# chmod 600 /etc/rsync_client.pwd

創建腳本

 

# vim rsync.sh
#!/bin/bash
host1=172.16.11.10
host2=172.16.11.11
src=/www/web/
dst1=web1
dst2=web2
user1=cyb
user2=lyn
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib  $src \
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync_client.pwd $src $user1@$host1::$dst1
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync_client.pwd $src $user2@$host2::$dst2
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
~

在後臺運行

# bash /tmp/rsync.sh

 

最後,將此腳本加入系統自啓動文件開機自己啓動

echo“/tmp/rsync.sh&”>>/etc/rc.local

至此兩遍已經同步

 

 

 

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