您的位置:首页 > 其它

Rsync + Inotify 实现文件自动同步

2014-12-05 14:47 246 查看
Rsync Server1:10.10.10.78 RHEL5.9
Rsync Server1:10.10.10.40 RHEL5.9

Rsync Client + Inotify server:10.10.10.201 RHEL5.9

一、在10.10.10.78 配置
useradd tomcat
mkdir /root/hunk
chown -R tomcat.tomcat /root/hunk
yum install -y rsync
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
read only = no #是否允许客户端上传数据,这里设置为允许
address=10.10.10.78 #Rsync server ip
port=873 #设置服务器监听端口号
max connections = 10
timeout = 300
motd file =/etc/rsyncd/rsyncd.motd
pid file = /var/run/rsyncd/rsyncd.pid
log file = /var/log/rsyncd/rsyncd.log
lock file = /var/run/rsyncd/rsyncd.lock
hosts allow = 10.10.10.201 #允许客户端同步的IP
hosts deny=*
list = yes
auth users = hunk

[test]
uid = tomcat
gid = tomcat
read only = no
path = /root/hunk
auth users = hunk
secrets file = /etc/rsyncd/rsyncd.secrets
comment = test rsync

mkdir /etc/rsyncd/

vim /etc/rsyncd/rsyncd.secrets
hunk:123456

vim /etc/rsyncd/rsyncd.motd
#########this is a 78 rsync test!#############

二、在10.10.10.40配置
useradd tomcat
mkdir /root/hunk
chown -R tomcat.tomcat /root/hunk
yum install -y rsync
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = no
read only = no #是否允许客户端上传数据,这里设置为允许
address=10.10.10.40 #Rsync server ip
port=873 #设置服务器监听端口号
max connections = 10
timeout = 300
motd file =/etc/rsyncd/rsyncd.motd
pid file = /var/run/rsyncd/rsyncd.pid
log file = /var/log/rsyncd/rsyncd.log
lock file = /var/run/rsyncd/rsyncd.lock
hosts allow = 10.10.10.201 #允许客户端同步的IP
hosts deny=*
list = yes
auth users = hunk

[test]
uid = tomcat
gid = tomcat
read only = no
path = /root/hunk
auth users = hunk
secrets file = /etc/rsyncd/rsyncd.secrets
comment = test rsync

mkdir /etc/rsyncd/

vim /etc/rsyncd/rsyncd.secrets
hunk:123456

vim /etc/rsyncd/rsyncd.motd
#########this is a 40 rsync test!#############

三、在10.10.10.201上配置
useradd tomcat

1、yum install -y rsync

mkdir /test/hunk
mkdir /test/log
2、vim /test/rsync.pw
123456
3、解压inotify-tools-3.14.tar.gz
[root@localhost inotify]# cd /data/inotify/
[root@localhost inotify]# tar zxvf inotify-tools-3.14.tar.gz

4、安装
[root@localhost inotify]# cd inotify-tools-3.14
[root@localhost inotify-tools-3.14]# ./configure
[root@localhost inotify-tools-3.14]# make
[root@localhost inotify-tools-3.14]# make install
5、编写脚本,当监控到数据发生改变时,自动进行数据同步操作。
并将该脚本放到 /etc/rc.local ,让其开机就运行。

vim /test/notify_rsync.sh
#!/bin/bash
#this is rsync script based on inotify.
#date:2014-08-25
#
#
export PATH=/bin:/usr/bin:/usr/local/bin
SRC=/test/hunk/
DEST1=test
DEST2=test
Client1=10.10.10.78
Client2=10.10.10.40
User=hunk
#password file must not be other-accessible.
Passfile=/test/rsync.pw
123456

[ ! -e $Passfile ] && exit 2
#wait for change
inotifywait -mrq --timefmt '%y-%m%d %H:%M' --format '%T %w%f %e' --event modify,create,move,delete,attrib $SRC|while read line
do
echo "$line" > /test/log/inotify_log 2>&1
/usr/bin/rsync -avz --delete --progress --password-file=${Passfile} ${SRC} ${User}@${Client1}::${DEST1} >> /test/log/sync_78 2>&1
/usr/bin/rsync -avz --delete --progress --password-file=${Passfile} ${SRC} ${User}@${Client2}::${DEST2} >> /test/log/sync_40 2>&1
done &

四、测试
在10.10.10.201上的/test/hunk 创建文件
cd /test/hunk
touch 1 2 3
在10.10.10.78和10.10.10.40上的/root/hunk
cd /root/hunk
ls
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rsync inotify 自备份