• rsync+inotify实时同步目录数据


    rsync+inotify实时同步目录数据

    为什么使用rsync+inotify

      配合rsync实现文件自动同步,利用inofity监听某个目录,当此目录发生变化时,触发rsync实现文件同步

      单独rsycn同步数据时,rsync先需要扫描所有文件进行对比,进行差量传输。在数据达到百万级别时,扫描文件时非常耗时的,往往就出现了瓶颈。

      rsync不能实时同步数据,而且触发同步数据时也会有时间差,造成数据不一致等情况。

    rsync同步命令中常用的几个参数说明:
    -a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
    -v, --verbose 详细模式输出
    -p, --perms 保持文件权限
    -g, --group 保持文件属组信息
    -o, --owner 保持文件属主信息
    -r, --recursive 对子目录以递归模式处理。同步目录的时候要加上这个参数
    -l, --links 保留软链结,加上这个参数,同步过来的文件会保持之前的软链接属性不变
    -H, --hard-links 保留硬链结
    -e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
    -z, --compress 对备份的文件在传输时进行压缩处理
    --stats 给出某些文件的传输状态
    --progress 打印同步的过程
    --timeout=TIME 同步过程中,IP超时时间,单位为秒
    --delete 删除那些目标目录中有而源目录中没有的多余文件。这个是rsync做增量方式的全备份的最佳选择方案!!!!!!
    --delete-before 接受者在输出之前进行删除操作。即先将目标目录中文件全部删除,再将源目录文件拷贝过去。这是rsync保持目标目录跟源目录一致的方案!!!
    --delete-after 在同步操作之后做比较,删除那些目标目录中有而源目录中没有的多余文件
    --delete-excluded 删除目标目录中那些被该选项指定排除的文件
    --ignore-errors 即使出现IO错误也进行删除,忽略错误
    --exclude 指定同步时需要过滤掉的文件或子目录(即不需要同步过去的),后面直接跟不需要同步的单个文件名或子目录(不需要跟路径) ,过滤多个文件或子目录,就使用多个--exclude
    --exclude-from 指定同步时需要过滤掉的文件或子目录,后面跟文件(比如/root/exclue.txt),然后将不需要同步的文件和子目录放到/root/exclue.txt下。
    --version 打印版本信息
    --port=PORT 指定其他的rsync服务端口
    --log-format=formAT 指定日志文件格式
    --password-file=FILE 从FILE中得到密码
    --bwlimit=KBPS 限制I/O带宽,KBytes per second
    

    案例说明:

      机器

      client  192.168.2.200

      client  192.168.2.202

      server  192.168.2.201

      需要将192.168.2.200 /data目录 和 192.168.2.202 /test目录实时同步到192.168.2.201 /home/backup对应的 data和test目录

    一、server部署过程

      1、部署前操作

    [root@cc ~]# vim /etc/sysconfig/selinux	#关闭selinux
    SELINUX=disabled
    [root@cc ~]# setenforce 0
    setenforce: SELinux is disabled
    
    #允许22号端口和873端口进来
    [root@cc ~]# iptables -I INPUT -s 192.168.2.200 -p tcp --dport 22 -j ACCEPT
    [root@cc ~]# iptables -I INPUT -s 192.168.2.200 -p tcp --dport 873 -j ACCEPT
    [root@cc ~]# iptables -I INPUT -s 192.168.2.202 -p tcp --dport 22 -j ACCEPT
    [root@cc ~]# iptables -I INPUT -s 192.168.2.202 -p tcp --dport 873 -j ACCEPT
    

       2、安装部署rsync服务

    [root@cc ~]# yum -y install rsync xinetd
    [root@cc ~]# systemctl enable rsyncd	#开机自启
    Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
    
    [root@cc ~]# systemctl restart rsyncd
    [root@cc ~]# vim /etc/rsyncd.conf
    log file = /var/log/rsyncd.log  #日志存放路径,无需手动创建
    pidfile = /var/run/rsyncd.pid   #pid文件存放路径
    lock file = /var/run/rsyncd.lock        #支持max connections参数的锁文件
    secrets file = /etc/rsync.pass  #用户认证配置文件,里面保存用户名称和密码,后面需要创建
    motd file = /etc/rsyncd.Motd    #rsync启动时欢迎信息页面配置文件(内容自己定义)
    
    [data]  #名称自定义
    path = /home/backup/data        #rsync服务端数据目录路径,即同步到目标目录后存放路径
    comment = data  #模块名称和[data]自定义名称一样
    uid = nginx     #设置rsync运行的uid权限,要保证同步到目标目录后的权限和源目录一致,即都是nginx
    gid = nginx     #gid权限
    port = 873      #默认rsnyc端口
    use chroot = no #默认为ture,改为no或者false,增加对目录文件软连接的备份
    read only = no  #设置rsync服务器文件为读写权限
    list = no       #不显示rsnyc服务器资源列表
    max connections = 200   #最大链接数
    timeout = 600   #设置超时时间
    auth users = RSYNC_USER #执行数据同步的用户名,后面手动设置。可以设置多个,用,隔开
    hosts allow = 192.168.2.200     #允许进行数据同步的客户端ip地址,可以设置多个,用,隔开
    #hosts deny = 192.168.2.200     #禁止数据同步的客户端ip地址,可以设置多个,用,隔开,如果没有,则不用设置此行
    
    [test]
    path = /home/backup/test	###centos6可以不写后面的test,如果写了到时候同步的路径为/home/backup/test/test
    comment = test
    uid = root
    gid = root
    port = 873
    use chroot = no
    read only = no
    list = no
    max connections = 200
    timeout = 600
    auth users = RSYNC_USER
    hosts allow = 192.168.2.202
    

       3、设置密码文件以及修改权限

    [root@cc ~]# cat /etc/rsync.pass 	#用户认证文件
    RSYNC_USER:123456@rsync
    
    [root@cc ~]# chmod 600 /etc/rsyncd.conf	#设置文件权限
    [root@cc ~]# chmod 600 /etc/rsync.pass
    

       4、创建同步过来目录

    [root@cc ~]# systemctl restart rsyncd	#重启服务
    [root@cc ~]# netstat -tunlp | grep 873
    tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      6429/rsync          
    tcp6       0      0 :::873                  :::*                    LISTEN      6429/rsync
    
    [root@cc ~]# cd /home/backup/	#创建同步过来后的目标目录
    [root@cc backup]# mkdir test
    [root@cc backup]# mkdir data
    [root@cc backup]# chown nginx:nginx data -R
    [root@cc backup]# chown test:test test -R
    

    二、server部署过程

      2台机器操作一样

      1、部署前准备工作

    [root@1ogg2zbl ~]# vim /etc/sysconfig/selinux	#关闭selinux
    SELINUX=disabled
    [root@1ogg2zbl ~]# setenforce 0
    setenforce: SELinux is disabled
    
    [root@1ogg2zbl ~]# yum -y install rsync xinetd
    

       2、启动rsync以及配置认证密码

    [root@1ogg2zbl ~]# vim /etc/xinetd.d/rsync
    disable = no	#由默认的yes改为no,设置开机启动rsync
    
    [root@1ogg2zbl ~]# /etc/init.d/xinetd restart
    Stopping xinetd:                                           [FAILED]
    Starting xinetd:                                           [  OK  ]
    [root@1ogg2zbl ~]# netstat -tunlp | grep 873
    tcp        0      0 :::873                      :::*                        LISTEN      1959/xinetd
    
    [root@1ogg2zbl ~]# vim /etc/rsync.pass	#认证文件
    123456@rsync
    [root@1ogg2zbl ~]# chmod 600 /etc/rsync.pass	#设置权限
    

      3、查看机器是否支持inofity

    [root@1ogg2zbl ~]# ll /proc/sys/fs/inotify/	#出现下列内容,说明服务器内核支持inofity 注:linux下inofity内核最小为2.6.13
    total 0
    -rw-r--r-- 1 root root 0 Nov 21 14:52 max_queued_events
    -rw-r--r-- 1 root root 0 Nov 21 14:52 max_user_instances
    -rw-r--r-- 1 root root 0 Nov 21 14:52 max_user_watches
    

       4、部署inofity

    [root@1ogg2zbl src]# cd /usr/local/src/
    [root@1ogg2zbl src]# yum -y install make g1ogg2zbl g1ogg2zbl-c++ wget
    [root@1ogg2zbl src]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
    [root@1ogg2zbl src]# tar -xf inotify-tools-3.14.tar.gz 
    [root@1ogg2zbl src]# cd inotify-tools-3.14
    [root@1ogg2zbl inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
    [root@1ogg2zbl inotify-tools-3.14]# make && make install
    
    [root@1ogg2zbl inotify-tools-3.14]# vim /etc/profile	#设置环境变量
    export export PATH=$PATH:/usr/local/inotify/bin
    [root@1ogg2zbl inotify-tools-3.14]# source  /etc/profile
    [root@1ogg2zbl inotify-tools-3.14]# vim /etc/ld.so.conf
    /usr/local/inotify/lib
    [root@1ogg2zbl inotify-tools-3.14]# ldconfig
    
    [root@1ogg2zbl inotify-tools-3.14]# sysctl -a | grep max_queued_events	#查看以及修改默认inofity参数大小
    fs.inotify.max_queued_events = 16384
    [root@1ogg2zbl inotify-tools-3.14]# sysctl -a | grep max_user_watches
    fs.inotify.max_user_watches = 8192
    fs.epoll.max_user_watches = 798556
    [root@1ogg2zbl inotify-tools-3.14]# sysctl -a | grep max_user_instances
    fs.inotify.max_user_instances = 128
    [root@1ogg2zbl inotify-tools-3.14]# sysctl -w fs.inotify.max_queued_events="99999999"	#inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
    fs.inotify.max_queued_events = 99999999
    [root@1ogg2zbl inotify-tools-3.14]# sysctl -w fs.inotify.max_user_watches="99999999"	#要同步的文件包含多少目录,可以用:find /data -type d | wc -l 统计这些源目录下的目录数,必须保证max_user_watches值大于统计结果(这里/data为同步的源文件目录)
    fs.inotify.max_user_watches = 99999999
    [root@1ogg2zbl inotify-tools-3.14]# sysctl -w fs.inotify.max_user_instances="65535"	#每个用户创建inotify实例最大值
    fs.inotify.max_user_instances = 65535
    

       5、开始同步

    ##第一次执行rsync全量同步(加上--delete参数,保持目标目录和源目录文件一致)
    [root@cc /]# rsync -avH --port=873 --progress --delete /test RSYNC_USER@192.168.2.201::test --password-file=/etc/rsync.pass
    
    sending incremental file list
    test/
    test/aa
               0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/2)
    
    sent 105 bytes  received 31 bytes  272.00 bytes/sec
    total size is 0  speedup is 0.00
    
    ##接下来使用rsync+inofity实时同步脚本操作
    ##实时同步脚本里添加的是--delete-before参数,而不是--delete参数(第一次全量同步时rsync用的参数),二者区别:
    ##	--delete参数:表示rsync同步前,暴力删除目标目录中的所有文件,然后再执行同步操作。
    ##	--delete-before参数:表示rsync同步前,会先对目标目录进行一次扫描检索,删除目标目录中对比源目录的多余文件,然后再执行同步操作。显然比--delete参数安全些。
    
    [root@cc /]# useradd rsync
    [root@cc /]# cd /home/rsync/
    [root@cc rsync]# ll
    total 0
    [root@cc rsync]# cat rsync_test_inotify.sh 
    #!/bin/bash
    
    SRCDIR=/$1
    USER=RSYNC_USER
    IP=192.168.2.201
    DESTDIR=$1
    
    /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 -avH --port=873 --progress --delete-before /${SRCDIR} RSYNC_USER@192.168.2.201::${DESTDIR} --password-file=/etc/rsync.pass
    	echo "${file} was rsyncd" >> /tmp/rsync.log 2>&1
    done
    
    [root@cc test]# nohup sh /home/rsync/rsync_test_inotify.sh test &	#后台执行
    
    [root@cc test]# su - test	#测试
    su: warning: cannot change directory to /home/test: Permission denied
    -bash: /home/test/.bash_profile: Permission denied
    -bash-4.1$ cd /test/
    -bash-4.1$ mkdir ggg
    -bash-4.1$ touch nnn
    
    [root@cc backup]# ll /home/backup/test/test/
    total 48
    drwxrwxr-x 2 test test  4096 Nov 21 16:02 ggg
    -rw-rw-r-- 1 test test     0 Nov 21 16:02 nnn
    

     

  • 相关阅读:
    09 shell脚本程序练习
    springboot整合vue03-创建springboot工程
    springboot整合vue02-创建前端页面
    springboot整合vue01-创建vue工程
    pe工具04-获取数据目录
    pe工具03-获取节表信息
    pe工具02-解析pe头信息
    pe工具01-获取进程和模块
    以挂起方式创建进程
    进程
  • 原文地址:https://www.cnblogs.com/jcici/p/9995803.html
Copyright © 2020-2023  润新知