一、rsync的优点与不足
与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!
二、 初识inotify
Inotify 是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
在上面章节中,我们讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
三、 安装inotify工具inotify-tools
由于inotify特性需要Linux内核的支持,在安装inotify-tools前要先确认Linux系统内核是否达到了 2.6.13以上,如果Linux内核低于2.6.13版本,就需要重新编译内核加入inotify的支持,也可以用如下方法判断,内核是否支持 inotify:
# uname -r 2.6.18-164.11.1.el5PAE # ll /proc/sys/fs/inotify total 0 -rw-r--r-- 1 root root 0 04-13 19:56 max_queued_events -rw-r--r-- 1 root root 0 04-13 19:56 max_user_instances -rw-r--r-- 1 root root 0 04-13 19:56 max_user_watches
如果有上面三项输出,表示系统已经默认支持inotify,接着就可以开始安装inotify-tools了。摘自 http://www.open-open.com/lib/view/open1423121939623.html
1. 两台服务器
1.1 centos6.5 192.168.0.218 要备份的资源服务器
1.2 centos6.5 192.168.0.219 备份服务器
2. 配置备份服务器 192.168.0.219
2.1 安装rsync 参考我前面写的博客 http://www.onephper.com/archive/21.html 需要注意的是,可以参考一下我这边备份服务器的配置
2.2 配置conf文件
# vi /etc/rsyncd.conf pid file = /var/run/rsyncd.pid port = 873 uid = root gid = root use chroot = no read only = no max connections =20 lock file = /var/run/rsync.lock log file = /var/log/rsync.log motd file = /etc/rsyncd.motd timeout = 300 [backup] path = /home/test # 自动备份到这台服务器的/home/test目录 comment = rsync files ignore errors read only = no list = no hosts allow = 192.168.0.218/24 hosts deny = * auth users = backup secrets file = /etc/rsyncd.secrets
2.3 配置 rsyncd.secrets 文件
# vi /etc/rsyncd.secrets backup:x13966498715
3.配置资源服务器 192.168.0.218
3.1 下载inotify-tools tar 包
# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz # tar -zxvf inotify-tools-3.14.tar.gz # cd inotify-tools-3.14 # ./configure # make && make install
3.2 安装rsync 并建立rsynd.passwd 文件
# vi /etc/rsyncd.passwd x13966498715
3.3 测试 是否可以与备份服务器连通(执行下列命令如没有报错,则可以直接传送文件了)
# /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.passwd backup@192.168.0.219::backup /home/test
3.4 编写 inotify.sh 实时监控文件
# vi inotify.sh #!/bin/bash host01=192.168.0.219 #备份服务器的ip地址 src=/home/test #本地监控的目录 dst=backup #备份服务器的rsync服务的模块名 user=backup #备份服务器的rsync服务的虚拟用户 rsync_passfile=/etc/rsyncd.passwd #本地调用rsync服务的密码文件 inotify_home=/usr/local #inotify的安装目录 if [ ! -e "$src" ] \ || [ ! -e "${rsync_passfile}" ] \ || [ ! -e "${inotify_home}/bin/inotifywait" ] \ || [ ! -e "/usr/bin/rsync" ]; then echo "Check File and Folder" exit 9 fi ${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src | while read file do cd $src && /usr/bin/rsync -aruz -R --delete ./ --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} > /home/inotify.log 2>&1 done exit 0
3.4 ./inotify.sh & #将脚本加入后台执行
3.5 在 /home/test 文件中新建一个文件 f.txt
# echo 'f' > f.txt
3.6 如果现在你没有配置错误,在备份服务器 192.168.0.219 这台服务器上立刻就可以看到f.txt这个文件了
4. inotify之inotifywait命令常用参数详解
#/usr/local/bin/inotifywait --help -r|--recursive Watch directories recursively. #递归查询目录 -q|--quiet Print less (only print events). #打印监控事件的信息 -m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received. #始终保持事件监听状态 --excludei <pattern> Like --exclude but case insensitive. #排除文件或目录时,不区分大小写。 --timefmt <fmt> strftime-compatible format string for use with %T in --format string. #指定时间输出的格式 --format <fmt> Print using a specified printf-like format string; read the man page for more details.#打印使用指定的输出类似格式字符串 -e|--event <event1> [ -e|--event <event2> ... ] Listen for specific event(s). If omitted, all events are listened for. #通过此参数可以指定需要监控的事件,如下所示: Events: access file or directory contents were read #文件或目录被读取。 modify file or directory contents were written #文件或目录内容被修改。 attrib file or directory attributes changed #文件或目录属性被改变。 close file or directory closed, regardless of read/write mode #文件或目录封闭,无论读/写模式。 open file or directory opened #文件或目录被打开。 moved_to file or directory moved to watched directory #文件或目录被移动至另外一个目录。 move file or directory moved to or from watched directory #文件或目录被移动另一个目录或从另一个目录移动至当前目录。 create file or directory created within watched directory #文件或目录被创建在当前目录 delete file or directory deleted within watched directory #文件或目录被删除 unmount file system containing file or directory unmounted #文件系统被卸载
5. 参考链接
http://chocolee.blog.51cto.com/8158455/1400596 rsync+inotify实现实时同步案例
http://www.open-open.com/lib/view/open1423121939623.html 通过rsync+inotify实现文件的实时备份同步
结束语
本文有任何错误,或有任何疑问,欢迎留言说明。
网友最新评论