zoukankan      html  css  js  c++  java
  • 通过rsync搭建一个远程备份系统(二)

    Rsync+inotify实时备份数据

    rsync在同步数据的时候,需要扫描所有文件后进行对比,然后进行差量传输,如果文件达到了百万或者千万级别以上是,扫描文件的时间也很长,而如果只有少量的文件变更了,那么此时rsync是非常低效的。所以此时需要一个实时监控文件变化的软件结合rsync来做实时数据备份。

    what's inotify

    inotify是一个强大的,细粒度的,异步的文件系统事件监控机制,linux内核从2.6.13开始加了这个功能,可以通过监控文件系统添加,删除,修改移动等事件,利用这个接口,第三方软件可以监控文件系统添加,删除,修改移动等事,inotify-tools就是这样的一个第三方软件。

    install it

    安全前首先确认内核大于2.6.13以上,以下重新编译内核加入inotify的支持,具体方法请自查。

    [root@salt ~]# uname -r
    2.6.32-504.el6.x86_64
    [root@salt ~]# ll /proc/sys/fs/inotify/
    total 0
    -rw-r--r-- 1 root root 0 Nov  4 02:53 max_queued_events
    -rw-r--r-- 1 root root 0 Nov  4 02:53 max_user_instances
    -rw-r--r-- 1 root root 0 Nov  4 02:53 max_user_watches
    

    如果 ll 后显示有三个,那么说明支持inotify了。
    既然系统支持,那么接下来安装它 。
    官网地址是:https://github.com/rvoicilas/inotify-tools
    我这里的下载地址是:https://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

    [root@salt tmp]# tar zxf inotify-tools-3.14.tar.gz 
    [root@salt tmp]# cd inotify-tools-3.14
    [root@salt inotify-tools-3.14]# ./configure --prefix=/usr/local/inotifytools && make && make install 
    
    [root@salt inotify-tools-3.14]# cd /usr/local/inotifytools/
    [root@salt inotifytools]# ls
    bin  include  lib  share
    [root@salt inotifytools]# ll bin/inotifywa*
    -rwxr-xr-x. 1 root root 44319 Nov  4 03:00 bin/inotifywait
    -rwxr-xr-x. 1 root root 41425 Nov  4 03:00 bin/inotifywatch
    

    安装好inotify-tools,会生成inotifywait 或者 inotifywatch 两个指令,

    • inotifywait用于等待文件或者文件集上的特定事件,可以监控任何文件和目录设置,并且可以递归地监控整个目录树
    • inotifywatch用于收集被监控的文件系统统计数据,包括整个inotify时间发生多少次等信息。
    inotify 相关参数

    inotify定义了一些接口参数,用来限制inotify消耗kernel memory的大小。由于这些参数都是内存参数,因此根据实际情况调节其大小,下面介绍下这些参数:

    • /proc/sys/fs/inotify/max_queued_events 表示调用了inotify_init时分配到inotify_instance中可排队的event数最大值,超过这值就会被丢弃,但会出发IN_Q_QVRFLOW事件
    • /proc/sys/fs/inotify/max_user_instances 表示每一个real_user ID可创建的inotify instances数量上限。
    • /proc/sys/fs/inotify/max_user_watches 表示每个inotify 实例相关联的watchs上限,也就是inotify实例监控最大文件的数量,如果文件数量巨大,这个值也需要调高。如我们修改成8百万
    [root@localhost inotify-tools-3.14]# cat /proc/sys/fs/inotify/max_user_watches
    8192
    [root@localhost inotify-tools-3.14]# echo "3000000" > /proc/sys/fs/inotify/max_user_watches
    [root@localhost inotify-tools-3.14]# cat /proc/sys/fs/inotify/max_user_watches
    3000000
    
    inotifywait 相关参数

    inotifywait是一个监控等待事件,可以配合shell脚本来使用它,下面说下常用的几个参数:

    • -m (--monitor) 表示处于一直保持时间监听的状态
    • -r (--recursive) 表示递归查询目录
    • -d (--daemon) 守护进程运行
    • -q (--quiet) 表示打印出监控事件
    • -c (--event) 通过此参数可以指定要监控的事件,常见的事件有modify,delete,create和attrib等等
      man inotifywait可以查询到更多的参数

    公司应用案例:rsync+inotify搭建实时同步系统

    网站架构简单概要

    这是一个应用商店业务,前端haproxy负载均衡,中间nginx作为web服务节点,网站数据访问都到后端存储节点,后端存储节点与公司app发布节点通过rsync+inotify进行同步,此时你也许会问了,为什么不直接把APP放在后端存储节点了呢,我们当时是这样考虑:

    • 存储节点是有多台服务器,如果要上传app的话,就得上传很多台服务器,我们通过inotify+rsync就可以上传到发布机上,自己自动同步到多台后端存储服务器了,这样就实现了上传到一台,同步到多台的效果。
    • 服务器都在阿里云,发布机在内网,公司需要访问的话,直接访问内部发布机即可,避免访问阿里云的服务器,占用它的带宽资源。

    所以下面就通过inotify+rsync来实现app文件的自动同步了

    环境介绍:

    以下为实验环境,但是其生产情况和这类似。只不过是服务器数量多了几台罢了。

    • 192.168.141.3 发布机 安装inotify+rsync
    • 192.168.141.4 存储节点 rsync服务端模式

    我们在刚才的操作中,已经在192.168.141.3上的发布机安装好了rsync+inotify的,192.168.141.4上把rsync配置成服务器模式,发布机配制成客户端模式,这样发布机上一有东西都自动同步到存储节点。

    在存储节点(192.168.141.4)上配置rsync
    [root@localhost ~]# useradd store        # 往store家目录下同步数据
    [root@localhost ~]# cat /etc/rsyncd.conf
    uid = nobody
    gid = nobody
    use chroot = no
    max connections = 10
    strict modes = yes
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    [appstore]
    path = /home/store
    comment = app file
    ignore errors
    read only = no
    write only = no
    hosts allow = *
    list = false
    uid = root
    gid = root
    auth users = store
    secrets file = /etc/server.pass
    [root@localhost ~]# cat /etc/server.pass
    store:123..aa
    [root@localhost ~]# chmod 600  /etc/server.pass
    [root@localhost ~]# ll   /etc/server.pass
    -rw-------. 1 root root 14 Nov  5 01:42 /etc/server.pass    # 千万不要忘记600权限
    
    在发布机(192.168.141.3)上的配置

    确保inotify安装正确后,我们首先手动测试下看rsync能不能够正常传输东西

    [root@localhost ~]# rsync -vzrtopg --delete --progress /home/store store@192.168.141.4::appstore
    Password:
    sending incremental file list
    store/
    store/.bash_logout
              18 100%    0.00kB/s    0:00:00 (xfer#1, to-check=7/9)
    ....一堆东西
    

    显然,上面的状态说明rsync正常工作的,接下来写一个脚本,实现rsync+inotify结合工作

    [root@localhost ~]# cat syncapp.sh
    #!/bin/bash
    node1="192.168.141.4"       # 如果有多个服务端,可以node2=xx,node3=xxx,总之自由发挥吧
    src=/home/store/            # 确保目录存在
    dst=appstore
    user=store
    /usr/local/inotifytools/bin/inotifywait  -mrq --timefmt '%d/%m/%y %H:%M'  --format '%T %w%f%e' -e close_write,delete,create,attrib $src 
    | while read file
    do
        rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src  $user@$node1::$dst
        echo "${file}  was rsynced" >> /var/log/rsyncd.log  2&>1
    done
    

    脚本相关参数如下:

    • --timefmt 指定时间的输出格式
    • --format 指定变化文件的详细信息
    • -e close_write,delete,create,attrib 需要注意的这个close_write,表示等文件写完以后在触发rsync同步,
    • -e modify,delete,create,attrib
    测试是否正常工作

    我们在发布机下的/home/store上放一个东西,或者新建一个目录,删除一个东西,如果存储节点能够自动同步过去,那么就正常了。。。。

  • 相关阅读:
    MS SQL float转nvarchar
    RDLC添加页码
    RDLC报表分页显示标题
    程序员必读书单
    SQL查询结果增加序号列
    SQL字符串分组聚合(分组后的数据查询后用逗号隔开)
    现在有一列是int的,把它变成5位的字符串,左边不够的补0
    oracle客户端精简绿色版-环境变量配置
    DISTINCT后按照DISTINCT之前的某列进行排序
    设计模式之代理模式
  • 原文地址:https://www.cnblogs.com/liaojiafa/p/6033520.html
Copyright © 2011-2022 走看看