通过rsync+inotify数据的实时备份
web1(nginx) 服务器节点1 192.168.0.9 /web1/wwwrooot
Server 内容发布节点 192.168.0.15 /web/wwwroot
1.安装rsync 与inotify-tools
安装inotify工具inotify-tools
[root@localhost wwwroot]# ll /proc/sys/fs/inotify/ 有下面3项,说明系统默认支持inotify
total 0
-rw-r--r-- 1 root root 0 Aug 31 23:02 max_queued_events
-rw-r--r-- 1 root root 0 Aug 31 23:02 max_user_instances
-rw-r--r-- 1 root root 0 Aug 31 23:02 max_user_watches
下载inotify-tools
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar xf inotify-tools-3.14.tar.gz
make make install
安装inotify-tools可以生成以下2个指令
[root@localhost wwwroot]# ll /usr/local/bin/
total 104
-rwxr-xr-x 1 root root 37260 Aug 31 17:01 inotifywait
-rwxr-xr-x 1 root root 35434 Aug 31 17:01 inotifywatch
2.在服务节点上配置rsync(rsync服务端)
web1的节点的rsyncd.conf配置如下:
[root@nginx web1]# cat /etc/rsyncd.conf
uid=nobody
gid=nobody
use chroot=no
max connections=10
strict modes=no
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
[web1]
path=/web1/wwwroot
comment =web1 file
ignore errors
read only=no
write only=no
hosts allow=192.168.0.1/24
hosts deny=*
list=false
uid=root
gid=root
auth users=web1
secrets file=/etc/web1.pass
echo "/usr/bin/rsync --daemon" >>/etc/rc.local
3.配置内容发布节点
[root@localhost wwwroot]# cat inotifyrsync.sh
#!/bin/bash
host1=192.168.0.9
src=/web/wwwroot/
dst1=web1
user1=web1
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src \
|while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
chmod 755 /web/wwwroot/inotifyrsync.sh
/web/wwwroot/inotifyrsync.sh &
最后执行脚本的时候
老是提示这个错误信息
[root@localhost wwwroot]# /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass /web/wwwroot/ web1@192.168.0.9::web1
rsync: failed to connect to 192.168.0.9: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]
一时没找到问题的原因:可以ping通192.168.0.9的,也可以ssh上去的
后来才发现是防火墙的问题
是web 节点(rsync服务端)的防火墙没有开启873(rsync的监听端口),后来关掉防火墙就可以实时同步了
一旦rsync客户端的数据有更新,会检测到有更新时会立马同步数据到rsync服务端(web节点)
测试结果
[root@client wwwroot]# touch 4.txt
[root@localhost wwwroot]# sh -x inotifyrsync.sh
+ host1=192.168.0.9
+ src=/web/wwwroot/
+ dst1=web1
+ user1=web1
+ /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib /web/wwwroot/
+ read files
+ /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass /web/wwwroot/ web1@192.168.0.9::web1
building file list ...
6 files to consider
deleting landy
./
4.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=1/6)
sent 186 bytes received 44 bytes 460.00 bytes/sec
total size is 373 speedup is 1.62
+ echo '31/08/12 23:43 /web/wwwroot/4.txtATTRIB was rsynced'
+ read files
+ /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass /web/wwwroot/ web1@192.168.0.9::web1
building file list ...
[root@web1 wwwroot]# ll
total 24
-rw-r--r-- 1 root root 0 Aug 31 2012 1.txt
-rw-r--r-- 1 root root 0 Aug 31 2012 2.txt
-rw-r--r-- 1 root root 0 Aug 31 2012 3.txt
-rw-r--r-- 1 root root 0 Aug 31 2012 4.txt
-rwxr-xr-x 1 root root 373 Aug 31 2012 inotifyrsync.sh
实现了实时的rsync同步
本文转自 xxl714 51CTO博客,原文链接:http://blog.51cto.com/dreamgirl1314/979489,如需转载请自行联系原作者