集群04-inotify工具实现nfs存储服务器实时复制到backup备份服务器

集群04-inotify工具实现nfs存储服务器实时复制到backup备份服务器

一、添加yum源

[root@bogon yum.repos.d]# wget http://mirrors.aliyun.com/repo/epel-6.repo

二、安装inotify-tools

[root@nfs01 yum.repos.d]# yum -yinstall inotify-tools                             

三、查看是否包含三个重要文件

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

总用量 0

-rw-r--r-- 1 root root 0 3月  22 19:09max_queued_events

-rw-r--r-- 1 root root 0 3月  22 19:09max_user_instances

-rw-r--r-- 1 root root 0 3月  22 19:09max_user_watches

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

max_user_watches:设置inotifywaitinotifywatch命令可以监视的文件数量(单进程)max_user_instances:设置每个用户可以运行的inotifywaitinotifywatch命令的进程数。max_queued_events设置inotify实例事件(event)队列可容纳的事件数量。

三、inotifywait命令监控文件或目录的变化

[root@nfs01 inotify]# inotifywait -mrq --timefmt'%d/%m/%y %H:%M' --format '%T %w %e %f' -e create,delete,modify,close_write/backup/

 

常用参数:
--timefmt 时间格式
%y年 %m月 %d日 %H小时 %M分钟
--format 输出格式
%T时间 %w路径 %f文件名 %e状态
-m 始终保持监听状态,默认触发事件即退出。
-r 递归查询目录
-q 打印出监控事件
-e 定义监控的事件,可用参数:
open打开文件 attrb  属性变更

access文件读取

modify文件更改。

attrib文件属性更改,如权限,时间戳等。

close_write以可写模式打开的文件被关闭,不代表此文件一定已经写入数据。

close_nowrite以只读模式打开的文件被关闭。

close文件被关闭,不管它是如何打开的。

open文件打开。

moved_to一个文件或目录移动到监听的目录,即使是在同一目录内移动,此事件也触发。

moved_from一个文件或目录移出监听的目录,即使是在同一目录内移动,此事件也触发。

move包括moved_to和 moved_from

move_self文件或目录被移除,之后不再监听此文件或目录。

create文件或目录创建

delete文件或目录删除

delete_self文件或目录移除,之后不再监听此文件或目录

unmount文件系统取消挂载,之后不再监听此文件系统。

四、实时监控脚本实现备份

#!/bin/sh

Path=/data

backup_server_ip=172.16.1.41

 

inotifywait -mrq --timefmt'%d/%m/%y %H:%M' --format '%T %w %f %e' -e create,close_write,delete $Path |while read result;

do

    #statements

    if [[ -f $result ]]; then

        #statements

        rsync -az --delete $result rsync_backup@$backup_server_ip::backup--password-file=/etc/rsync.password

    else

        cd $Path

        rsync -az --delete ./ rsync_backup@$backup_server_ip::backup--password-file=/etc/rsync.password

    fi

done

五、将实时监控脚本设置开机启动

加入脚本路径为:/server/scripts/inotify.sh

[root@nfs01inotify]# echo "/bin/sh/server/scripts/inotify.sh &" >> /etc/rc.local

一个& 代表从后台开始运行该条命令

 

6、使用start/stop启动或关闭脚本

#!/bin/bash

#chkconfig: 2345 38 46

#################################

# author:killer12998

# mail:[email protected]

# qq:2747481521

#################################

 

. /etc/init.d/functions

 

start(){

    /bin/bash./inotify_rsync.sh &

    if [[ !-e /usr/local/inotify/i.pid ]]; then

       cd/usr/local && mkdir inotify && touch i.pid

    fi

    echo$$>/usr/local/inotify/i.pid

    if [[ `ps-ef | grep inotify | wc -l` -gt 2 ]]; then

       action'inotify starting:' /bin/true

    else

       action'inotify starting:' /bin/false

    fi

}

stop(){

    kill -9`cat /usr/local/inotify/i.pid` &> /dev/null

    pkillinotifywait

    sleep 1

    if [[ `ps-ef | grep -v grep | grep inotifywait | wc -l` -eq 0 ]]; then

       action'inotify stopped' /bin/true

    else

       action'inotify stopped' /bin/false

    fi

}

 

case $1 in

    start )

       start

       ;;

    stop )

       stop

       ;;

    restart|reload)

       stop

       start

       ;;

    * )

       echo"usage:$0 {start|stop|restart|reload}"

       exit 1

       ;;

esac

 

你可能感兴趣的:(集群)