zoukankan      html  css  js  c++  java
  • 通过rsync+inotify实现数据实时备份

    • rsync的优点与不足

    与传统的cp,scp,tar,备份方式相比,rsync具有安全性高备份迅速支持增量备份的优点,可以满足对实时性要求不高的需求,例如定期备份文件服务器数据到远端服务器,但是,当数据量非常大的时候,单独使用rsync就突显出有些不足,rsync在同步数据时,需要扫描所有文件后进行对比,然后进行差量传输,扫描所有文件是非常耗时的操作,最后只同步了很少的内容,rsync不能实时监测,同步数据。

    • inotify的作用

    inotify是一种强大的异步的文件系统事件监控机制。通过inotify可以监控文件系统中添加,删除,修改,移动等各种事件,利用inotify,第三方软件可以监控文件系统下文件的各种变化情况。在linux中的安装包名是inotify-tools

    • 安装查看系统环境

    inotify-tools需要2.6.13以上的内核支持,查看内核版本和系统对inotify的支持

    [root@localhost ~]#uname -r
    3.10.0-693.el7.x86_64
    [root@localhost ~]#ll /proc/sys/fs/inotify/
    total 0
    -rw-r--r-- 1 root root 0 Apr  6 00:10 max_queued_events
    -rw-r--r-- 1 root root 0 Apr  6 00:10 max_user_instances
    -rw-r--r-- 1 root root 0 Apr  6 00:10 max_user_watches
    • 安装inotify
    [root@localhost ~]#yum install inotify-tools -y
    [root@localhost ~]#inotifywa
    inotifywait   inotifywatch

    安装完inotify后,会生成inotifywait和inotifywatch两个指令,inotifywait用于等待文件或者文件集上的一个特定的事件,可以监听任何文件和目录的设置,并且可以递归地监控整个目录树;

    inotifywatch用于收集被监控的文件系统统计数据,包括每个inotify事件发生多少次等信息。

    • inotify相关系统参数

    inotify定义了一些系统参数用来限制inotify消耗内存的大小,参数设置文件在/proc/sys/fs/inotify下

    • max_queued_events值:表示调用inotify_init时分配到instances中可排队的event数的最大值,超出这个值的事件被丢弃
    • max_user_instances值:表示每一个real user ID 可以创建的inotify instatnces数量的上限
    • max_user_watches值:表示每个inotify实例可以监控的最大目录数量
    • inotifywait相关参数

                -m:–monitor,表示始终保持事件监听状态

                -r:–recursive,表示递归查询目录

                -q:–quiet,表示打印出监控事件

                -e:–event,通过这个参数可以指定要监控的事件,常见的事件有modify,delete,create,和attrib等

                –timefmt:指定时间的输出格式

                –format:指定变化文件的详细信息

     在上一篇介绍rsync使用的文章基础上,基于原来的系统环境,实现rysnc+inotify的实时同步

    【当rsync结合inotify时,原来的server端就变成了客户端了,当server端的文件更新就会向远程主机同步传输数据此时远程的客户端就成了server,,,有点绕】

    生产服务器主机A的地址:192.168.214.190  :centos7.4  (rsync+inotify) 本地客户端

    备份数据主机B的地址:192.168.214.187     :centos6.9    (rsync)            远程节点服务端

    结合工作时,远程的服务节点需要将rsync以守护进程的方式启动,并将rsync加入到自启动文件中

    #以守护进程方式启动
    [root@yufu ~]# /usr/bin/rsync --daemon --config=/etc/rsync.d/rsyncd.conf
    #加入开机自启动
    [root@yufu ~]# echo '/usr/bin/rsync --daemon --config=/etc/rsync.d/rsyncd.conf' >> /etc/rc.local

    生产服务器只需安装好rsync+inotify,并定义inotifywait监控及同步脚本就好了,(除了在/etc/rsync.d/下添加同步用户的密码文件外,定义密码文件后在同步过程中不需要再客户端输入密码,在脚本中要指定这个文件位置)

    echo  fsz… >> /etc/rsync.d/server.pass

    在生产服务器主机A的地址:192.168.214.190  :centos7.4  (rsync+inotify)上编写脚本实现自动同步

    #/bin/bash
    ###
    ##
    #
    backupdir=/databak
    srcdir=/data
    bakmod=bak
    #备份的用户,不是真正的系统用户
    user=feng
    host1=192.168.214.187
    
    /usr/bin/inotifywait -mrq --timefmt '%F %T' --format '%T %w%f%e' -e modify,delete,create,attrib $srcdir | while read files
            do
                    /usr/bin/rsync -vzrtopg --delete --progress  $srcdir $user@$host1::$bakmod --password-file=/etc/rsync.d/server.pass >> /tmp/rsync.log 2>&1
                    echo "${files} was rsynced" >> /var/log/rsync.log 2>&1
            done

    对于脚本内容参数做简单理解:

    backupdir=/databak  :远程服务器存放备份文件的目录
    srcdir=/data  :本地要备份的数据目录
    bakmod=bak   远程服务器定义的rsyncd.conf中的模块名称
    user=feng   :执行备份的用户,该用户是rsyncd.conf中“auth users”指定的用户
    host1=192.168.214.187   :远程主机地址

    这个脚本的作用就是同inotify监控文件目录的变化,进而触发rsync进行同步操作

    给脚本添加执行权限

    [root@localhost data]#chmod 755 inotifyrsync.sh

    指令方式测试同步

    #远程上传
    [root@localhost data]#rsync -zvrtopg --progress /data feng@192.168.214.187::bak --password-file=/etc/rsync.d/server.pass 
    sending incremental file list
    data/
    data/.inotifyrsync.sh.swp
           12288 100%    0.00kB/s    0:00:00 (xfer#1, to-check=7/9)
    data/
             473 100%   65.99kB/s    0:00:00 (xfer#2, to-check=6/9)
    data/access.log
        21328307 100%   42.91MB/s    0:00:00 (xfer#3, to-check=5/9)
    data/epel-release-latest-7.noarch.rpm
           15080 100%   30.49kB/s    0:00:00 (xfer#4, to-check=4/9)
    data/fengsuzhou.txt
               0 100%    0.00kB/s    0:00:00 (xfer#5, to-check=3/9)
    data/gudaoyufu.txt
               0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=2/9)
    data/inotifyrsync.sh
             476 100%    0.96kB/s    0:00:00 (xfer#7, to-check=1/9)
    data/lost+found/
    
    sent 1232415 bytes  received 149 bytes  821709.33 bytes/sec
    total size is 21356624  speedup is 17.33
    
    

    测试下载

    #远程下载
    [root@localhost data]#rsync -vzrtopg --progress  feng@192.168.214.187::bak /opt/app/ --password-file=/etc/rsync.d/server.pass 
    receiving incremental file list
    ./
    .inotifyrsync.sh.swp
           12288 100%   11.72MB/s    0:00:00 (xfer#1, to-check=9/11)
    data/
    data/.inotifyrsync.sh.swp
           12288 100%   11.72MB/s    0:00:00 (xfer#2, to-check=7/11)
    data/
             473 100%  461.91kB/s    0:00:00 (xfer#3, to-check=6/11)
    data/access.log
        21328307 100%   44.90MB/s    0:00:00 (xfer#4, to-check=5/11)
    data/epel-release-latest-7.noarch.rpm
           15080 100%   32.51kB/s    0:00:00 (xfer#5, to-check=4/11)
    data/fengsuzhou.txt
               0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=3/11)
    data/gudaoyufu.txt
               0 100%    0.00kB/s    0:00:00 (xfer#7, to-check=2/11)
    data/inotifyrsync.sh
             476 100%    1.02kB/s    0:00:00 (xfer#8, to-check=1/11)
    data/lost+found/
    
    sent 216 bytes  received 1233016 bytes  822154.67 bytes/sec
    total size is 21368912  speedup is 17.33

    将脚本放入后台执行

    [root@localhost data]#sh /data/inotifyrsync.sh  &
    [1] 2096

    再做最后的总结:被搞得够晕的

    客户端:(需要备份数据的那台服务器)

    生产服务器只需安装好rsync+inotify

    在/etc/rsync.d/下添加同步用户的密码文件,在脚本中要指定这个文件位置

    编写inotifywait监控及同步脚本

    服务端:(同步数据,存放备份数据的那台服务器)

    只需安装rsync,定义配置文件

    配置文件中定义 模块名称,备份数据的用户,及数据存放的路径

    定义备份用户的密码文件:username:password 格式

    必须以守护进程的方式启动

    最后再贴上rsync的 rsyncd.conf 配置文件(需要手动编写)

    vim /etc/rsync.d/rsyncd.conf
    
    log file = /var/log/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    
    [bak]
    path=/databak
    uid = root
    gid = root
    ignore = errors
    read only = no
    write only = no
    host allow = *
    max connections = 5
    host deny = 192.168.22.21
    list = false
    auth users = feng
    secrets file = /etc/rsync.d/server.pass

    同步测试

    在客户端上的备份源目录中创建文件

    [root@localhost data]#ls
                                     file12.txt  file18.txt  file4.txt  gudaoyufu.txt
    access.log                        file13.txt  file19.txt  file5.txt  inotifyrsync.sh
    epel-release-latest-7.noarch.rpm  file14.txt  file1.txt   file6.txt  lost+found
    fengsuzhou.txt                    file15.txt  file20.txt  file7.txt  zhuhui.txt
    file10.txt                        file16.txt  file2.txt   file8.txt
    file11.txt                        file17.txt  file3.txt   file9.txt

    在服务端存放备份的目录查看新同步的文件

    [root@yufu data]# ls
                                     file12.txt  file18.txt  file4.txt  gudaoyufu.txt
    access.log                        file13.txt  file19.txt  file5.txt  inotifyrsync.sh
    epel-release-latest-7.noarch.rpm  file14.txt  file1.txt   file6.txt  lost+found
    fengsuzhou.txt                    file15.txt  file20.txt  file7.txt  zhuhui.txt
    file10.txt                        file16.txt  file2.txt   file8.txt
    file11.txt                        file17.txt  file3.txt   file9.txt11111
  • 相关阅读:
    反射+自定义注解,实现获取注解标记的属性
    jeecg导出Excel
    Date类的getYear(),getMonth过时,现在的获取方法
    Mysql的sql语句,Delete 中包含 not in
    EhCache与Redis的比较
    SpringMVC+Bootstrap项目
    按位运算| ^
    按步长检索数据,放缓有问题数据的处理
    DateTime 格式相比较,timestampdiff() 函数的运用
    作用域在函数定义时就已经确定了。而不是在函数调用时确定
  • 原文地址:https://www.cnblogs.com/anay/p/8724493.html
Copyright © 2011-2022 走看看