• Linux课程笔记 Rsync数据同步服务


    一 Rsync

    1. Rsync介绍

    Rsync具有可使本地主机不同分区或目录之间及本地和远程两台主机之间的数据快速同步镜像,远程备份等功能。

    在同步备份时,默认情况下,Rsync通过独特的“quick  check”算法,仅同步大小或者最后修改时间发生变化的文件或目录(也可根据权限,属主等变化同步,需要指定参数),甚至是只同步一个文件里有变化的内容部分,所以可实现快速的同步数据的功能。

    2. Rsync的特性

    • 支持拷贝特殊文件如链接,设备等
    • 可以排除指定文件或目录同步的功能,相当于打包命令tar
    • 可以做到保持原来文件或目录的权限、时间、软硬链接等所有属性均不变
    • 可实现增量同步,既只同步发生变化的数据
    • 可以使用rcp、rsh、ssh等方式来配合传输文件,也可以通过直接的socket连接
    • 支持匿名的或认证的进程模式传输,方便进行数据备份及镜像

    3. Rsync的工作方式

    Rsync大致使用三种主要的方法来传输数据,分别为

    (1) 本地数据传输

    (2) 通过rcp、ssh等传输通道

    (3) 以守护进程的方式传输数据

    Rsync本地传输的语法为:

    rsync  [OPTION]  SRC  DEST

    语法说明:

    1)  Rsync为同步的命令

    2)  [OPTION]为同步时的参数选项

    3)  SRC为源,即待拷贝的分区、文件或目录等

    4)  [DEST]为目的分区、文件或目录等

    特别提示:请注意以下命令的差别:

    1)  rsync  -avz  /opt/  /tmp

    2)  rsync  -avz  /opt   /tmp

    1)中/opt/意思是,仅把/opt/目录里的内容同步过来,opt目录本身并不同步;而后者2)中/opt表示把opt本身及其全部内容都同步到/tmp下,仅一个/之差,意义就大不同。

    通过远程shell进行数据传输(remote  shell  mode)

    通过远程shell(rcp、ssh等)传输可以分为两种情况,其语法分别为

    拉取:rsync  [OPTION]  [USER@]HOST:SRC  DEST

    推送:rsync  [OPTION]  SRC   [USER@]HOST:DEST

    拉取实例:

    rsync   -vzrtopg  --progress  -e  ‘ssh  -p  22’  oldboy@192.168.1.4:/opt  /tmp

    -vzrtopg相当于上文的-avz,表示同步文件和目录属性不变

    --progress显示同步的过程,可以用-P替换

    -e  ‘ssh  -p  22’表示通过ssh的通道传输数据,-p  22可以省略

    使用守护进程的方式数据传输:

    通过守护进程方式传输同样分为两种情况,每种情况又分为两种语法:

    拉取:

    1)rsync  [OPTION]  [USER@]HOST::SRC  DEST

    2)  rsync  [OPTION] rsync:// [USER@]HOST[:PORT]/SRC  [DEST]

    推送:

    1)   rsync  [OPTION] SRC  [USER@]HOST::DEST

    2)   rsync  [OPTION] SRC  rsync://[USER@]HOST[:PORT]/DEST

    值得注意的是,与远程shell方式的命令不同的是,第1种语法格式,均为双冒号连接。另外,这个SRC或DEST也不再是路径了,而是守护进程中配置的模块名称。

    4. Rsync命令同步参数选项

    常用参数选项说明:

    -v,--verbose详细模式输出,传输时的进度等信息

    -z,--compress传输时进行压缩以提高传输效率,--compress-level=NUM可按级别压缩

    -r,--recursive对子目录以递归模式,即目录下的所有目录都同样传输,注意是小写r

    -t,--times保持文件时间信息

    -o,--owner保持未见属主信息

    -p,--perms保持文件权限

    -g,--group保持文件属组信息

    -P,--progress显示同步的过程及传输时的进度等信息

    -a,--archive归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rtopgDl

    -e,--rsh=COMMAND使用的信道协议,指定代替rsh的shell程序

    --exclude=PATTERN指定排除不需要传输的文件模式

    -D,--devices保持设备文件信息

    -l,--links保留软链接

    5. 开始部署rsync服务--Rsync服务端操作过程:

    5.1  配置rsyncd.conf

    1)  配置rsyncd.conf

    2)  配置文件常用参数选项说明:

    rsyncd.conf参数

    参数说明

    uid = root

    rsync使用的用户,缺省uid为-2,通常为nobody

    gid = root

    rsync使用的组,缺省gid为-2,通常为nobody

    use chroot = no

    如果为true,daemon会在客户端传输文件前“chroot to the path”。这是rsync安全的一个配置,因为我们大多数都是在内网使用,所以不配也可以

    max  connections = 200

    设置最大连接数,默认为0,意味无限制,负值为关闭这个模块

    timeout = 400

    默认为0,意味着no  timeout,建议为300-600(5-10分钟)

    pid file =/var/run/rsyncd.pid

    rsync  daemon启动后将其进程PID写入此文件,如果这个文件进程不会覆盖该文件,而是会终止

    lock file = /var/run/rsyncd.lock

    指定lock文件用来支持“max connection”参数,使得总连接数不会超过限制,默认为/var/run/rsyncd.lock

    log  file = /var/run/rsyncd.log

    不设或者设置错误,rsync会使用syslog输出相关日志信息

    ignore  errors

    忽略I/O错误

    read  only

    指定客户端是否可以上传文件,默认对所有模块都为true

    list = false

    是否允许客户端可以查看可用模块列表,默认可以

    hosts  allow = 10.0.0.0/24

    指定可以联系的客户端主机名或ip地址或地址段,默认情况没有此参数,即都可以连接

    host deny = 0.0.0.0/32

    指定不可以联系的客户端主机名和ip地址,默认情况没有此参数,即都可以连接

    auth  users  = rsync_backup

    指定以空格或逗号分隔的用户可以哪些模块,用户不需要在本地系统中存在,默认所有用户无密码的访问(anonymous rsync)

    secrets  file = /etc/rsyncd.password

    指定用户名和密码存放的文件,格式:用户名:密码,密码不超过8位

    [oldboy]

    这里就是模块名称,需要用中括号括起来,起名没有特殊要求,但最好是有意义的名称,便于以后维护

    path = /oldboy/

    在这个模块中,daemon使用的文件系统或目录,目录的权限要注意和配置文件中的权限一直,否则会遇到读写问题

    #exclude = a b c/2

    排除的文件或目录,相对路径

    特别说明:如果配置中的path = /oldboy/提到的路径不存在,则需要创建,命令为:

    mkdir  -p /oldboy/

    chmod  -R  root.root  /oldboy  #授权权限可以根据配置文件中的属主来设定。

    5.2 配置用于rsync同步的账号、密码及账号文件权限

    #auth

    echo  “rsync_backup:oldboy”>/etc/rsyncd.password

    #注意:其中rsync_backup:oldboy中的rsync_backup为同步传输用到的虚拟账号,这个账号仅为rsync的账号,不需要是系统账号,。后面的oldboy为密码,不超过8位,账号和密码中间用冒号分割。

    chmod  600  /etc/rsyncd.password   #必须为600权限

    #check

    cat  /etc/rsyncd.password

    ll  /etc/rsync.password

    5.3 启动rsync服务

    以守护进程方式来启动rsync服务:

    rsync  --daemon  #daemon表示以守护进程的方式启动rsync服务

    --address     #绑定指定IP地址提供服务,适用于多网卡的服务器

    --config=FILE  #更改配置文件路径,而不是默认的/etc/rsyncd.conf

    --port=Port   更改其他端口提供服务,而不是缺省的873端口

    提示:以上几个选项为了解内容,生产场景使用的不多

    5.4 设置rsync服务开机自启动

    echo  “/usr/bin/rsync  --daemon”  >> /etc/rc.local

    也可以使用chkconfig  rsync  on命令(适用于设置其他服务为开机自启动服务)

    #从Windows系统拷贝到Linux系统的文件需要转换格式:

    [root@test2 ~]# dos2unix /etc/init.d/rsyncd

    dos2unix: converting file /etc/init.d/rsyncd to UNIX format ...

    [root@test2 ~]# cat /etc/init.d/rsyncd

    #!/bin/sh

    #author oldboy QQ 31333741

    #

    # rsync       Start/Stop Rsync service

    #

    # chkconfig: 35 13 91

    # description: This is Rsync service management shell script

    # processname: rsyncd

    # Source function library.

    . /etc/rc.d/init.d/functions

    start(){

            rsync --daemon

            if [ $? -eq 0 -a `ps -ef|grep -v grep|grep rsync|wc -l` -gt 0 ];then

              action "Starting Rsync:" /bin/true

              sleep 1

            else

              action "Starting Rsync:" /bin/false

              sleep 1

            fi

    }

    stop(){

            pkill rsync;sleep 1;pkill rsync

            #if [ $? -eq 0 -a `ps -ef|grep -v grep|grep rsync|wc -l` -lt 1 ];then

            if [ `ps -ef|grep -v grep|grep "rsync --daemon"|wc -l` -lt 1 ];then

              action "Stopping Rsync: `ps -ef|grep -v grep|grep rsync|wc -l` " /bin/true

              sleep 1

            else

              action "Stopping Rsync: `ps -ef|grep -v grep|grep "rsync --daemon"|wc -l` " /bin/false

              sleep 1

            fi

    }

    case "$1" in

       start)

          start;

         ;;

       stop)

         stop

         ;;

       restart|reload)

        $0 stop;

        $0 start;

         ;;

      *)

            echo $"Usage: $0 {start|stop|restart|reload}"

            ;;

    esac

    #/etc/init.d/rsyncd文件的权限为700,root用户

    [root@test2 ~]# chkconfig rsyncd on

    [root@test2 ~]# chkconfig --list rsyncd

    rsyncd          0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭

    这里之所以可以使用chkconfig设置为开机自启动,是因为老师写的文件比较规范了,要学习这里的格式。

    [root@test2 ~]# service rsyncd start

    Failed to parse config file: /etc/rsyncd.conf

    Starting Rsync:                                            [失败]

    [root@test2 ~]# /etc/init.d/rsyncd start

    Failed to parse config file: /etc/rsyncd.conf

    Starting Rsync:                                            [失败]

    这里设置开机自启动失败,是因为我没有配置/etc/rsyncd.conf这个配置文件。

    配置文件如下:

    [root@test2 ~]# cat /etc/rsyncd.conf

    #rsync_config_______________start

    #created by oldboy 15:01 2007-6-5

    #QQ 31333741 blog:http://oldboy.blog.51cto.com

    ##rsyncd.conf start##

    uid = root

    gid = root

    use chroot = no

    max connections = 200

    timeout = 300

    pid file = /var/run/rsyncd.pid

    lock file = /var/run/rsync.lock

    log file = /var/log/rsyncd.log

    [oldboy]

    path = /oldboy/

    ignore errors

    read only = false

    list = false

    hosts allow = 10.0.0.0/24

    hosts deny = 0.0.0.0/32

    auth users = rsync_backup

    secrets file = /etc/rsync.password

    [root@test2 ~]# service rsyncd start

    Starting Rsync:                                            [确定]

    [root@test2 ~]#

    [root@test2 ~]#

    [root@test2 ~]# /etc/init.d/rsyncd stop

    已终止

    重启rsync的组合命令

    pkill  rsync        #关闭rsync服务

    rsync  --daemon   #启动rsync服务

    ps  -ef  | grep  rsync   #检查rsync服务

    [root@test2 ~]# netstat -tupnl |grep 873

    tcp      0     0 0.0.0.0:873        0.0.0.0:*                LISTEN      4380/rsync         

    tcp      0     0 :::873            :::*                     LISTEN      4380/rsync         

    [root@test2 ~]# ps -ef | grep -v grep | grep rsync

    root      4380     1  0 16:56 ?        00:00:00 rsync --daemon

    6. 开始部署Rsync服务--Rsync客户端操作过程

    6.1 配置rsync账号及账号文件权限

    请注意与服务端的配置的区别

    echo  “oldboy” > /etc/rsyncd.password  #这里仅配置了密码,不需要账号。这是与服务端的区别。

    chmod  600  /etc/rsyncd.password     #必须为600权限

    到此rsync服务配置大功告成!

    7. 检查部署的Rsync服务:

    默认情况下,以下均在客户端rsync客户端执行操作:下面以[ip:192.168.1..4]为例说明:

    从客户端推送/etc目录到服务器rsync指定的目录下:

    推送方法结论:--delete参数的作用,它可以使本地的目录和rsync服务端指定的目录实现完全同步,即本地有啥远端就有啥,本地没有的,远端也必须删除。确保数据一致。推送时使用--delete命令有使远端rsync服务端的目录数据丢失的风险。

    二 Sersync应用指南

    2.1 项目简介

    Sersync项目利用inotify与rsync技术实现对服务器数据实时同步解决方案,其中inotify用于监控sersync所在服务器上文件系统的事件变化,rsync是目前广泛使用的本地及异地数据同步工具

    2.2 基本架构

     

     

    (1)       线程组是线程是等待线程队列的守护线程,设计的目的是能够同时处理多个inotify事件,从而提升服务器的并发同步能力

    (2)       服务线程的三个作用:

    a.  首先是处理同步失败的文件,将这些文件再次同步,对于再次同步失败的文件会生成rsync_fail_log.sh脚本,记录失败的事件

    b.  每隔10个小时执行脚本,同时清空脚本

    c.  第三个作用是crontab功能,可以每隔一定时间,将所有路径整体同步一次

    (3)       过滤队列的建立是为了过滤短时间内产生的重复inotify信息,例如在删除文件夹的时候,inotify就会同时产生删除文件夹里的文件与删除文件夹的时间,通过过滤队列,当删除文件夹时间产生的时候,会将之前加入队列的删除文件的时间全部过滤掉,这样只产生一条删除文件夹的事件。

    2.3  Sersync逻辑需求

    当前版本的sersync依赖于rsync进行数据同步,在同步主服务器(Master)上开启sersync,sersync负责监控配置路径中的文件系统事件变化,然后调用rsync命令把更新的文件同步到目标服务器(Slave),因此,需要在主服务器配置sersync,在同步目标服务器配置rsync server(注意,是rsync服务)

    2.4  配置同步服务器

    2.4.1  Slave上部署rsync服务

    (1) 升级rsync到3.0

    (2) 写入配置文件/etc/rsyncd.conf

    [root@test4 ~]# /bin/cp /etc/rsyncd.conf /etc/rsyncd.conf.qinbf_$(date +%F)

    [root@test4 ~]# echo >/etc/rsyncd.conf<<EOF

    > #Rsync server

    > #created by oldboy 15:01 2009-6-5

    > ##rsyncd.conf start##

    > uid = root

    > gid = root

    > use chroot = no

    > max connections = 2000

    > timeout = 600

    > pid file = /var/run/rsyncd.pid

    > lock file = /var/run/rsync.lock

    > log file = /var/log/rsyncd.log

    > ignore errors

    > read only = false

    > list = false

    > hosts allow = 10.0.0.0/24

    > hosts deny = 0.0.0.0/32

    > auth users = rsync_backup

    > secrets file = /etc/rsync.password

    > #####################################

    > [www]

    > comment = www by old0boy 14:18 2012-1-13

    > path = /data0/www/www/

    > #####################################

    > [bbs]

    > comment = bbs by old0boy 14:18 2012-1-13

    > path = /data0/www/bbs/

    > #####################################

    > [blog]

    > comment = blog by old0boy 14:18 2012-1-13

    > path = /data0/www/blog/

    > EOF

    (3)创建待同步目录

    mkdir -p /data0/www/www /data0/www/bbs /data0/www/blog

    [root@test3 ~]# tree /data0/www

    /data0/www

    |-- bbs

    |-- blog

    `-- www

    3 directories, 0 files

    [root@test3 ~]# tree /data0

    /data0

    `-- www

        |-- bbs

        |-- blog

        `-- www

    4 directories, 0 files

    (4)       配置相关权限认证

    [root@test3 ~]# echo "rsync_backup:oldboy123" >/etc/rsync.password

    [root@test3 ~]# chmod 600 /etc/rsync.password

    [root@test3 ~]# cat /etc/rsync.password   #for check

    rsync_backup:oldboy123

    [root@test3 ~]# ll /etc/rsync.password     #for check

    -rw------- 1 root root 23 09-03 12:15 /etc/rsync.password

    (5)       开启守护进程

    [root@test3 ~]# rsync --daemon    #开启进程

    [root@test3 ~]# ps -ef |grep rsync   #查看rsync进程的情况

    root     10005     1  0 12:18 ?        00:00:00 rsync --daemon

    root     10007  9887  0 12:18 pts/0    00:00:00 grep rsync

    [root@test3 ~]# lsof -i :873        #查看端口对应的进程

    COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

    rsync   10005 root    3u  IPv6  20111      0t0  TCP *:rsync (LISTEN)

    rsync   10005 root    5u  IPv4  20112      0t0  TCP *:rsync (LISTEN)

    #pkill rsync 关闭rsync服务

    echo  “/usr/local/bin/rsync --daemon”>>/etc/rc.local

    2.4.2  在Master上配置rsync客户端

    (1) Master上配置rsync权限

    [root@test2 ~]# echo "oldboy123" >/etc/rsync.password

    [root@test2 ~]# chmod 600 /etc/rsync.password

    [root@test2 ~]# cat /etc/rsync.password

    oldboy123

    [root@test2 ~]# ll /etc/rsync.password

    -rw------- 1 root root 10 10-02 11:46 /etc/rsync.password

     (2) Master上手工测试rsync同步情况

    #分别创建待同步数据

    [root@test2 ~]# mkdir -p /data0/www/www /data0/www/bbs /data0/www/blog

    [root@test2 ~]# cd /data0/www/

     root@test2 www]# pwd

    /data0/www

    [root@test2 www]# ls

    bbs  blog  www

    [root@test2 www]# touch www/www.log

    [root@test2 www]# touch bbs/bbs.loog

    [root@test2 www]# touch blog/blog.log

    [root@test2 ~]# tree /data0

    /data0

    `-- www

        |-- bbs

        |   `-- bbs.loog

        |-- blog

        |   `-- blog.log

        `-- www

            `-- www.log

    4 directories, 3 files

    #关闭防火墙,执行同步

    [root@test2 ~]# rsync -avzP /data0/www/www/ rsync_backup@192.168.1.4::www/ --password-file=/etc/rsync.password

    sending incremental file list

    ./

    www.log

               0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/2)

    sent 77 bytes  received 30 bytes  71.33 bytes/sec

    total size is 0  speedup is 0.00

    #/data0/www/www/中最后的这一个“/”很关键,直接决定你是同步整个文件夹 ,还是只是同步文件夹里面的文件

    ……

    #然后检查同步的结果

    [root@test3 www]# tree /data0

    /data0

    `-- www

        |-- bbs

        |   `-- bbs.loog

        |-- blog

        |   `-- blog.log

        `-- www

            `-- www.log

    4 directories, 3 files

    2.5 在Master上部署sersync

    (1) 下载sersync

    [root@test2 ~]# mkdir tools

    [root@test2 ~]# cd tools/

    [root@test2tools]#wget  https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz(由于防火墙的问题在虚拟机上无法直接下载)

    (2) 安装sersync

    [root@test2 tools]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/

    GNU-Linux-x86/

    GNU-Linux-x86/sersync2

    GNU-Linux-x86/confxml.xml

    [root@test2 tools]# cd /usr/local/

    [root@test2 local]# mv GNU-Linux-x86 sersync

    (3) 规范sersync目录结构

    [root@test2 sersync]# mkdir bin conf logs

    [root@test2 sersync]# mv  sersync2 bin/sersync 

    [root@test2 sersync]# mv confxml.xml conf/

    [root@test2 sersync]# tree .

    .

    |-- bin

    |   `-- sersync

    |-- conf

    |   `-- confxml.xml

    `-- logs

    3 directories, 2 files

    (4) 配置sersync

    [root@test2 sersync]# /bin/cp conf/confxml.xml conf/confxml.xml_qinbf_$(date +%F)

    然后修改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="/data0/www/www">

     25             <remote ip="192.168.1.4" name="www"/>

     26             <remote ip="192.168.1.5" name="www"/>

     27         </localpath>

     28         <!--################################-->

     29         <localpath watch="/data0/www/bbs">

     30         <remote ip="192.168.1.4" name="bbs"/>

     31         <remote ip="192.168.1.5" name="bbs"/>

     32         </localpath>

     33         <!--################################-->

     34         <localpath watch="/data0/www/blog">

     35         <remote ip="192.168.1.4" name="blog"/>

     36         <remote ip="192.168.1.5" name="blog"/>

     37         </localpath>

     38         <!--################################-->

    提示:<localpath watch="/data0/www/www">中watch部分是本地待同步的目录,name是远端服务器的同步模块名

    修改29-35行

        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>

    为:

    39         <rsync>

     40           <commonParams params="-aruz"/>

     41           <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>

     42           <userDefinedPort start="false" port="874"/><!-- port=874 -->

     43           <timeout start="true" time="100"/><!-- timeout=100 -->

     44           <ssh start="false"/>

     45         </rsync>

    提示:这几行的配置其实是在配置同步的命令参数而已

    修改36-37行

     36        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

    为:

     46  <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

    提示:此步修改同步失败之后,日志文件路径

    2.6 开启sersync守护进程同步数据

    (1) 配置环境变量

    [root@test2 sersync]# echo 'export PATH=$PATH:/usr/local/sersync/bin' >>/etc/profile

    [root@test2 sersync]# tail -1 /etc/profile

    export PATH=$PATH:/usr/local/sersync/bin

    [root@test2 sersync]# source /etc/profile

    (2)启动命令

    [root@test2 sersync]# sersync -d -r -o /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

    #让Master和slave的数据在初始化的时候相同

    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.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: 32 = 12(Thread pool nums) + 20(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 /data0/www/www && rsync -aruz -R --delete ./  --timeout=100 rsync_backup@192.168.1.4::www --password-file=/etc/rsync.password >/dev/null 2>&1

    [root@test2 sersync]# run the sersync:

    watch path is: /data0/www/www

    #现在的问题是,仅能同步第一个模块的目录

    于是,配置多实例,每个配置文件仅同步一个目录,仅记录该目录的失败日志

    [root@test2 conf]# cp confxml.xml confxml.xml.new

    [root@test2 conf]# cp confxml.xml www_confxml.xml

    [root@test2 conf]# cp confxml.xml bbs_confxml.xml

    [root@test2 conf]# cp confxml.xml blog_confxml.xml

    例如bbs的配置:

    23     <sersync>

     24         <!--################################-->

     25         <localpath watch="/data0/www/bbs">

     26         <remote ip="192.168.1.4" name="bbs"/>

     27         <remote ip="192.168.1.5" name="bbs"/>

     28         </localpath>

     29         <!--################################-->

     30         <rsync>

     31             <commonParams params="-aruz"/>

     32           <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>

     33             <userDefinedPort start="false" port="874"/><!-- port=874 -->

     34             <timeout start="true" time="100"/><!-- timeout=100 -->

     35             <ssh start="false"/>

     36         </rsync>

     37   <failLog path="/usr/local/sersync/logs/bbs_rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

    开启多实例:

    sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml

    sersync -r -d -o /usr/local/sersync/conf/bbs_confxml.xml

    sersync -r -d -o /usr/local/sersync/conf/blog_confxml.xml

    设置开机启动:

    cat >>/etc/rc.local <<EOF

    sersync  -d -o /usr/local/sersync/conf/www_confxml.xml

    sersync  -d -o /usr/local/sersync/conf/bbs_confxml.xml

    sersync  -d -o /usr/local/sersync/conf/blog_confxml.xml

    EOF

    #在开机启动的时候一般不需要加上-r参数,因为关机之前大部分的数据是已经同步的,如果再同步,就浪费时间。

    其实本例可以只监控上一次的目录,这样可以一次监控三个子目录,这样做是为了方便学习

    写入10000个文件批量实时同步测试

    [root@test2 www]# for i in `seq 10000`;do echo "qinbf">www/$i.txt;done

    [root@test2 www]# ps -ef |grep rsync 

    root      4380     1  0 05:05 ?        00:00:00 rsync --daemon

    root      6246     1  0 10:06 ?        00:00:00 sersync -d -r -o /usr/local/sersync/conf/confxml.xml

    root      6601     1  0 10:39 ?        00:00:00 sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml

    root      6621     1  0 10:39 ?        00:00:00 sersync -r -d -o /usr/local/sersync/conf/bbs_confxml.xml

    root      6641     1  0 10:39 ?        00:00:00 sersync -r -d -o /usr/local/sersync/conf/blog_confxml.xml

    root     12592  6601  0 10:54 ?        00:00:00 sh -c cd /data0/www/www && rsync -aruz -R  --timeout=100 "./464.txt" rsync_backup@192.168.1.4::www --password-file=/etc/rsync.password >/dev/null 2>&1

    root     12593 12592  0 10:54 ?        00:00:00 rsync -aruz -R --timeout=100 ./464.txt rsync_backup@192.168.1.4::www --password-file=/etc/rsync.password

    root     12594  6601  0 10:54 ?        00:00:00 sh -c cd /data0/www/www && rsync -aruz -R  --timeout=100 "./464.txt" rsync_backup@192.168.1.5::www --password-file=/etc/rsync.password >/dev/null 2>&1

    root     12599  6246  0 10:54 ?        00:00:00 sh -c cd /data0/www/www && rsync -aruz -R  --timeout=100 "./461.txt" rsync_backup@192.168.1.5::www --password-file=/etc/rsync.password >/dev/null 2>&1

    root     12608  6246  0 10:54 ?        00:00:00 sh -c cd /data0/www/www && rsync -aruz -R  --timeout=100 "./463.txt" rsync_backup@192.168.1.4::www --password-file=/etc/rsync.password >/dev/null 2>&1

    提示:当我们已经写入10000个文件,发现同步的线程,依然在同步,甚至才同步400多个文件。可见,实时同步也是相对的,如果没有海量文件的写入,同步还是比较快的。

    2.7  Sersync参数说明:

    Sersync参数

    说明

    -r

    作用是在开启实时监控之前对主服务器目录与远程目标机器目录进行一次整体同步。如果需要将sersync运行前,主服务器目录下已经存在的所有文件或目录全部同步到远端,则要以-r参数运行sersync,将本地与远程整体同步一次

    特别说明:如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步

    -o

    不指定-o参数时,sersync会使用sersync可执行文件目录下的默认配置文件confxml.xml,如果要使用其他的配置文件,则需要使用-o指定

    -n

    -n参数为指定默认的线程池的线程总数。

    例如./sersync -n 5则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n调高默认的线程总数,提升同步效率

    -d

    -d参数为后台启动服务,在通常情况下,使用-r参数对本地到远程整体同步一遍之后,在后台运行此参数启动守护进程实时同步,在第一次整体同步时,-d和-r参数经常会联合使用

    -m

    -m参数为不进行同步,只运行插件

    ./sersync -m pluginName

    例如./sersync  -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件

    --help

    查看帮助的参数,目前还未完善

    2.8 sersync配置文件说明

    #xml配置文件注释不是”#”,而是<!--中间是注释内容-->

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <head version="2.5">

    <host hostip="localhost" port="8008"></host>

    #hostip与port是针对插件的保留字段,对于同步功能没有任何作用,保留默认即可

    <debug start="false"/>

    #Debug开启开关,设置为true,开启debug模式,会在sersync正在运行的控制台,打印inotify时间与rsync同步命令

    <fileSystem xfs="false"/>

    #对于xfs文件系统的用户,需要将这个选项开启,才能使sersync正常工作(ext3、ext4的用户可以不用开启)

        <filter start="false">

            <exclude expression="(.*).svn"></exclude>

            <exclude expression="(.*).gz"></exclude>

            <exclude expression="^info/*"></exclude>

            <exclude expression="^static/*"></exclude>

    </filter>

    #对于sersync监控文件,会默认过滤系统的临时文件(以”.”开头,以”~”结尾),除了这些文件外,可以自定义其他需要过滤的文件。将start设置为true后开启过滤功能,在exclude标签中,填写正则表达式,默认给出两个例子分别是过滤以”.gz”结尾的文件与过滤监控目录下的info路径(监控路径/info/*),可以根据需要添加,但开启的时候,自己测试一下,正则表达式如果出现错误,控制台会有提示。

        <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>

    #对于大多数应用,可以尝试把createFile设置为false来提高性能,减少rsync通讯。因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的事件close_write,同样可实现文件完整同步。

    #要使得createFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控。

    #默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete参数设置为false,则不会对删除事件进行监控。

        <sersync>

            <localpath watch="/opt/tongbu">

                <remote ip="127.0.0.1" name="tongbu1"/>

                <!--<remote ip="192.168.8.39" name="tongbu"/>-->

                <!--<remote ip="192.168.8.40" name="tongbu"/>-->

            </localpath>

    #/opt/tongbu目录为sersync主服务器本地待同步的目录,ip=’192.168.18.39,为从服务器的IP地址,如果有多个从服务器,依次列出来即可。name=”tongbu”,这里的tongbu为rsyncd.conf中的模块名字,即中括号的名称

            <rsync>

                <commonParams params="-artuz"/>

                <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>

                <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-->

    #失败日志配置,默认每60分重新同步

            <crontab start="false" schedule="600"><!--600mins-->

                <crontabfilter start="false">

                    <exclude expression="*.php"></exclude>

                    <exclude expression="info/*"></exclude>

                </crontabfilter>

            </crontab>

    #crontab整体同步,每600分同步一次

            <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>

    #plugin标签设置为true的时候,在同步文件或路径到远程服务器之后,会调用插件。通过name参数指定需要执行的插件。目前支持的又command  refreshCDN socket  http四种插件。其中http插件目前由于兼容性原因已经去除,以后会重新加入。

    #模块可以单独使用(发生文件改变不同步只调用插件),只需要在命令行下使用-m参数即可。

    三 inotify+rsync远程实时同步

    [root@test2 ~]# ls -l /proc/sys/fs/inotify/*

    #如果该目录下有下面三个文件,就表示支持inotify

    -rw-r--r-- 1 root root 0 10-07 23:29 /proc/sys/fs/inotify/max_queued_events

    -rw-r--r-- 1 root root 0 10-07 23:29 /proc/sys/fs/inotify/max_user_instances

    -rw-r--r-- 1 root root 0 10-07 23:29 /proc/sys/fs/inotify/max_user_watches

    上传inotify源软件包,解压—编译—-安装

    执行脚本如下:

    [root@test2 inotify-tools-3.14]# cat inotify.sh

    #!/bin/bash

    #para

    host01=10.0.0.191

    src=/data0/www/www/

    dst=www

    user=rsync_backup

    rsync_passfile=/etc/rsync.password

    inotify_home=/usr/local/inotify-tools-3.14/

    #judge

    if [ ! -e "$src" ]

    || [ ! -e "${rsync_passfile}" ]

    || [ ! -e "${inotify_home}/bin/inotifywait" ]

    || [ ! -e "/usr/bin/rsync" ];

    then

      echo "Check File and Folder"

      exit 9

    fi

    ${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src

    | while read file

            do

          #  rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host01::$dst >/dev/null 2>&1

             cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1

            done

    exit 0

    #./ inotify.sh &  在后台运行

    提示:sersync其实是inotify和rsync的封装

    对比sersync和inotify+rsync,sersync支持过滤,但是经过压力测试sersync的实时同步测试效果没有inotify+rsync的效果好。

  • 相关阅读:
    input失效后,怎么改变它默认就有的灰色
    弹性盒布局-宽度自动分配-图片自适应
    时钟效果收集
    1秒加1
    tab切换☆☆☆☆☆
    音乐播放的动画效果
    css3-文字旋转
    红黑树
    Ruby2.0后版本的debug工具: byebug
    [转]DSL-让你的 Ruby 代码更优秀
  • 原文地址:https://www.cnblogs.com/fengze/p/6756314.html
Copyright © 2020-2023  润新知