安装环境:
server:192.168.2.183
client:192.168.2.222
两台服务器上分别安装:
yum install rsync -y
sever端配置rsync文件vi /etc/rsyncd.conf 默认配置文件不存在自己创建,以下为配置
uid = nobody gid = nobody port = 873 host all = 192.168.2.222 use chroot = on max connections = 4 timeout = yes [web] path = /var/www/html/ comment = rsync files ignore errors read only = no list = yes auth users = rsync secrets file = /etc/rsync_server.passwd ~
创建密码通信文件vi /etc/rsync_server.passwd 并给与600权限
[root@localhost ~]# cat /etc/rsync_server.passwd rsync:rsync chmod 600 /etc/rsync_server.passwd
启动rsync
rsync --daemon
查看监听端口
[root@localhost ~]# netstat -ntulp | grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 20404/rsync tcp 0 0 :::873 :::* LISTEN 20404/rsync
rsync配置完毕
client端配置
实现实时同步需要安装inotify文件检查工具
下载安装
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz ./configure --prefix=/usr/local/inotify make && make install
编写脚本inotify.sh然后后端执行,脚本内容如下:
#!/bin/bash host=192.168.2.183 src=/data/soft/ dst=web user=rsync inotifywait=/usr/local/inotify/bin/inotifywait rsync=/usr/bin/rsync $inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib $src | while read file DATE TIME DIR; do $rsync -vzrtopg --delete --progress $src $user@$host::$dst --password-file=/etc/rsync_client.passwd echo "${files} was rsynced" >> /var/log/rsync.log 2>&1 done
客户端创建rsync同步密码文件rsync_client.passwd 并给与600权限
[root@node2 ~]# cat /etc/rsync_client.passwd rsync chmod 600 /etc/rsync_client.passwd rsync
然后
后台运行inotify.sh脚本
nohup sh inotify.sh & [root@node2 ~]# jobs [1]+ Running nohup sh inotify.sh & fg是调用后台运行的程序的,
然后测试客户端将文件推送到服务端:
在客户端/data/soft/下创建删除文件,服务端的/var/www/html/目录下也会实时同步,/html/目录要有写的权限
http://jingyan.baidu.com/article/59703552c5be9a8fc0074087.html