您的位置:首页 > 其它

rsync实现服务器之间文件实时同步

2015-03-25 11:35 543 查看
一、主服务器
其中主服务器需要安装rsync与inotify,主服务器作为server,向备份服务器client传输文件
1.安装rsync
yum -y install rsync

2.建立密码文件
echo '123456' >> /etc/data.passwd
chmod 600 /etc/data.passwd

3、安装inotify

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make && make install

4、创建rsync复脚本
无论是添加、修改、删除文件都能够通过inotify监控到,并通过rsync实时的同步给client的/data里
#!/bin/bash
ip=10.6.88.229
src=/data/test
des=test
user=root
/usr/local/inotify/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/data.passwd $src $user@$ip::$des
echo "${files} was rsynced" >>/var/log/rsync.log 2>&1
done

5.启用脚本
echo "/tw/rsync.sh" >> /etc/rc.local
sh /tw/rsync.sh &

二、备份服务器

1.安装rsyncyum -y install rsync2.建立rsync配置文件
vim /etc/rsyncd.conf

uid = root
gid = root
port = 873
max connections = 20000
use chroot = yes
timeout = 200
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log format = %t %a %m %f %b
auth users = root
secrets file = /etc/data.passwd

[test]
path = /data/
comment = "test directory file"
list = yes
read only = no
ignore errors = yes
hosts allow = 10.6.88.0/255.255.0.0

3.建立密码文件

echo 'root:123456' >> /etc/data.passwdchmod 600 /etc/data.passwd

4.启动rsync

/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf
echo '/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsync.conf ' >> /etc/rc.local

接下来我们来做一下测试,在server上/data/test/目录下创建个test文件,看看client是否能收到
注:如需在主服务器上删除备份文件,而备份服务器上保留文件。直接把脚本里--delete参数去掉就可以。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息