zoukankan      html  css  js  c++  java
  • sersync+rsync实现服务器文件实时同步

                     sersync+rsync实现服务器文件实时同步

    一、为什么要用rsync+sersync架构?

    1、sersync是基于inotify开发的,类似于inotify-tools的工具
    2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录

    二、rsync+inotify-tools与rsync+sersync架构的区别?

    1、rsync+inotify-tools
    a、inotify只能记录下被监听的目录发生了变化(增,删,改)并没有把具体是哪个文件或者哪个目录发生了变化记录下来;
    b、rsync在同步的时候,并不知道具体是哪个文件或目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此效率很低
     
    2、rsync+sersync
    a、sersync可以记录被监听目录中发生变化的(增,删,改)具体某个文件或目录的名字;
    b、rsync在同步时,只同步发生变化的文件或目录(每次发生变化的数据相对整个同步目录数据来说很小,rsync在遍历查找对比文件时,速度很快),因此效率很高。
     
    总结:
    当同步的目录数据量不大时,建议使用rsync+inotify
    当同步的目录数据量很大时(几百G甚至1T以上)文件很多时,建议使用rsync+sersync

    架构:

    1.部署rsync服务(rsync-server服务器上配置)

    yum install rsync -y #安装rsync,如果嫌yum版本过低也可以源码安装
    2.vim /etc/rsyncd.conf #默认rsync没有配置文件,创建一个,文件中#和汉字仅为注释,使用中请将所有注释清除
    #Rsync server
    uid = root
    gid = root
    use chroot = no                         # 安全相关
    max connections = 2000                  # 并发连接数
    timeout = 600                           # 超时时间(秒)
    pid file =/var/run/rsyncd.pid           # 指定rsync的pid目录
    lock file =/var/run/rsync.lock          # 指定rsync的锁文件【重要】
    log file = /var/log/rsyncd.log          # 指定rsync的日志目录
    ignore errors                             #忽略一些I/O错误
    read only = false                       #设置rsync服务端文件为读写权限
    list = false                            #不显示rsync服务端资源列表
    hosts allow = 172.16.0.0/16               #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    hosts deny = 0.0.0.0/32                 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
    auth users = rsync_backup               #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
    secrets file =/etc/rsync.password       #用户认证配置文件,里面保存用户名称和密码
    #################################################
    [www]                                   # 模块 
    comment = www 
    path = /data/www/
    #################################################
    [bbs]
    comment = bbs
    path = /data/bbs/
    #################################################
    [blog]
    comment = blog
    path = /data/blog/
    #rsync_config____________end
    
    :wq!  #保存,退出
    3、创建用户认证文件
    echo  "rsync_backup:123456">/etc/rsync.password  #配置文件,添加以下内容
    4、设置文件权限
    chmod 600 /etc/rsync.password  
    5.启动守护进程,并写入开机自启动
    rsync --daemon  #可以使用--config= 指定非标准路径下的配置文件
    vim /etc/rc.local
    # rsync server progress
    /usr/bin/rsync --daemon
    6.创建相关待同步的目录

    mkdir -p /data/{www,bbs,blog}

    #rsync-client客户端配置

    1.安装rsync,方法同上
    2.创建rsync配置文件,客户端创建即可,无需内容
    touch /etc/rsyncd.conf
    3.配置rsync客户端相关权限认证:
     echo "123456">/etc/rsync.password
     chmod 600 /etc/rsync.password 
    4.创建待同步数据,在客户端创建一些数据
    mkdir -p /data/{www,bbs,blog}
    touch /data/www/www.log /data/bbs/bbs.log  /data/blog/blog.log
    5.测试rsync是否同步
    [root@rsync-client www]# touch {1..3}.txt
    [root@rsync-client www]# rsync  -avzP /data/www/ rsync_backup@172.16.150.131::www/   --password-file=/etc/rsync.password   #/data/www/表示本地需要同步的数据目录  rsync_backup@172.16.150.131::www表示服务端的指定名称的模块下  本条命令执行的操作为:将第一个路径参数下的文件同步到第二个路径参数下  即:推模式 调换路径则为:拉模式
    sending incremental file list
    ./
    1.txt              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)
    2.txt              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=1/4)
    3.txt
                  0 100%    0.00kB/s    0:00:00 (xfr#3, to-chk=0/4)
    sent 207 bytes  received 84 bytes  582.00 bytes/sec
    total size is 0  speedup is 0.00
    
    #rsync  -avzP /data/www rsync://rsync_backup@172.16.150.131/data/www   --password-file=/etc/rsync.password 第二种方法,直接指定路径
    #rsync  -avzP /data/www rsync://rsync_backup@172.16.150.131/data/www/   --password-file=/etc/rsync.password #test为服务器上的目录
    参数: --delete 无差异同步
              --bwlimit=KB/S  限速
               --exclude=PATTERN       exclude files matching PATTERN
               --exclude-from=FILE     read exclude patterns from FILE
               --include=PATTERN       don’t exclude files matching PATTERN
               --include-from=FILE     read include patterns from FILE
    #此步骤必须成功才能进行下一步
    6.开始部署sersync服务(rsync-server服务器上)
    tar fxzsersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/  #百度网盘链接: https://pan.baidu.com/s/11yL6HtZsblkZR8vSSIvzrw 提取码: tdam 
    cd /usr/local/
    mv GNU-Linux-x86 sersync
    7.配置sersync
    cp sersync/confxml.xml  sersync/confxml.xml-bak
    vim  sersync/confxml.xml
    修改24--28行
     24         <localpath watch="/opt/tongbu">
     25             <remote ip="127.0.0.1" name="tongbu1"/>
     26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
     27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
     28         </localpath>
    修改后的内容为:
    24         <localpath watch="/data/www">
    25             <remote ip="10.1.20.109" name="www"/>
    26         </localpath>
    
    修改29--35行,认证部分(rsync密码认证)
    29         <rsync>
    30             <commonParams params="-artuz"/>
    31             <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
    32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
    33             <timeout start="false" time="100"/><!-- timeout=100 -->
    34             <ssh start="false"/>
    35         </rsync>
    
    修改后的内容如下:
    27         <rsync>
    28             <commonParams params="-artuz"/>
    29             <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
    30             <userDefinedPort start="false" port="874"/><!-- port=874 -->
    31             <timeout start="true" time="100"/><!-- timeout=100 -->
    32             <ssh start="false"/>
    33         </rsync>
    8.开启sersync守护进程同步数据

    /usr/local/sersync/sersync2  -d -r -o /usr/local/sersync/confxml.xml 
    配置sersync环境变量
    echo"PATH=$PATH:/usr/local/sersync/">>/etc/profile
    source /etc/profile
    启动命令后返回结果如下为正常:
    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:  ./confxml.xml
    daemon thread num: 10
    parse xml config file
    XML Parsing error inside file './confxml.xml'.
    Error: File not found
    At line 0, column 0.

    同步测试

    [root@panwenbin1-sa GNU-Linux-x86]# ./sersync2 -d -r -o ./confxml-www.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:  ./confxml-www.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.password
    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 /data/www && rsync -artuz -R --delete ./  --timeout=100 rsync_backup@10.1.20.109::www --password-file=/etc/rsync.password >/dev/null 2>&1 
    run the sersync: 
    watch path is: /data/www
    9.多实例情况

       1、配置多个confxml.xml文件(比如:www、bbs、blog....等等)
        confxml-bbs.xml  confxml-blog.xml  confxml-www.xml(按照单个实例配置即可)
        2、根据不同的需求同步对应的实例文件
        rsync -avzP /data/www/ rsync_backup@10.1.20.109::www/   --password-file=/etc/rsync.password
        rsync -avzP /data/bbs/ rsync_backup@10.1.20.109::bbs/   --password-file=/etc/rsync.password
        rsync -avzP /data/test/ rsync_backup@10.1.20.109::blog/   --password-file=/etc/rsync.passwor
    分别启动即可
    rsync缺点:
    1.大量小文件同步时,比对时间长,有时候rsync进程会停止
    2.同步大文件,10G这样的大文件有时也会有问题,会中断。未完整同步之前是隐藏文件,可通过参数续传

  • 相关阅读:
    2019.8.15刷题统计
    2019.8.12刷题统计
    2019.8.11刷题统计
    2019.8.10刷题统计
    2019.8.9刷题统计
    2019.8.8刷题统计
    2019.8.7刷题统计
    2019.8.6刷题统计
    xuezhan.org 6.28
    xuezhan.org 6.27
  • 原文地址:https://www.cnblogs.com/panwenbin-logs/p/7742288.html
Copyright © 2011-2022 走看看