rsync+inotify

来源:互联网 发布:mpii数据集介绍 编辑:程序博客网 时间:2024/06/11 00:32
系统:ubuntu12.04
源端:192.168.88.145
节点端:192.168.88.155
应用:数据的实时同步

rsync节点端:

sudo apt-get install rsync 
#/usr/share/doc/rsync/examples/rsyncd.conf  参考配置文件
sudo vim /etc/rsync.conf  需自行创建
-->
uid=root
gid=root
use chroot = no
max connections = 20
strict modes = yes
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log format = %t %a %m %f %b
hosts allow = 192.168.88.0/24
hosts_deny= *

[test]
path = /app/webapps
auth users = xxx    ---用户
ignore errors = yes
read only = no
write only = no
list = no
secrets file = /etc/rsync.passwd

mkdir /app/webapps -p
chown xxx.xxx /app/webapps -R
sudo vim /etc/rsync.passwd
格式  用户:密码
eg--> xxx:pass
sudo chmod 600 /etc/rsync.passwd
启动daemon:sudo rsync --daemon --config=/etc/rsync.conf
写入开机自启动:vim  /etc/rc.local
-->rsync --daemon --config=/etc/rsync.conf

源端:
sudo apt-get install rsync inotify-tools
sudo vim /etc/rsync.passwd
-->这里只需密码 如:pass
sudo chmod 600 /etc/rsync.passwd
sudo chown xxx.xxx /etc/rsync.passwd
mkdir /app/webapps -p
chown xxx.xxx /app/webapps -R

编写rsync同步脚本rsync.sh
-->
#!/bin/sh

SRC=/app/webapps
#DES=root
#USER=root

media1=192.168.88.155
#media2=192.168.88.130
#media3=192.168.88.131

#/usr/bin/inotifywait -mrq -e create,move,delete,modify $SRC | while read files
/usr/bin/inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F
 
do
rsync -avzxPl --delete --password-file=/etc/rsync.passwd /app/webapps/ xxx@$media1::test
rsync -avzxPl --delete --password-file=/etc/rsync.passwd /app/webapps/ xxx@$media2::test
if [ $? -eq 0 ];then
        echo "${file} ok" >>/tmp/rsync.log 2>&1
fi
done

启动脚本:nohup /etc/rsync.sh &
注:用nohup后台启动,当终端关闭后进程不结束
写入开机自启动:vim  /etc/rc.local
--->nohup /etc/rsync.sh &

测试:略
0 0
原创粉丝点击