zoukankan      html  css  js  c++  java
  • 备份服务-Rsync

    什么是 Rsync?

      Rsync 是一款开元的,快速的,多功能的, 可以实现全量及增量的本地或者远程数据同步备份的工具. 并且可以不进行改变原有的数据的属性信息. 实现数据的备份迁移特性. Rsync 软件适用于 unix/ linux/ windows 等多种操作系统.

        Rsync 是一个快速和非常通用的文件复制工具. 他能本地复制,远程复制, 或者远程守护进程方式复制. 他提供了大量的参数来控制其运行的行为方式.

    如何实现的增量传输数据
      1) 利用rsync程序算法(判断数据属性信息是否一致), 判断不同主机上的数据是否一致
      2) 利用rsync程序算法(判断文件md5数值是否一致), 判断不同主机上的数据是否一致

    Rsync单机应用

    软件安装

    # 如果可以查询到 证明已经安装
    [root@test2 ~]# rpm -qa | grep rsync
    rsync-3.1.2-6.el7_6.1.x86_64
    
    # 安装执行
    yum install rsync
    

    单机常用命令

    1. 本地拷贝文件
    rsync -avz /etc/hosts /tmp/
    
    2. 查看文件属性
    rsync /tmp/hosts 
    
    3. 本地拷贝文件夹
    rsync -ravz /etc /tmp/
    
    4. 删除文件,需指定空目录
    mkdir /test
    rsync --delete-before --force -d /test/ /tmp/
    
    5. 远程拷贝文件
    rsync -avz /etc/hosts root@172.16.0.111:/tmp/

    参数说明

    -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指定模式匹配的文件。
    --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 显示帮助信息

    守护进程方式

    服务端配置

    [root@test4 www]# cat /etc/rsyncd.conf 
    #指定rsync服务运行的时候向磁盘写入读取的操作者
    uid = rsync
    gid = rsync
    #安全相关
    use chroot = yes
    #最大连接数
    max connections = 10
    #指定PID文件路径
    pid file = /var/run/rsyncd.pid
    #锁文件
    lock file = /var/run/rsync.lock
    #日志文件
    log file = /var/log/rsyncd.log
    # exclude = lost+found/
    #开启Rsync数据传输日志功能
    transfer logging = yes
    #超时时间
    timeout = 900# ignore nonreadable = yes
    #指定那些文件在传输的时候不进行压缩处理
    dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
     
     
    #模块名称
    [backup]
    #模块对应位置
    path = /www
    #忽略错误文件
    ignore errors
    #是否只读
    read only = false
    #是否可以列表
    list = false
    #允许rsync白名单
    hosts allow = 172.16.0.0/24
    #禁止访问黑名单
    hosts deny = 0.0.0.0/32
    #不存在的用户,只用于认证
    auth users = rsync_backup,rsync_yan
    #设置进行连接的秘钥
    secrets file = /etc/rsync.password

    创建用户及文件夹:

    useradd -s /sbin/nologin -M rsync
    mkdir /www
    chown -R rsync. /www/

    创建认证文件:

    [root@test4 ~]# cat /etc/rsync.password 
    rsync_backup:yan123456
    [root@test4 ~]# chmod 600 /etc/rsync.password

    创建systemd 管理文件:

    [root@test4 ~]# cat /usr/lib/systemd/system/rsync.service
    [Unit]
    Description=The rsync server
    After=network.target 
    
    [Service]
    Type=forking
    PIDFile=/var/run/rsyncd.pid
    ExecStartPost=/bin/sleep 0.1
    ExecStartPre=/usr/bin/rm -f /var/run/rsyncd.pid
    ExecStart=/usr/bin/rsync --config=/etc/rsyncd.conf --daemon
    KillSignal=SIGQUIT
    TimeoutStopSec=5
    KillMode=process
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target

    启动进程并检查:

    [root@test4 ~]# systemctl start rsync
    [root@test4 ~]# ps -ef | grep rsync
    root      53224      1  0 16:32 ?        00:00:00 /usr/bin/rsync --config=/etc/rsyncd.conf --daemon
    root      53227  50997  0 16:32 pts/0    00:00:00 grep --color=auto rsync

    客户端配置

    配置密码文件

    [root@test1 ~]# cat /etc/rsync.password 
    yan123456
    [root@test1 ~]# chmod  600 /etc/rsync.password

    用法

    推送数据
    #密码验证模式
    [root@Linux-node5-example ~]# rsync -avz /root/tui1 rsync_backup@172.16.0.114::backup
    Password: 
    #免秘钥交互模式  
    [root@Linux-node5-example ~]# rsync -avz /root/tui2 rsync_backup@172.16.0.114::backup --password-file=/etc/rsync.password
    
    拉取数据
    #密码认证模式
    [root@Linux-node5-example ~]# rsync -avz rsync_backup@172.16.0.114::backup /www/ Password: 
    #免密码模式
    [root@Linux-node5-example ~]# rsync -avz rsync_backup@172.16.0.114::backup /www/ --password-file=/etc/rsync.password
    

    Inotify + rsync

        Inotify 是一款强大的,细粒度的异步的文件系统事件监控软件. Linux 内核从 2.6.13 起对 Inotify的支持.

        Inotify 可以监控文件系统中的,添加,删除,修改,移动等各种事情.

        客户端启动 Inotify 服务进程, 监测共享目录的数据变化.获取到共享目录数据发生变化通知 rsync 同步数据.

    部署 Inotify

    部署软件

    [root@test1 ~]# yum install epel-*
    [root@test1 ~]# yum install inotify-tools

    调优

    [root@test1 ~]# cd /proc/sys/fs/inotify/
    [root@test1 inotify]# ll
    总用量 0
    -rw-r--r--. 1 root root 0 4月  10 13:25 max_queued_events      # 设置实例时间最大事件数量
    -rw-r--r--. 1 root root 0 4月  10 13:25 max_user_instances      # 设置每个用户可以运行多少个命令
    -rw-r--r--. 1 root root 0 4月  10 13:25 max_user_watches       # 设置可以监听的文件数量(单进程)
    [root@test1 inotify]# echo "5000000" >/proc/sys/fs/inotify/max_user_watches
    [root@test1 inotify]# echo "5000000" >/proc/sys/fs/inotify/max_queued_events

    参数说明

    1. 指令说明
    -m	     始终保持事件监听状态
    -d	     类似-m参数,开启一个守护进程方式后台运行
    -q	     只输出事件信息
    --timefmt	指定事件输出格式
    --format	打印使用指定的输出类似格式字符串,
    --excludei	排除过滤文件或者目录
    -s	        发送错误到syslog,错误输出
    -O	        打印事件到文件中,正确输出
    -e	        指定监听的事件,省略则表示所有事件都监听
    
    2. -e参数指定的事件
    access	文件或者目录内容被读取
    modify	文件或者目录内容被写入
    attrib	文件或者目录属性发生变化
    close_write	文件或目录关闭,在写入模式打开之后关闭
    close_nowrite	文件或者目录关闭,在只读模式打开之后关闭
    close	                文件或者目录关闭,不管读或者写模式
    open	                文件或者目录打开
    moved_to	        文件或者目录被移动到监控的目录中
    moved_from	文件或者目录从监控的目录中被移动
    move	        文件或者目录不管移动到或者是移动出都触发事件
    create	        在监控目录中创建文件或者目录
    delete	        删除
    delete_self	删除
    unmount	        文件系统包含的文件或目录不能卸载
    
    3. inotifywait参数--format <>格式参数
    
    %w	    时间出现时,监控文件或者目录的名称信息
    %r	    时间出现时,显示监控目录下触发事件的文件或者目录信息,否则为空
    %e	    显示发生的事件信息,不同的事件信息用逗号进行分隔
    %Xe	    指定分隔符
    %T	    输出时间格式中定义时间格式信息

    自动监测脚本

    cat inotify.sh 
    #!/bin/sh
    cmd="/usr/bin/inotifywait"
    dir="/tmp"
    bak_dir="/tmp"
    $cmd -mrq $dir --format '%w%f' -e create,close_write,delete,moved_to|
    while read line;do
        rsync -az --delete $bak_dir rsync_backup@172.16.0.114::backup --password-file=/etc/rsync.password
    done

    Sersync + Rsync

        Sersync 支持配置文件管理,以守护进程方式启动,可以对失败文件定时重传(定时任务功能)

        支持 第三方的 http 接口(更新CDN缓存).默认多线程同步.

     下载地址: https://github.com/wsgzao/sersync

    软件部署

    [root@test1 ~]# unzip sersync-master.zip 
    [root@test1 ~]# cd sersync-master/
    
    [root@test1 sersync-master]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz 
    [root@test1 sersync-master]# mkdir -p /usr/local/sersync/{conf,bin,log}
    [root@test1 sersync-master]# cd GNU-Linux-x86/
    
    [root@test1 GNU-Linux-x86]# cp confxml.xml /usr/local/sersync/conf/
    [root@test1 GNU-Linux-x86]# cp sersync2 /usr/local/sersync/bin/sersync
    
    [root@test1 GNU-Linux-x86]# cd /usr/local/sersync/
    [root@test1 sersync]# tree 
    .
    ├── bin
    │   └── sersync
    ├── conf
    │   └── confxml.xml
    └── log

    编辑配置文件

    [root@test1 bin]# cat ../conf/confxml.xml 
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <head version="2.5">
        <host hostip="localhost" port="8008"></host>
        <debug start="false"/>
        <fileSystem xfs="false"/>
    <!--过滤排除同步数据-->
        <filter start="false">
    	<exclude expression="(.*).svn"></exclude>
    	<exclude expression="(.*).gz"></exclude>
    	<exclude expression="^info/*"></exclude>
    	<exclude expression="^static/*"></exclude>
        </filter>
    <!--监控指定的事件-->
        <inotify>
    	<delete start="true"/>
    	<createFolder start="true"/>
    	<createFile start="false"/>
    	<closeWrite start="true"/>
    	<moveFrom start="true"/>
    	<moveTo start="true"/>
    	<attrib start="false"/>
    	<modify start="false"/>
        </inotify>
    <!--监控的目录信息,推送的主机信息-->
        <sersync>
    	<localpath watch="/tmp/">
    	    <remote ip="172.16.0.114" name="backup"/>
    	    <!--<remote ip="192.168.8.39" name="tongbu"/>-->
    	    <!--<remote ip="192.168.8.40" name="tongbu"/>-->
    	</localpath>
    <!--配置rsync参数信息-->
    	<rsync>
    	    <commonParams params="-artuz"/>
    	    <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
    	    <userDefinedPort start="false" port="874"/><!-- port=874 -->
    	    <timeout start="false" time="100"/><!-- timeout=100 -->
    	    <ssh start="false"/>
    	</rsync>
    	<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
    	<crontab start="false" schedule="600"><!--600mins-->
    	    <crontabfilter start="false">
    		<exclude expression="*.php"></exclude>
    		<exclude expression="info/*"></exclude>
    	    </crontabfilter>
    	</crontab>
    	<plugin start="false" name="command"/>
        </sersync>
    
        <plugin name="command">
    	<param prefix="/bin/sh" suffix="" ignoreError="true"/>	<!--prefix /opt/tongbu/mmm.sh suffix-->
    	<filter start="false">
    	    <include expression="(.*).php"/>
    	    <include expression="(.*).sh"/>
    	</filter>
        </plugin>
    
        <plugin name="socket">
    	<localpath watch="/opt/tongbu">
    	    <deshost ip="192.168.138.20" port="8009"/>
    	</localpath>
        </plugin>
        <plugin name="refreshCDN">
    	<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
    	    <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
    	    <sendurl base="http://pic.xoyo.com/cms"/>
    	    <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
    	</localpath>
        </plugin>
    </head>

    启动软件

    [root@test1 bin]# ./sersync -h
    set the system param
    execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
    execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
    parse the command param
    _______________________________________________________
    参数-d:启用守护进程模式
    参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
    c参数-n: 指定开启守护线程的数量,默认为10个
    参数-o:指定配置文件,默认使用confxml.xml文件
    参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
    参数-m:单独启用其他模块,使用 -m socket 开启socket模块
    参数-m:单独启用其他模块,使用 -m http 开启http模块
    不加-m参数,则默认执行同步程序
    ________________________________________________________________
    [root@test1 bin]# ./sersync -dro /usr/local/sersync/conf/confxml.xml
    set the system param
    execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
    execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
    parse the command param
    option: -d 	run as a daemon
    option: -r 	rsync all the local files to the remote servers before the sersync work
    option: -o 	config xml name:  /usr/local/sersync/conf/confxml.xml
    daemon thread num: 10
    parse xml config file
    host ip : localhost	host port: 8008
    daemon start,sersync run behind the console 
    use rsync password-file :
    user is	rsync_backup
    passwordfile is 	/etc/rsync.oassword
    config xml parse success
    please set /etc/rsyncd.conf max connections=0 Manually
    sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) 
    Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
    please according your cpu ,use -n param to adjust the cpu rate
    ------------------------------------------
    rsync the directory recursivly to the remote servers once
    working please wait...
    execute command: cd /tmp && rsync -artuz -R --delete ./ rsync_backup@172.16.0.114::backup --password-file=/etc/rsync.oassword >/dev/null 2>&1 
    run the sersync: 
    watch path is: /tmp
    [root@test1 bin]# ps -ef | grep sersync
    root      40115      1  0 14:44 ?        00:00:00 ./sersync -dro /usr/local/sersync/conf/confxml.xml
    root      40149 102523  0 14:44 pts/1    00:00:00 grep --color=auto sersync
    

      

  • 相关阅读:
    sql被注入,用友不能建账
    项目总帐金额翻倍
    1)123104科目的余额出现翻倍情况,经调数据库,期初余额已调平,但余额表中的数仍是未调平前的错误数。2)一月结账时提示有一科目119101的总账与个人明细账不平....
    尚有已全部暂估报销的单据未进行处理,不能进行12月的期末处理
    用友U8尚有已全部暂估报销的单据未进行处理,不能进行12月的期末处理
    用sql替换T6工作流中的操作员
    解决win7科迈登录报错RASRDP MODULE已停止工作
    sql2005 64 位 连接 sql2000 32位
    jquery选择器
    深入理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法
  • 原文地址:https://www.cnblogs.com/yanshicheng/p/12801452.html
Copyright © 2011-2022 走看看