6、inotify实时备份


备份用户nfs共享文件系统,存储单点解决方案inotify+rsync(增量,无差异备份),inotify是单线程,

inotify是一种强大的,细粒度的,异步的文件系统事件监控机制,通过加入了inotify可以实时监控文件

系统中添加,删除,修改,移动等各种事件 ,sersync比inotify有更多功能;

6.1、实现从nfs(rsync)存储端到rsync(rsync --daemon)服务器的数据传送 :

6.2、在nfs服务器上安装inotify服务,实现nfs客户端对nfs目录文件系统事件的实时监控:

1、查看nfs服务器是否支持inotify

ls -l /proc/sys/fs/inotify/

2、安装inotify包

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #安装第三方源

yum -y install inotify-tools

[root@nfs01 ~]# rpm -qa inotify-tools

inotify-tools-3.14-1.el6.x86_64

3、工具介绍

inotifywait:在被监控的文件或目录上等待待定文件系统事件(open,close,delete等)发生,

执行后处于阻塞状态,适用在shell脚本中使用;

-r#递归查询目录

-q#打印很少的信息,仅仅打印监控事件的信息

-m#始终保持事件监听状态

--excludei#排除文件或目录时不区分大小写

--timefmt#指定时间输出的格式

--format#打印使用指定的输出类似格式的字符串

-e#通过此参数可以指定需要监控的事件

access#文件或目录被读取

modify#文件或目录内容被修改

close_write:修改

inotifywatch:收集被监视的文件系统,使用统计数据,指文件系统时间发生的次数;

举例:

[root@nfs01 ~]# inotifywait -mrq --timefmt '%F %H:%M' --format '%T %w%f' -e close_write,create,delete /tmp

2018-10-14 15:47 /tmp/a

--timefmt:时间的格式;

--format:对设置的格式进行执行,%T代表时间,%w%f代表目录和文件;

4、当客户端监控到nfs共享文件系统事件变化后,触发rsync推送数据:

vim /server/scripts/inotify.sh

#!/bin/sh

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Path=/data

Ip=172.16.1.41

/usr/bin/inotifywait -mrq --format '%w%f' -e close_write,create,delete $Path/ \

|while read file;do

if [ -f $file ];then #$file读取的是‘%w%f’的地址格式

rsync -az $file --delete rsync_backup@$Ip::nfsbackup/ --password-file=/etc/rsync.password >/dev/null 2>&1 &

else

rsync -az $Path/ --delete rsync_backup@$Ip::nfsbackup/ --password-file=/etc/rsync.password >/dev/null 2>&1 &

fi

done

#/usr/bin/inotifywait -mrq --timefmt '%F %H%M' --format '%T %w%f' -e close_write,create,delete $Path/ \

chown u+x inotify.sh

4、启动脚本:

sh inotify.sh

[root@nfs01 ~]# ps -ef | grep inotify

root 2034 1972 0 13:25 pts/0 00:00:00 sh /server/scripts/inotify.sh

root 2035 2034 0 13:25 pts/0 00:00:00 /usr/bin/inotifywait -mrq --format %w%f -e close_write,create,delete /data/

root 2036 2034 0 13:25 pts/0 00:00:00 sh /server/scripts/inotify.sh

root 2063 2039 0 13:25 pts/1 00:00:00 grep inotify

4、加入到 开机自启动中:

echo “/bin/sh /server/scripts/inotify.sh”>/etc/rc.local

5、在backup服务器上进行查看watch ls 每两秒刷新一次

6.3、inotify系统调优:

1、在/proc/sys/fs/inotify 目录下有三个文件,对inotify机制有一定的限制:

[root@nfs01 inotify]# ls -l /proc/sys/fs/inotify/

总用量 0

-rw-r--r-- 1 root root 0 10月 15 13:13 max_queued_events #默认是16334

-rw-r--r-- 1 root root 0 10月 15 13:13 max_user_instances #128

-rw-r--r-- 1 root root 0 10月 15 13:13 max_user_watches #8192

max_queued_events:设置inotify实例事件(event)队列可容纳的事件数量

max_user_watches:设置inotifywait或inotifywatch命令可以监视的文件数量

max_user_instance:设置每个用户可以运行的inotifywait或intoifywait命令的进程数

2、最好写入到/etc/rc.local自启文件中,防止重启后失效:

echo '655350'> /proc/sys/fs/inotify/max_queued_events

echo '655350' > /proc/sys/fs/inotify/max_user_watches



猜你喜欢

转载自www.cnblogs.com/LiuChang-blog/p/12313879.html