1.架构
服务器端:192.168.202.135
客户端:192.168.202.129
2.rsync服务器端安装配置
为rsync服务提供配置文件:
vim /etc/rsyncd .conf
uid = root
gid = root
port = 873
hosts allow = 192.168.202.0 /24
max connections = 0
timeout= 300
pid file = /var/run/rsyncd .pid
lock file = /var/run/rsync .lock
log file = /var/log/rsyncd .log
[backup]
path = /rsync/test
read only = no
list = no
auth users = rsync
secrets file = /etc/rsync . passwd
|
设置密码文件:
vim /etc/rsync . passwd
rsync :123456
chmod 600 /etc/rsync . passwd
|
注:当配置文件中参数strict modes为true时,rsync认证口令文件的权限一定是600,否则客户端将不能连接服务器。rsync认证口令文件中每一行指定一个 用户名:口令 对,格式为:username:passwd。
启动rsync服务并设置开机自启动:
/usr/bin/rsync --daemon
echo "/usr/bin/rsync --daemon" >> /etc/rc . local
|
3.rsync客户端安装与配置
安装inotify-tools:
在安装inotify-tools前请先确认你的linux内核是否达到了2.6.13,并且在编译时开启了CONFIG_INOTIFY选项,也可以通过以下命令检测,如果出现以下输出,说明支持:
[root@localhost ~]
max_queued_events max_user_instances max_user_watches
|
安装:
wget http: //cloud .github.com /downloads/rvoicilas/inotify-tools/inotify-tools-3 .14. tar .gz
tar xvf inotify-tools-3.14. tar .gz
cd inotify-tools-3.14
. /configure
make ; make install
|
编写rsync.sh监控脚本:
#!/bin/bash
host=192.168.202.135
src= /data/www/
des=backup
user= rsync
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' -- format '%T %w%f' \
-e modify,delete,create,attrib \
${src} \
| while read file
do
rsync -vzrtopg --delete --progress ${src} ${user}@${host}::${des} --password- file = /etc/www . pwd &&
echo "${files} was rsynced" >> /tmp/rsync .log 2>&1
echo "---------------------------------------------------------------------------"
done
|
建立认证文件(客户端认证文件只用加入密码即可)并后台运行rsync.sh脚本:
echo 'rsync' >> /etc/www . pwd
chmod 600 /etc/ww . pwd
nohup sh /root/rsync .sh &
echo "nohup sh /root/rsync.sh &" >> /etc/rc . local
|
参考资料:
rsync服务器的安装与配置:http://www.cnblogs.com/mchina/p/2829944.html
rsync+inotify实现文件批量更新:http://kerry.blog.51cto.com/172631/734087/
rsync+inotify实现数据实时备份:
http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=11840697&id=3890795