centos-rhel服务器相关 / 未分类 · 2013年9月26日

rsync+inotify 实时数据的实时备份

第一 首先是内容发布机
[[email protected] ~]# uname -r
2.6.18-348.el5
#有下面三项输出
[[email protected] ~]# ll /proc/sys/fs/inotify/
总计 0
-rw-r–r– 1 root root 0 09-26 01:58 max_queued_events
-rw-r–r– 1 root root 0 09-26 01:58 max_user_instances
-rw-r–r– 1 root root 0 09-26 01:58 max_user_watches
安装inotify-tools
https://github.com/rvoicilas/inotify-tools/wiki
wget -c http://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 --prefix=/usr && make -j2 && su -c 'make install'

#安装成功
[[email protected] ~]# ll /usr/bin/inotifywa*
-rwxr-xr-x 1 root root 47192 09-26 02:05 /usr/bin/inotifywait
-rwxr-xr-x 1 root root 43530 09-26 02:05 /usr/bin/inotifywatch
[[email protected] ~]# cat inotifyrsync.sh
#!/bin/bash
host1=192.168.1.2
host2=192.168.1.3
src=/data/wwwroot/
dst1=web1
dst2=web2
user1=web1user
user2=web2user
#/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
/usr/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/server.pass $src [email protected]$host1::$dst1
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src [email protected]$host2::$dst2
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

chmod 755 inotifyrsync.sh
[[email protected] ~]# cat /etc/server.pass
1234

chmod 600 /etc/server.pass
##运行测试,对了得先web1 web2 配置好了才测试
sh inotifyrsync.sh &
建议写到系统自启动文件 /etc/rc.local
脚本相关解释如下:
–timefmt:指定时间的输出格式。
–format:指定变化文件的详细信息。
内容发布机 到此配置完成,下面是 web机器
第二 web机器
1.
/etc/rsyncd.conf
#Web1节点rsyncd.conf配置如下:
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.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.1.9
hosts deny = *
list = false
uid = root
gid = root
auth users = web1user
secrets file = /etc/web1.pass

cat /etc/web1.pass
evan:1234

chmod 600 /etc/web1.pass
#启动服务
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
2.
Web2节点rsyncd.conf配置如下:
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[web2]
path = /data/wwwroot/
comment = web2 file
ignore errors
read only = no
write only = no
hosts allow = 192.168.1.9
hosts deny = *
list = false
uid = root
gid = root
auth users = web2user
secrets file = /etc/web2.pass

##
cat /etc/web2.pass
evan:1234

chmod 600 /etc/web2.pass
#启动服务
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
在2台web 服务节点rsyncd.conf文件配置完成后,依次启动rsync守护进程,接着将rsync服务加入到自启动文件中:
echo “/usr/local/bin/rsync –daemon” >>/etc/rc.local
到此为止,2个web服务节点已经配置完成。多个节点也依此如此
chmod 755 /web/wwwroot/inotifyrsync.sh
/web/wwwroot/inotifyrsync.sh &
问题处理
问题1
[[email protected] ~]# /usr/bin/inotifywait: error while loading shared libraries: libinotifytools.so.0: cannot open shared object file: No such file or directory
[1]+ Done bash inotifyrsync.sh
[[email protected] src]# find / -name libinotifytools.so.0
/usr/lib/libinotifytools.so.0
/root/inotify-tools-3.14/libinotifytools/src/.libs/libinotifytools.so.0
ln -s /usr/lib/libinotifytools.so.0 /usr/lib64/libinotifytools.so.0
问题2
用户名写错了
@ERROR: auth failed on module web1
rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6]
@ERROR: auth failed on module web2
rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6]
注意点
usr/local/bin/inotifywait -mrq –timefmt ‘%d/%m/%y %H:%M’ –format ‘%T %w%f’ \
-e modify,delete,create,attrib \
${src} \
上面的命令返回的值类似于:
10/03/09 15:31 /wwwpic/1
这3个返回值做为参数传给read,关于此处,有人是这样写的:
inotifywait -mrq -e create,move,delete,modify $SRC | while read D E F;do
细化了返回值。
参考
http://ixdba.blog.51cto.com/2895551/580280
http://www.centos.bz/2012/06/inotify-tools-introduction/
http://blog.csdn.net/firefoxbug/article/details/8188804
http://bbs.linuxtone.org/thread-2681-1-1.html