Chinaunix首页 | 论坛 | 博客
  • 博客访问: 610997
  • 博文数量: 244
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 130
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-27 09:53
个人简介

记录学习,记录成长

文章分类

全部博文(244)

我的朋友

分类: LINUX

2015-12-05 23:47:16

rsync+inotify实现数据实时备份

一.inotify概述
1.inotify介绍
在日常的运维过程中,经常需要备份某些文件,或者对系统的某些文件进行监控,比如重要的配置文件等。如果需要作到实时同步或者监控,就需要使用内核的inotify机制;

inotify是基于inode级别的文件系统监控技术,是一种强大的、细粒度的、异步的机制,它满足各种各样的文件监控需要,不仅限于安全和性能;

inotify不需要对被监视的目标打开文件描述符,而且如果被监视目标在可移动介质上,那么在umount该介质上的文件系统后,被监视目标对应的watch将被自动删除,并且会产生一个umount事件;

inotify 既可以监视文件,也可以监视目录;

inotify 使用系统调用而非 SiGiO(信号驱动)来通知文件系统事件;

inotify 使用文件描述符作为接口,因而可以使用通常的文件I/O操作select和poll来监视文件系统的变化;

2. inotify可监视的文件系统事件

            access:被监控的文件或被监控目录内的文件被读取;

            modify:被监控的文件或被监控目录内的文件被写入;

            attrib:被监控的文件的元数据或被监控目录内的文件的元数据被修改,包括时间戳,权限,扩展属性等;

            close_write:被监控的文件或被监控目录内的文件被关闭(不管如何被打开的);

            open:被监控的文件或被监控目录内的文件被打开;

            moved_to:文件或目录被移往被监控的目录中(即使文件从目录被移往相同的目录);

            moved_from:文件或目录从被监控的目录中移出(即使文件从目录从相同的目录中移出);

            move:文件或目录被移到或移出被监控的目录;

            move_self:文件或目录自移动,这个事件完成后,文件或目录不在被监视;

            create:在被监控的目录下创建文件或目录;

            delete:在被监控的目录下删除文件或目录;

            delete_self:文件或目录被删除,这个事件完成后,文件或目录不在被监控(即使未明确被监听,事件也依旧会发生);

            umount:被监控的文件或目录所在的文件系统被卸载,这个事件完成后,文件或目录不在被监听;

个人理解,具体请自行man inotifywait查看;

3.  inotify的内核版本支持
从2.6.13版本内核以后,inotify已经被植入内核,而判断当前内核是否支持inotify的方法为
[root@client ]# ll /proc/sys/fs/inotify/
-rw-r--r-- 1 root root 0 Dec  5 17:32 max_queued_events
-rw-r--r-- 1 root root 0 Dec  5 17:32 max_user_instances
-rw-r--r-- 1 root root 0 Dec  5 17:32 max_user_watches
如果有上面3项输出,就表示系统默认支持inotify,可以安装inotify-tools了

其中:
/proc/sys/fs/inotify/max_queued_evnets表示调用inotify_init时分配给inotify instance中可排队的event的数目的最大值,超出这个值的事件被丢弃,但会触发IN_Q_OVERFLOW事件,默认值为16384
如果在日志中看到Event Queue Overflow,说明max_queued_events太小需要调整参数后再次使用;

/proc/sys/fs/inotify/max_user_instances表示每一个real user ID可创建的inotify instatnces的数量上限,默认值为128

/proc/sys/fs/inotify/max_user_watches表示每个inotify实例相关联的watches的上限,即每个inotify实例可监控的最大目录数量。如果监控的文件数目巨大,需要根据情况,适当增加此值的大小,默认值为8192

4.inotify-tools说明

inotify-tools是为linux下inotify文件监控工具提供的一套C的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件;
 
inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数;

5.安装inotify-tools
下载地址:
或者

也可以配置EPEL源直接安装
inotify-tools.i686                      3.14-1.el6                               epel

6.inotify工具的参数

6.1  inotifywait:
语法格式:inotifywait [-hcmrq][-e][-t][--format][-timefmt][...]

   -m或--monitor:始终保持事件监听状态。

   -r或--recursive:递归查询目录

   -q或--quiet:打印出监控事件

   -o或--outfile:输出事情到一个文件而不是标准输出

   -s或--syslog:输入错误信息到系统日志

   -e或--event:  通过此参数可以指定要监控的事件,常见的事件有modify、delete、create、close_write、move、close、unmount和attrib等

   -format:指定输出格式;常用的格式符如:%w:表示发生事件的目录  %f:表示发生事件的文件  %e:表示发生的事件  %T:使用由-timefmt定义的时间格式

   -timefmt:指定时间格式,用于-format选项中的%T格式

6.2  inotifywatch:

语法格式:inotifywatch[-hvzrqf][-e][-t][-a][-d][...]

   -fromfile:从文件中读取需要监控的文件或排除的文件,一个文件一行,排除的文件以"@"开头

   -z或-zero:输出表格的行和列,即使元素为空

   -r或-recursive:监视一个目录下的所有子目录

   -t或-timeout:设置超时时间

   -e或-event:只监听指定的事件

