linux之实时备份rsync+inotify

rsync缺点/不足:

1.rsync在同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的,并且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。
2.rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。

linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除、修改、移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施监控的软件。
在使用rsync首次全量同步后,结合inotify对源目录进行实时监控,只有有文件变动或新文件产生,就会立刻同步到目标目录下,非常高效使用!

inotify :创建一个文件描述符,附加一个或多个监视器(一个监视器是一个路径和一组事件),然后用read方法从描述符获取事件,read并不会用完整个周期,它是事件发生之前是被阻塞的。

文件描述符:linux内核为了更优秀的管理被打开的文件创建的索引,是一个非负整数。用于指代被打开的文件,所有执行io的操作 系统调用都是通过文件描述符。0表示标准输入,1表示标准输出,2表示标准错误输出。


查看linux内核版本

需要看当前linux是否支持inotify

[root@Rsync-139 ~]# uname -a
Linux Rsync-139 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
[root@Rsync-139 ~]# ls -lsart /proc/sys
sys/           sysrq-trigger  sysvipc/
[root@Rsync-139 ~]# ls -lsart /proc/sys/fs/inotify/
总用量 0
0 dr-xr-xr-x 0 root root 0 11月  5 15:31 ..
0 dr-xr-xr-x 0 root root 0 11月  6 16:12 .
0 -rw-r--r-- 1 root root 0 11月  6 16:12 max_user_watches
0 -rw-r--r-- 1 root root 0 11月  6 16:12 max_user_instances
0 -rw-r--r-- 1 root root 0 11月  6 16:12 max_queued_events

安装

[root@Rsync-139 ~]# tar -zxvf inotify-tools-3.14.tar.gz -C /usr/local/src/
[root@Rsync-139 ~]# ./configrue --prefix=/usr/local/inotify
[root@Rsync-139 ~]# make &&make install

需要加环境变量

vim /etc/profile
最低行加:/usr/local/inotify/bin/

重新加载配置文件

. /etc/profile 或者
source /etc/profile

查看帮助信息

扫描二维码关注公众号,回复: 3958165 查看本文章

inotifywait --help

[root@WebA-136 script]# inotifywait --help
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:可用选项
        -h|--help       Show this help text.
        @<file>         Exclude the specified file from being watched.
        --exclude <pattern>
                        Exclude all events on files matching the
                        extended regular expression <pattern>.
        --excludei <pattern>
                        Like --exclude but case insensitive.
        -m|--monitor    Keep listening for events forever.  Without
                        this option, inotifywait will exit after one
                        event is received.
        -d|--daemon     Same as --monitor, except run in the background
                        logging events to a file specified by --outfile.
                        Implies --syslog.
        -r|--recursive  Watch directories recursively.
        --fromfile <file>
                        Read files to watch from <file> or `-' for stdin.
        -o|--outfile <file>
                        Print events to <file> rather than stdout.
        -s|--syslog     Send errors to syslog rather than stderr.
        -q|--quiet      Print less (only print events).
        -qq             Print nothing (not even events).
        --format <fmt>  Print using a specified printf-like format
                        string; read the man page for more details.
        --timefmt <fmt> strftime-compatible format string for use with
                        %T in --format string.
        -c|--csv        Print events in CSV format.
        -t|--timeout <seconds>
                        When listening for a single event, time out after
                        waiting for an event for <seconds> seconds.
                        If <seconds> is 0, inotifywait will never time out.
        -e|--event <event1> [ -e|--event <event2> ... ]
                Listen for specific event(s).  If omitted, all events are
                listened for.

Exit status:可以监控的事件
        0  -  An event you asked to watch for was received.
        1  -  An event you did not ask to watch for was received
              (usually delete_self or unmount), or some error occurred.
        2  -  The --timeout option was given and no events occurred
              in the specified interval of time.

Events:
        access          file or directory contents were read
        modify          file or directory contents were written
        attrib          file or directory attributes changed
        close_write     file or directory closed, after being opened in
                        writeable mode
        close_nowrite   file or directory closed, after being opened in
                        read-only mode
        close           file or directory closed, regardless of read/write mode
        open            file or directory opened
        moved_to        file or directory moved to watched directory
        moved_from      file or directory moved from 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
        delete_self     file or directory was deleted
        unmount         file system containing file or directory unmounted

常用

-r 递归(目录)

-m 永久监听

-d 后台运行

-q 静默,只输出较少的信息

编写脚本进行实时备份

vim inotify.sh
inotifywait -mrq --timefmt '%d%m%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move /var/log/ |while read file
do
rsync -az --delete-before /var/log/ [email protected]::WebA/ --password-file=/etc/WebA.pass
done
./inotify &放入后台执行即可

注:

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at

main.c(1505)是因为在同步的时候,源目录下有软链接文件!

rsync同步软链接文件,应该加参数-l


参考文档:https://www.cnblogs.com/kevingrace/p/6001252.html  此文档非常详细。

猜你喜欢

转载自blog.51cto.com/12107790/2313578