• 文件实时同步(rsync+inotify)


    目标服务器:10.11.6.11

    源服务器:10.11.6.12

    准备条件:

      1、关闭selinux;

      2、开启防火墙tcp 873端口(或关闭防火墙)

      3、安装rsync

         yum -y install xinetd rsync gcc

      CentOS7 需要编写/etc/xinetd.d/rsync

    # default: off
      # # description: The rsync server is a good addition to an ftp server, as it 
      # # allows crc checksumming etc.
      service rsync
      {
      disable = no   #6上默认的配置为yes,需改成no 
      flags = IPv6
      socket_type = stream
      wait = no
      user = root
      server = /usr/bin/rsync
      server_args = --daemon
      log_on_failure += USERID
      }

      #启动服务并检查端口

      systemctl restart xinetd
    
      lsof -i:873

      #如xinetd无法启动,则可以直接启动rsyncd(只需启动目标服务器的rsyncd服务)

      

        systemctl restart rsyncd

    一:目标服务器

    1、建立rsin用户并设置密码

    echo "rsin:password" > /etc/rsync.passwd #可以设置多个,每行一个用户名:密码,源服务器只需密码
    chmod 600 /etc/rsync.passwd

    2、配置rsyncd.conf

    uid = root
    #设置rsync运行权限为root
    gid = root
    #设置rsync运行权限为root 
    port = 873
    #默认端口 
    use chroot = no
    #默认为true,修改为no,增加对目标文件软连接的备份
    strict modes = yes
    #是否检查用户家目录和rhosts文件的权限和所有权
    timeout = 600
    #设置超时时间
    pid file = /var/run/rsyncd.pid
    #pid文件的存放位置
    lock file = /var/run/rsync.lock
    #支持max connections参数的锁文件
    log file = /var/log/rsyncd.log
    #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
    [get]
    #自定义模块名称
    path = /home/rsync_inoti/get
    #rsync目标目录路径
    comment = get
    #模块名称与[get]自定义名称相同
    read only = no
    #设置rsync服务端文件为读写权限
    write only = no
    #设置rsync服务端文件为读写权限
    hosts allow = 172.16.15.223
    #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    list = false
    #不显示rsync服务端资源列表
    uid = root
    gid = root
    auth users = rsin
    #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
    max connections = 200
    #最大连接数
    secrets file = /etc/rsync.passwd

    一:源服务器

    1、建立rsync密码认证文件

      echo "password" > /etc/rsync.passwd #可以设置多个,每行一个用户名:密码,源服务器只需密码
    
      chmod 600 /etc/rsync.passwd #两台均需要

    2、安装inotify

      #下载地址

      github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

      #安装(需要gcc gcc-c++)

      解压后

      ./configure --prefix=/usr/local/inotify && make && make install

      #修改inotify默认参数(inotify默认内核参数值太小)、  

    查看系统默认参数值
    sysctl -a | grep max_queued_events
    结果是:fs.inotify.max_queued_events
    = 16384
    sysctl
    -a | grep max_user_watches 结果是:fs.inotify.max_user_watches = 8192 sysctl -a | grep max_user_instances 结果是:fs.inotify.max_user_instances = 128 修改参数: sysctl -w fs.inotify.max_queued_events="99999999" sysctl -w fs.inotify.max_user_watches="99999999" sysctl -w fs.inotify.max_user_instances="65535" vi /etc/sysctl.conf #添加以下代码 fs.inotify.max_queued_events=99999999 fs.inotify.max_user_watches=99999999 fs.inotify.max_user_instances=65535 :wq! #保存退出 参数说明: max_queued_events: inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确 max_user_watches: 要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录) max_user_instances: 每个用户创建inotify实例最大值

       #测试rsync是否正常工作

      rsync -avH --port=873 --progress --delete 测试.txt rsin@目标IP::get(目标模块名) --password-file=/etc/rsync.passwd

      #编写脚本,实时触发rsync进行同步 

    #!/bin/sh
    srcdir=/usr/nginx-1.10.2/
    #源服务器同步目录,加'/'同步该目录下边的内容,不加‘/’则会把这个目录拷贝到目标指定位置
    dstdir=get
    #目标服务器rsync同步目录模块名称
    excludedir=/usr/all_shell/exclude.list
    #不需要同步的目录,如果有多个,每一行写一个目录,无需绝对路径,使用相对于同步模块的路径;
    rsyncuser=rsin #目标服务器rsync同步用户名
    rsyncpassdir=/etc/rsync.passwd #目标服务器rsync同步用户的密码在源服务器的存放路径
    dstip="10.11.6.12" #目标服务器ip,多个ip用空格分开
    for ip in $dstip
    do
    rsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
    done
    /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move --fromfile '/usr/all_shell/inotify-list' | while read file
    do
    for ip in $dstip
    do
    rsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir
    echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
    done
    done

    #注意/usr/local/inotify/bin/inotifywait中--fromfile '/usr/all_shell/inotify-list'可以编写多个实时监控的目录和需要排除的目录,用@区分,加@的为要排除的目录,如

      /usr/nginx-1.10.2 #要监控的文件及目录

      @/usr/nginx-1.10.2/logs/  #不监控的文件及目录

      @/usr/nginx-1.10.2/on   #不监控的文件及目录

     也还可以通过--exclude ‘logs’ 指定排除某一个文件或目录

      具体参考

      #在目标服务器的rsync安装并启动rsync之后,再启动该脚本(后台启动),可以考虑加入到开机启动项中

      echo "/home/rsync_inoti/push.sh &" >> /etc/rc.local

       

     

     inotify参数说明

    选项:
        -m:表示持续监视变化。 
        -r:表示使用递归形式监视目录。 
        -q:表示减少冗余信息,只打印出需要的信息。 
        -e:表示指定要监视的事件列表。 
        --timefmt是指定时间的输出格式。 
        --format指定文件变化的详细信息。其中 %w:表示监听的目录,%f表示触发事件的文件
    
    事件:
        access  访问,读取文件。 
        modify  修改,文件内容被修改。 
        attrib  属性,文件元数据被修改。 
        move    移动,对文件进行移动操作。 
        create  创建,生成新文件 
        open    打开,对文件进行打开操作。 
        close   关闭,对文件进行关闭操作。 
        delete  删除,文件被删除。

      rsync参数说明 

    -v, --verbose 详细模式输出
    -q, --quiet 精简输出模式
    -c, --checksum 打开校验开关,强制对文件传输进行校验
    -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
    -r, --recursive 对子目录以递归模式处理
    -R, --relative 使用相对路径信息
    -b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
    --backup-dir 将备份文件(如~filename)存放在在目录下。
    -suffix=SUFFIX 定义备份文件前缀
    -u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
    -l, --links 保留软链结
    -L, --copy-links 想对待常规文件一样处理软链结
    --copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结
    --safe-links 忽略指向SRC路径目录树以外的链结
    -H, --hard-links 保留硬链结
    -p, --perms 保持文件权限
    -o, --owner 保持文件属主信息
    -g, --group 保持文件属组信息
    -D, --devices 保持设备文件信息
    -t, --times 保持文件时间信息
    -S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
    -n, --dry-run现实哪些文件将被传输
    -W, --whole-file 拷贝文件,不进行增量检测
    -x, --one-file-system 不要跨越文件系统边界
    -B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
    -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
    --rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
    -C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
    --existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
    --delete 删除那些DST中SRC没有的文件
    --delete-excluded 同样删除接收端那些被该选项指定排除的文件
    --delete-after 传输结束以后再删除
    --ignore-errors 及时出现IO错误也进行删除
    --max-delete=NUM 最多删除NUM个文件
    --partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
    --force 强制删除目录,即使不为空
    --numeric-ids 不将数字的用户和组ID匹配为用户名和组名
    --timeout=TIME IP超时时间,单位为秒
    -I, --ignore-times 不跳过那些有同样的时间和长度的文件
    --size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
    --modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
    -T --temp-dir=DIR 在DIR中创建临时文件
    --compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
    -P 等同于 --partial
    --progress 显示备份过程
    -z, --compress 对备份的文件在传输时进行压缩处理
    --exclude=PATTERN 指定排除不需要传输的文件模式
    --include=PATTERN 指定不排除而需要传输的文件模式
    --exclude-from=FILE 排除FILE中指定模式的文件
    --include-from=FILE 不排除FILE指定模式匹配的文件
    --version 打印版本信息
    --address 绑定到特定的地址
    --config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
    --port=PORT 指定其他的rsync服务端口
    --blocking-io 对远程shell使用阻塞IO
    -stats 给出某些文件的传输状态
    --progress 在传输时现实传输过程
    --log-format=formAT 指定日志文件格式
    --password-file=FILE 从FILE中得到密码
    --bwlimit=KBPS 限制I/O带宽,KBytes per second
    -h, --help 显示帮助信息

     

     参考链接

    https://www.cnblogs.com/MacoLee/p/5633650.html

    https://www.cnblogs.com/liu1026/p/7436412.html

    https://www.osyunwei.com/archives/7435.html

  • 相关阅读:
    css圆,背景,img填满等样式
    MySQL双日志
    MySQL分层和查询数据的流程
    ZJNU 2345
    ZJNU 2342
    ZJNU 2340/2341/2343
    ZJNU 2235
    ZJNU 2226
    ZJNU 2212
    ZJNU 2208
  • 原文地址:https://www.cnblogs.com/suminem/p/10601494.html
Copyright © 2020-2023  润新知