更详细的介绍请参看man inotifywait和man inotifywatch

7.  inotify使用示例(局域脚本实现)
[root@server ~]# cat inotify_test.sh
#!/bin/bash
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format  '%T %w%f %e' --event modify,delete,create,attrib  /media | while read  date time file event
  do
      case $event in
          MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)
                  echo $event'-'$file
              ;;
  
          MOVED_FROM|MOVED_FROM,ISDIR|DELETE|DELETE,ISDIR)
                  echo $event'-'$file
              ;;
      esac
  done
执行脚本:
[root@server ~]# ./inotify_test.sh 
进行操作:
[root@server media]# touch a.txt
[root@server media]# echo "Hello" > a.txt
[root@server media]# ls
a.txt
[root@server media]# rm -rf a.txt
查看输出:
CREATE-/media/a.txt
MODIFY-/media/a.txt
MODIFY-/media/a.txt
DELETE-/media/a.txt

二.使用rsync+inotify实现数据实时备份
1.配置环境
数据服务器:192.168.85.128+inotify+rsync
备份客户端:192.168.85.143+rsync

备份端:
2.编写rsync配置文件
[root@client ~]# cat /etc/rsync.conf
uid=root
gid=root
use chroot=no
max connections=5
strict modes=yes
port=873
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log

[test]
path=/test            #记得要创建
comment=This is rsync test
ignore errors
read only=no
write only=no
hosts allow=192.168.85.128
hosts deny=*
list=no
uid=root
gid=root
auth users=backup
secrets file=/etc/rsync_server.passwd        

3.密码文件
[root@client ~]# vim /etc/rsync_server.passwd 
backup:redhat

[root@client ~]# chmod 600 /etc/rsync_server.passwd

4.启动rsync服务
[root@client ~]# rsync  --daemon --config=/etc/rsync.conf
Starting xinetd: [  OK  ]

[root@client ~]# netstat -ntlp | grep :873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      5835/rsync          
tcp        0      0 :::873                      :::*                              LISTEN      5835/rsync          

数据服务器端:
5.编写密码文件
[root@server media]# vim /etc/rsync_server.passwd 
redhat

6.编写备份脚本
[root@server media]# cat /root/backup.sh 
#!/bin/bash
#192.168.85.128 back to 192.168.85.143
#
host=192.168.85.143
src=/media/
dst=test
user=backup
if [ ! -e "$src" ] || [ ! -e "/etc/rsync_server.passwd" ] || [ ! -e "/usr/bin/inotifywait" ] || [ ! -e "/usr/bin/rsync"];
then
echo "Check complished!!! "
exit 9
fi 

/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M:%S' --format '%T  %w%f%e' -e modify,delete,create,attrib $src | while read file
        do
        /usr/bin/rsync -vztropg --delete --password-file=/etc/rsync_server.passwd $src $user@$host::$dst
                echo "${file}  was rsyncd" >> /var/log/rsync.log 2>&1
done

[root@server ~]# chmod +x backup.sh 

7.备份测试
7.1  服务端执行脚本:
[root@server ~]# ./backup.sh 

7.2  进入/media目录进行操作:
[root@server ~]# cd /media/
[root@server media]# touch a.txt
[root@server media]# echo "Hello" > a.txt 
[root@server media]# mkdir b
[root@server media]# rm -rf b

7.3  查看输出:
sending incremental file list
./
a.txt

sent 79 bytes  received 30 bytes  218.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 40 bytes  received 8 bytes  96.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list
a.txt

sent 90 bytes  received 27 bytes  234.00 bytes/sec
total size is 6  speedup is 0.05
sending incremental file list

sent 44 bytes  received 8 bytes  104.00 bytes/sec
total size is 6  speedup is 0.12
sending incremental file list
./
b/

sent 72 bytes  received 15 bytes  174.00 bytes/sec
total size is 6  speedup is 0.07
sending incremental file list
./
deleting b/

sent 47 bytes  received 11 bytes  116.00 bytes/sec
total size is 6  speedup is 0.10

7.4  查看客户端是否同步:
[root@localhost test]# ll
-rw-r--r-- 1 root root 6 Dec  5 23:23 a.txt

7.5  服务器端同步日志:
[root@server ~]# cat /var/log/rsync.log 
05/12/15 23:23:15  /media/a.txtCREATE  was rsyncd
05/12/15 23:23:15  /media/a.txtATTRIB  was rsyncd
05/12/15 23:23:32  /media/a.txtMODIFY  was rsyncd
05/12/15 23:23:32  /media/a.txtMODIFY  was rsyncd
05/12/15 23:23:39  /media/bCREATE,ISDIR  was rsyncd
05/12/15 23:23:43  /media/bDELETE,ISDIR  was rsyncd

至此,通过rsync+inotify实现数据实时备份全部完成了;

参考资料:
http://blog.chinaunix.net/uid-23665447-id-3861341.html

http://www.51know.info/system_security/inotify.html

http://www.cnblogs.com/davidwang456/p/3684945.html

阅读(688) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~