rsync+intofy实现数据的实时备份
一、环境
1、主机信息:
server node0 192.168.32.30 /var/www/html
client node1 192.168.32.31 /var/www/html
2、实验要求:
client node1 web目录下文件或目录发生任何修改、新建、删除等操作,都实时同步备份到server node0上
二、rsync服务器端配置(node0)
1、主配置文件(/etc/rsyncd.conf)
[root@node0 ~]# vim /etc/rsyncd.conf
gid = nobody
use chroot = no
max connections = 10 # 最大连接数为4
strict modes = yes
pid file = /var/run/rsyncd.pid #日志文件
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[webdata] #认证模块,在client端需要指定
path = /var/www/html #需要备份或接受备份的目录
comment = web server file
ignore errors #忽然一些无关的IO错误
read only = no
write only = no
hosts allow = *
hosts deny = 192.168.32.33
list = false
uid = root
gid = root
auth users = backup #认证用户,不是系统用户,如没有则匿名
secrets file = /etc/rsync_srv.pass #认证密码文件,权限必须为600
2、认证密码文件(文件名rsyncd.conf中定义)
[root@node0 ~]# vim /etc/rsync_srv.pass
backup:backupweb
#一行一个用户,可多行多用户
3、将rsync以守护进程形式启动
[root@node0 ~]# vim /etc/xinetd.d/rsync
service rsync
{
disable = no #no启动,yes禁止
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
#如不用xinetd进程启动rsync服务可直接运行命令:rsync --daemon
[root@node0 ~]# service xinetd restart #启动rsync服务
三、rsync客户端node1上配置
1、认证密码文件
[root@node1 ~]# vim /etc/rsync_srv.pass
backupweb
#密码文件名随意,权限600,密码通server node0上定义
2、同步node1到node0上测试
[root@node1 ~]# rsync -vzrtopg --delete --progress --password-file=/etc/rsync_srv.pass /var/www/html/
[email protected]::webdata
building file list ...
10 files to consider
./
index5
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/10)
index6
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=2/10)
index7
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=1/10)
index8
0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=0/10)
sent 334 bytes received 110 bytes 888.00 bytes/sec
total size is 30 speedup is 0.07
#同步node1上数据到node0上,如果想同步node0上数据到node1上将源或目标调换一下位置即可:
3、同步node0到node1上测试
[root@node1 ~]# rm -rf /var/www/html/*
[root@node1 ~]# rsync -vzrtopg --delete --progress --password-file=/etc/rsync_srv.pass
[email protected]::webdata /var/www/html/
receiving file list ...
5 files to consider
./
index5
0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=3/10)
index6
0 100% 0.00kB/s 0:00:00 (xfer#7, to-check=2/10)
index7
0 100% 0.00kB/s 0:00:00 (xfer#8, to-check=1/10)
index8
0 100% 0.00kB/s 0:00:00 (xfer#9, to-check=0/10)
sent 308 bytes received 623 bytes 1862.00 bytes/sec
total size is 30 speedup is 0.03
[root@node1 ~]# ls /var/www/html
index5 index6 index7 index8 index.html
四、安装inotify工具inotify-tool(node1上)
1、确认系统内核支持inotify
[root@node1 ~]# ll /proc/sys/fs/inotify/
总计 0
-rw-r--r-- 1 root root 0 02-07 16:14 max_queued_events #分配到每个实例中排队的event数量
-rw-r--r-- 1 root root 0 02-07 16:14 max_user_instances #每个real user可创建的inotify实例最大数量
-rw-r--r-- 1 root root 0 02-07 16:14 max_user_watches #每个inotify实例支持的最大目录数量
#inotify只对其所在系统平台进行监控,所用要将node1的数据实时传送到node0上,就必须安装在node1上
[root@node1 ~]#echo 30000000 > /proc/sys/fs/inotify/max_user_watches #增加实例支持的最大目录数量
2、安装inotify-tools工具
[root@node1 ~]# tar -zxvf inotify-tools-3.14.tar.gz
[root@node1 ~]# cd inotify-tools-3.14
[root@node1 inotify-tools-3.14]#
[root@node1 inotify-tools-3.14]# ./configure
[root@node1 inotify-tools-3.14]# make&&make install
[root@node1 inotify-tools-3.14]# ll /usr/
[root@node1 inotify-tools-3.14]# ls -ls /usr/local/bin/inotify*
40 -rwxr-xr-x 1 root root 37260 02-07 11:20 /usr/local/bin/inotifywait
36 -rwxr-xr-x 1 root root 35434 02-07 11:20 /usr/local/bin/inotifywatch
五、利用inotifywait监控等待事件,配合shell脚本实现rsync的实时同步
1、编辑实时同步脚本
[root@node1 ~]# vim /etc/init.d/rsy_inotify.sh
#!/bin/bash
host=192.168.32.30
src=/var/www/html/
des=webdata
user=backup
case $1 in
start)
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attri
b $src \
| while read files
do
rsync -vzrtopg --delete --progress --password-file=/etc/rsync_srv.pass $src $user@$host::$des >
> /tmp/rsyncd.log 2>&1
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done &
;;
stop)
kill -9 `ps -ef | grep inotifywait |awk '{print $2}' |sed -n '1p'`
echo "the inotifywait service have been stoped" >>/tmp/rsync.log
;;
esac
2、启动测试
[root@node1 ~]# /etc/init.d/rsy_inotify.sh start
[root@node0 ~]# ls /var/www/html/ #查看node0上内容
index1.html index2.html index3.html index4.html index5 index6 index7 index8 index.html
[root@node1 ~]# ls /var/www/html/ #查看node1上内容
index1.html index2.html index3.html index4.html index5 index6 index7 index8 index.html
[root@node1 ~]# rm -rf /var/www/html/*
[root@node0 ~]# ls /var/www/html/ #结果:node0上同步删除了
07/02/12 16:27 /var/www/html/index1.htmlDELETE was rsynced
07/02/12 16:27 /var/www/html/index2.htmlDELETE was rsynced
07/02/12 16:27 /var/www/html/index3.htmlDELETE was rsynced
07/02/12 16:27 /var/www/html/index4.htmlDELETE was rsynced
07/02/12 16:27 /var/www/html/index5DELETE was rsynced
07/02/12 16:27 /var/www/html/index6DELETE was rsynced
07/02/12 16:27 /var/www/html/index7DELETE was rsynced
07/02/12 16:27 /var/www/html/index8DELETE was rsynced
07/02/12 16:27 /var/www/html/index.htmlDELETE was rsynced
#日志显示文件删除备inotifywait监控到,并同步了。