• rsync+inotify


    rsync+inotify
    在内核2.6.13起,加入了inotify支持
    介绍:rsync扫描所有文件后对比,进行差量备份,inotify可以监控文件系统添加,删除修改,移动的各种事件
    在使用rsync首次全量同步后,结合inotify源目录进行实时监控,当文件变动和新文件产生,就会同步到目标目录下,高效使用

    案例:(网站图片备份方案)
    分别将
    192.168.133.1的/data/test1和/data/test2
    192.168.133.2的/data/test3和/data/test4
    实时同步到
    192.168.133.3的/home/backup/image-back目录下对应的test1,test2,test3,test4
    1,2这两台服务器是源服务器,作为rsync的客户端,部署rsync+inotify
    3是目标服务器,作为rsync的服务端,部署rsync



    详细部署过程
    第一部分:在目标服务器上部署rsync服务
    在192.168.133.3中部署rsync服务端
    1)关闭SELinux
    cat /etc/selinux/config
    SELINUX=disabled
    setenforce 0

    2)防火墙上允许以上2台服务器访问22端口和873端口
    vi /etc/sysconfig/iptables
    -A INPUT -s 192.168.133.1 -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    -A INPUT -s 192.168.133.1 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
    省略。。。。。。
    /etc/init.d/iptables restart

    如果在host.allow里面作了限制,同样开放三台服务器的权限
    vim /etc/hosts.allow
    sshd:192.168.133.1,192.168.133.2:allow
    sshd:all:deny
    cat /etc/hosts.deny
    无内容

    3)安装部署rsync服务,CentOS中是以xinetd来管理Rsync服务的
    yum install rsync xinetd
    vim /etc/xinetd.d/rsync
    disabled = no           默认的yes改为no,设置开机启动rsync
    /etc/init.d/xinetd start
    vi /etc/rsyncd.conf
    log file = /var/log/rsyncd.log          #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
    pidfile = /var/run/rsyncd.pid           #pid文件的存放位置
    lock file = /var/run/rsync.lock            #支持max connections参数的锁文件
    secrets file = /etc/rsync.pass    用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
    motd file = /etc/rsyncd.Motd                rsync启动时欢迎信息页面文件位置(自己创建这个文件,内容随便自定义)

    [test1]                                    自定义名称,最好用1,2的服务器目录
    path = /home/backup/image-back/test1    rsync服务端数据目录路径,即同步到目标目录后的存放路径
    comment = test1                            和自定义名字相同
    uid = nobody                #设置rsync运行的uid权限。这个要保证同步到目标目录后的权限和源目录一致,即都是nobody!
    gid = nobody                   #设置rsync运行的gid权限。
    port=873                       #默认的rsync端口
    use chroot = no                 #默认为true,修改为no或false,增加对目录文件软连接的备份
    read only = no                  #设置rsync服务端文件为读写权限
    list = no                       #不显示rsync服务端资源列表
    max connections = 200           #最大连接数
    timeout = 600                           #设置超时时间
    auth users = RSYNC_USER       执行数据同步的用户名,需要后面手动设置。可以设置多个,用英文状态下逗号隔开
    hosts allow = 192.168.133.1     #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    hosts deny =                    #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
                                    (如果没有禁止,就不用设置这一行)
    [test2]
    path = /home/backup/image-back/test2
    comment = test2
    uid = nobody
    gid = nobody
    port=873
    use chroot = no
    read only = no
    list = no
    max connections = 200
    timeout = 600
    auth users = RSYNC_USER
    hosts allow = 192.168.133.1
    同理,test3,test4 一样配置,更改path,comment,注意可能gid和pid不同,hosts allow
    test3和4,hosts allow改成192.168.133.2


    创建用户认证文件, 设置文件权限,即rsyncd.conf和rsync.pass认证文件都是600权限!
    vim /etc/rsync.pass
    RSYNC_USER:123456@rsync
    chmod 600 /etc/rsyncd.conf
    chmod 600 /etc/rsync.pass
    重启rsync服务
    /etc/init.d/xinetd restart
     lsof -i:873
     
     
    4)最后,创建rsync同步过来后的目标目录
    cd /home/backup/image-back/
    mkdir test1 test2 test3 test4
     ll
    total 40
    drwxr-xr-x. 8 nobody nobody 4096 Jun 12 17:25         test1
    drwxrwxrwx. 584 nobody nobody 20480 Oct 26 13:41     test2
    drwxr-xr-x. 11 nobody nobody 4096 Oct 26 14:23         test3
    drwxr-xr-x. 10 nginx nginx 4096 Oct 26 13:44        test4




    第二部分,在源服务器上192.168.133.1和2部署rsync客户端和inotify监控

    1)两台机器同样操作
    关闭selinux,做为客户端的rsync可以不用在iptables里开放873端口
    vim /etc/selinux/config
    SELINUX=disabled
     setenforce 0
    2)两台机器同样操作
    安装rsync
     yum install rsync xinetd
     vim /etc/xinetd.d/rsync
     disable = no   
     /etc/init.d/xinetd start
    lsof -i:873
    创建同步的密码文件,这个文件名可以跟服务端的认证文件不一样,但是里面的密码必须一致!
    用于rsync同步命令中。不过,最好两边的文件设置成一样,便于管理
    vim /etc/rsync.pass
    123456@rsync
    chmod 600 /etc/rsync.pass

    查看服务器内核是否支持inotify,出现下面的内容,说明服务器内核支持inotify
     ll /proc/sys/fs/inotify
    total 0
    -rw-r--r-- 1 root root 0 Oct 26 12:03 max_queued_events
    -rw-r--r-- 1 root root 0 Oct 26 12:03 max_user_instances
    -rw-r--r-- 1 root root 0 Oct 26 12:03 max_user_watches
    Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核

    下面开始安装inotify-tools
    yum install make gcc gcc-c++       
    cd /use/local/src
    wget https://sourceforge.net/projects/inotify-tools/files/latest/download --no-check-certificate
    tar zxvf inotify-tools-3.14.tar.gz
    cd inotify-tools-3.14
    ./configure --prefix=/usr/local/inotify
     make && make install
     
    发现已经成功安装inotify-tools了
    ll -d /usr/local/inotify/
     drwxr-xr-x 6 root root 4096 Oct 26 12:01 /usr/local/inotify/
     
    设置系统环境变量
    vim /etc/profile
    export PATH=$PATH:/usr/local/inotify/bin
    source /etc/profile
     
     添加库文件
    vim /etc/ld.so.conf
    /usr/local/inotify/lib

    ldconfig
     
     修改inotify默认参数(inotify默认内核参数值太小)
    查看系统默认参数值
     
    [root@static-img ~]# 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

    修改参数:
    [root@static-img ~]# sysctl -w fs.inotify.max_queued_events="99999999"
     sysctl -w fs.inotify.max_user_watches="99999999"
     sysctl -w fs.inotify.max_user_instances="65535"
     
     参数说明:
    max_queued_events:
    inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
    max_user_watches:
    要同步的文件包含多少目录,可以用:find /data/test1    -type d | wc -l 统计这些源目录下的目录数,
    必须保证max_user_watches值大于统计结果(这里/data/test1为同步的源文件目录)
    max_user_instances:
    每个用户创建inotify实例最大值

    4)接着执行同步操作
    在192.168.133.1服务器上
     
    第一次全量同步
    rsync -avH --port=873 --progress --delete /data/test1 RSYNC_USER@192.168.133.3::test1
    --password-file=/etc/rsync.pass
     
    rsync -avH --port=873 --progress --delete /data/test2 RSYNC_USER@192.168.133.3::test2
    --password-file=/etc/rsync.pass
     待第一次rsync全量同步完成后,就进行rsync+inotify实时同步脚本操作
    rsync+inotify实时同步
    cd /home/rsync/
    cat rsync_test1_inotify.sh
    cat rsync_test2_inotify.sh

    然后启动同步脚本,放在后台执行
    nohup sh rsync_test1_inotify.sh&
    nohup sh rsync_test2_inotify.sh&

    ps -ef查看程序进程pid
    kill可以杀死其进程

    最后进行测试:
    在源目录/data/test1中创建一个文件或者目录,会自动实时同步到目标机器192.168.133.3的目标目录
    /home/backup/image-back/test1中

    如果在同步过程中,发现中途报错!重复执行同步命令一直是报这个错误:
    rsync error: some files/attrs were not transferred (see previous errors) (code 23) at
    main.c(1505)

    最后发现原因:
    是因为在同步的时候,源目录下有软链接文件!
    rsync同步软链接文件,应该加参数-l
    最好在使用rsync同步命令的时候,后面跟-avpgolr参数组合

    脚本文件rsync_test1_inotify.sh
    #!/bin/bash
    SRCDIR=/Data/test1
    USER=RSYNC_USER
    IP=192.168.133.3
    DESTDIR=test1
    /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 $SRCDIR | while read file
    do
    /usr/bin/rsync -avpgorl    --port=873 --progress --delete-before $SRCDIR $USER@$IP::$DESTDIR --password-file=/etc/rsync.pass
    echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
    done




    rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
    方法一:
    将192.169.133.3服务端的rsyncd.conf配置文件的UID和gid分别改成root,重新加载
    service xinetd reload,再次执行同步,同步成功
    ####此种方法,执行完同步后,为了安全,将UID和gid修改回来,或修改成nobody

    方法二:将需要同步的文件夹和下属文件赋予777权限,再次执行同步,同步成功。

  • 相关阅读:
    latex之插入伪代码 [转]
    BIBTeX制作参考文献 [转]
    latex 页眉设置 [转]
    python : list tuple set dictionary [转]
    ctags使用简介 [转]
    conda在指定目录下创建虚拟环境
    Ubuntu系统安装Anaconda3
    ModuleNotFoundError: No module named 'google' 问题解决方案
    PyCharm无法输入中文
    checkpoint文件
  • 原文地址:https://www.cnblogs.com/fengzhongzhuzu/p/8670168.html
Copyright © 2020-2023  润新知