美文网首页Linux我爱编程
Debian9配置Sersync+rsync,自动同步

Debian9配置Sersync+rsync,自动同步

作者: 吾星喵 | 来源:发表于2018-04-14 15:29 被阅读169次

    Debian9配置Sersync+rsync

    目标:将同步服务端的数据实时同步到客户端

    同步客户端配置rsync

    安装rsync,创建同步目录

    root@debian9_C:/home/user# apt-get install rsync
    root@debian9_C:/home/user# cp /usr/share/doc/rsync/examples/rsyncd.conf /etc/
    root@debian9_C:/home/user# mkdir /home/user/images
    root@debian9_C:/home/user# vim /etc/rsyncd.conf
    

    修改配置文件

    # sample rsyncd.conf configuration file
    
    # GLOBAL OPTIONS
    
    motd file=/etc/motd
    log file=/var/log/rsyncd
    # for pid file, do not use /var/run/rsync.pid if
    # you are going to run rsync out of the init.d script.
    # The init.d script does its own pid file handling,
    # so omit the "pid file" line completely in that case.
    pid file=/var/run/rsyncd.pid
    #syslog facility=daemon
    #socket options=
    
    # MODULE OPTIONS
    
    [images]
    
        comment = Images Sync
        path = /home/user/images
        use chroot = no
        max connections=2000
        lock file = /var/lock/rsyncd
    # the default for read only is yes...
        read only = no
        list = yes
        uid = root
        gid = root
    #   exclude =
    #   exclude from =
    #   include =
    #   include from =
        auth users = user
        secrets file = /etc/rsyncd.secrets
        strict modes = yes
        hosts allow = 192.168.66.231/24
    #   hosts deny =
        ignore errors = yes
        ignore nonreadable = yes
        transfer logging = no
    #   log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes.
        timeout = 600
        refuse options = checksum dry-run
        dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
    
    

    参数解释

    • [images] # 模块名称
    • comment = Images Sync # 目录的名称
    • path = /home/user/images # 模块路径
    • uid = root # rsync对后面模块中的path路径拥有什么权限
    • gid = root # rsync对后面模块中的path路径拥有什么权限
    • use chroot = no # 安全操作
    • max connections = 2000 # 定义连接数2000
    • timeout = 600 # 600秒超时
    • read only = false # false才能上传文件,true不能上传文件
    • list = true # 文件列表显示
    • auth users = user # 虚拟用户,同步时需要用这个用户
    • secrets file = /etc/rsyncd.secrets # 密码文件,格式在后面

    如果有多台目标服务器,则每一台都需要进行类似的rsync服务端配置,上面的uid和gid需要换成服务器的相应的同步用户。相同的配置,如motd file=/etc/motdlog file=/var/log/rsyncdpid file=/var/run/rsyncd.pid或其他,可先放在最前面作为全局设置,各个[xxx]模块中可使用不同的设置。

    注意,Rsync服务账户(这里用root)要有对被同步目录(/home/user/images)的写入和更新权限。

    例如:

    [images]
        comment = Images Sync
        path = /home/user/images
        # ......各自定义不同的参数
    
    [web]
        comment = Web Sync
        path = /home/user/web
        # ......
    

    相关认证和权限项配置

    root@debian9_C:/home/user# vim /etc/rsyncd.secrets
    root@debian9_C:/home/user# chmod 600 /etc/rsyncd.secrets
    root@debian9_C:/home/user# ls -l /etc/rsyncd.secrets
    -rw------- 1 root root 9 Dec 23 23:27 /etc/rsyncd.secrets
    root@debian9_C:/home/user# cat /etc/rsyncd.secrets
    user:password
    

    创建密码文件,采用这种方式不能使用系统用户对客户端进行认证,所以需要创建一个密码文件,其格式为“username:password”,用户名可以和密码可以随便定义,最好不要和系统帐户一致,多个用户名:密码需换行,同时要把创建的密码文件权限设置为600。

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

    root@debian9_C:/home/user# rsync --daemon
    root@debian9_C:/home/user# lsof -i tcp:873
    COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    rsync   1372 root    4u  IPv4  16477      0t0  TCP *:rsync (LISTEN)
    rsync   1372 root    5u  IPv6  16478      0t0  TCP *:rsync (LISTEN)
    
    root@debian9_C:/home/user# kill 1372
    root@debian9_C:/home/user# service rsync status
    ● rsync.service - fast remote file copy program daemon
       Loaded: loaded (/lib/systemd/system/rsync.service; enabled; vendor preset: enabled)
       Active: inactive (dead)
    
    root@debian9_C:/home/user# service rsync start
    root@debian9_C:/home/user# lsof -i tcp:873
    COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    rsync   1400 root    4u  IPv4  17015      0t0  TCP *:rsync (LISTEN)
    rsync   1400 root    5u  IPv6  17016      0t0  TCP *:rsync (LISTEN)
    root@debian9_C:/home/user# service rsync status
    ● rsync.service - fast remote file copy program daemon
       Loaded: loaded (/lib/systemd/system/rsync.service; enabled; vendor preset: enabled)
       Active: active (running) since Sat 2017-12-23 23:37:46 EST; 6s ago
     Main PID: 1400 (rsync)
        Tasks: 1 (limit: 19660)
       CGroup: /system.slice/rsync.service
               └─1400 /usr/bin/rsync --daemon --no-detach
    
    Dec 23 23:37:46 debian9_C systemd[1]: Started fast remote file copy program daemon.
    Dec 23 23:37:46 debian9_C rsyncd[1400]: rsyncd version 3.1.2 starting, listening on port 873
    
    

    或者通过进程名称杀掉

    pkill rsync
    rsync --daemon
    

    通过rsync --daemon启动后,下次重启服务器,该服务也会自动运行

    在同步服务端配置rsync

    rsync命令

    rsync命令是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。rsync使用所谓的“rsync算法”来使本地和远程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。

    语法

    rsync [OPTION]... SRC DEST
    rsync [OPTION]... SRC [USER@]host:DEST
    rsync [OPTION]... [USER@]HOST:SRC DEST
    rsync [OPTION]... [USER@]HOST::SRC DEST
    rsync [OPTION]... SRC [USER@]HOST::DEST
    rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
    

    工作模式

    1. 拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。如:rsync -a /data /backup
    2. 使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。如:rsync -avz *.c foo:src
    3. 使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式。如:rsync -avz foo:src/bar /data
    4. 从远程rsync服务器中拷贝文件到本地机。当SRC路径信息包含"::"分隔符时启动该模式。如:rsync -av root@192.168.78.192::www /databack
    5. 从本地机器拷贝文件到远程rsync服务器中。当DST路径信息包含"::"分隔符时启动该模式。如:rsync -av /databack root@192.168.78.192::www
    6. 列远程机的文件列表。这类似于rsync传输,不过只要在命令中省略掉本地机信息即可。如:rsync -v rsync://192.168.78.192/www

    选项

    -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指定模式匹配的文件。
    --version 打印版本信息。
    --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@debian9_S:/home/user# rsync  --list-only  user@192.168.66.232::images
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    
    Password:password
    drwxr-xr-x          4,096 2017/12/23 23:29:57 .
    

    使用密码文件进行同步

    root@debian9_S:/home/user# vim /etc/rsyncd.secrets
    root@debian9_S:/home/user# cat /etc/rsyncd.secrets
    password
    root@debian9_S:/home/user# chmod 600 /etc/rsyncd.secrets
    root@debian9_S:/home/user# rsync  --list-only  user@192.168.66.232::images --password-file=/etc/rsyncd.secrets
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    
    drwxr-xr-x          4,096 2017/12/23 23:29:57 .
    
    

    创建测试文件,执行同步

    参数-artuz

    root@debian9_S:/home/user# tree images/
    images/
    ├── testdir
    │   └── 123
    └── test.txt
    
    1 directory, 2 files
    
    root@debian9_S:/home/user# rsync -artuz --delete  /home/user/images/  user@192.168.66.232::images --password-file=/etc/rsyncd.secrets
    
    The programs included with the Debian GNU/Linux system are free software;
    the exact distribution terms for each program are described in the
    individual files in /usr/share/doc/*/copyright.
    
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
    permitted by applicable law.
    
    sending incremental file list
    ./
    test.txt
                  0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=2/4)
    testdir/
    testdir/123
                  0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=0/4)
    
    sent 230 bytes  received 77 bytes  614.00 bytes/sec
    total size is 0  speedup is 0.00
    

    客户端查看

    root@debian9_C:/home/user# tree images/
    images/
    ├── testdir
    │   └── 123
    └── test.txt
    
    1 directory, 2 files
    

    参数-vrpogt

    root@SambaMain96-99:/home/user/images# cd /home/user/images && rsync -vrpogt -R --delete ./  --timeout=100 user@192.168.96.21::images --password-file=/etc/rsyncd.secrets
    

    后台服务方式(可无)

    启动rsync服务,编辑/etc/xinetd.d/rsync文件,将其中的disable=yes改为disable=no,并重启xinetd服务

    root@debian9_S:/home/user# apt-get install xinetd
    root@debian9_S:/home/user# vim /etc/xinetd.d/rsync
    root@debian9_S:/home/user# cat /etc/xinetd.d/rsync
    #default: off
    ## description: The rsync server is a good addition to an ftp server, as it \
    ## allows crc checksumming etc.
    service rsync {
        disable = no
        socket_type = stream
        wait = no
        user = root
        server = /usr/bin/rsync
        server_args = --daemon
        log_on_failure += USERID
    }
    root@debian9_S:/home/user# /etc/init.d/xinetd restart
    [ ok ] Restarting xinetd (via systemctl): xinetd.service.
    

    在同步服务端配置sersync

    下载sersync

    root@debian9_S:/home/user# mkdir sersync
    root@debian9_S:/home/user# cd sersync/
    root@debian9_S:/home/user/sersync# wget https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
    --2017-12-24 00:44:37--  https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
    Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.196.133
    Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.196.133|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 727290 (710K) [application/octet-stream]
    Saving to: ‘sersync2.5.4_64bit_binary_stable_final.tar.gz’
    
    sersync2.5.4_64bit_ 100%[===================>] 710.24K   622KB/s    in 1.1s
    
    2017-12-24 00:44:39 (622 KB/s) - ‘sersync2.5.4_64bit_binary_stable_final.tar.gz’ saved [727290/727290]
    
    root@debian9_S:/home/user/sersync# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
    GNU-Linux-x86/
    GNU-Linux-x86/sersync2
    GNU-Linux-x86/confxml.xml
    

    安装sersync

    root@debian9_S:/home/user/sersync# cp -r GNU-Linux-x86/ /usr/local/sersync
    root@debian9_S:/home/user/sersync# tree /usr/local/sersync/
    /usr/local/sersync/
    ├── confxml.xml
    └── sersync2
    
    0 directories, 2 files
    

    规范目录结构

    root@debian9_S:/home/user/sersync# cd /usr/local/sersync/
    root@debian9_S:/usr/local/sersync# mkdir conf bin logs
    root@debian9_S:/usr/local/sersync# ls
    bin  conf  confxml.xml  logs  sersync2
    root@debian9_S:/usr/local/sersync# mv confxml.xml conf
    root@debian9_S:/usr/local/sersync# mv sersync2 bin/sersync
    root@debian9_S:/usr/local/sersync# tree
    .
    ├── bin
    │   └── sersync
    ├── conf
    │   └── confxml.xml
    └── logs
    
    3 directories, 2 files
    
    
    

    配置sersync

    root@debian9_S:/usr/local/sersync# cp conf/confxml.xml conf/confxml.xml.bak.$(date +%F)
    root@debian9_S:/usr/local/sersync# ls conf/
    confxml.xml  confxml.xml.bak.2017-12-24
    root@debian9_S:/usr/local/sersync# vim conf/confxml.xml
    
    root@debian9_S:/usr/local/sersync# vim conf/confxml.xml
    
    root@debian9_S:/home/user# 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="true"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
        </inotify>
    
        <sersync>
        <localpath watch="/home/user/images">
            <remote ip="192.168.66.232" name="images"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="user" passwordfile="/etc/rsyncd.secrets"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="true" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="true" 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>
    

    配置说明

    <debug start="false"/>该行为Debug开启开关。true为开启debug模式,会在sersync正在运行的控制台,打印 inotify 事件与 rsync 同步命令,生产环境一般不开启。

    <fileSystem xfs="false"/> 对于XFS文件系统的用户,需要将这个选项开启,才能使sersync正常工作

    <localpath watch="/home/user/images"> 本地要同步的目录

    <remote ip="192.168.66.232" name="images"/> 同步到那个ip设备的rsync的哪个模块,可添加多个ip的多个模块

    <filter start="false"> <exclude expression="(.*)\.svn"></exclude>

    对于sersync监控的文件,会默认过滤系统的临时文件(以“.”开头,以“~”结尾),除了这些文件外,还可以自定义其它需要过滤的文件。

    通过将 start 设置为 true 后可开启过滤功能,在exclude标签中可使用正则表达式。默认给出的两个例子分别是过滤以“.gz”结尾的文件与过滤监控目录下的info路径(监控路径/info/*),可以根据需求自己添加。但在开启的时候,自己一定要测试下,如果正则表达式出现错误,控制台会有相应提示。相比较使用 Rsync 的 exclude 功能,被过滤的路径,不会加入监控,大大减少 Rsync 同步的通讯量.

    <inotify> <delete start="true"/>

    定义 inotify 监控参数,我们可以根据项目的特点来优化 Sersync。

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

    注意:要使得 createFolder 保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控,所以除非特殊需要,请开启。默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将 delete 参数设置为 false,则不对删除事件进行监控。

    <rsync> <commonParams params="-avzP"/>

    commonParams可以用户自定义rsync参数,默认是-artuz

    auth start=”false” 设置为true的时候,使用rsync的认证模式传送,需要配置user与passwrodfile(–password-file=/etc /rsync.pas),来使用。userDefinedPort 当远程同步目标服务器的rsync端口不是默认端口的时候使用(–port=874)。timeout设置rsync的timeout时间 (–timeout=100)。ssh 使用rsync -e ssh的方式进行传输。

    <failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/>

    对于失败的传输,会进行重新传送,再次失败就会写入rsync_fail_log,然后每隔一段时间(timeToExecute进行设置)执行该脚本再次重新传送,然后清空该脚本。可以通过path来设置日志路径。

    <crontab start="true" schedule="600">

    Crontab定期整体同步功能,crontab可以对监控路径与远程目标主机每隔一段时间进行一次整体同步,可能由于一些原因两次失败重传都失败了,这个时候如果开启了crontab功 能,还可以进一步保证各个服务器文件一致,如果文件量比较大,crontab的时间间隔要设的大一些,否则可能增加通讯开销。schedule这个参数是设置crontab的时间间隔,默认是600分钟。

    如果开启了filter文件过滤功能,那么crontab整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但crontab整体同步的时候 如果不单独设置crontabfilter,还会将需过滤的文件同步到远程,crontab的过滤正则与filter过滤的不同,也给出了两个实例分别对 应与过滤文件与目录。总之如果同时开启了filter与crontab,则要开启crontab的crontabfilter,并按示例设置使其与 filter的过滤一一对应。

    配置sersync密码文件

    root@debian9_S:/home/user# vim /etc/rsyncd.secrets
    root@debian9_S:/home/user# chmod 600 /etc/rsyncd.secrets 
    root@debian9_S:/home/user# cat /etc/rsyncd.secrets
    password
    

    启动sersync

    root@debian9_S:/usr/local/sersync# bin/sersync -r -d -o /usr/local/sersync/conf/confxml.xml
    

    Sersync的多实例和往常的apache,nginx一样,你只需要把配置文件拷贝一份出来,然后针对不同的实例做不同的修改,然后在最后开启 sersync 服务的时候指定不同的配置文件做启动即可!意思就是如果要同步多个帐密或文件夹不同时,则直接启动多个sersync即可,例如:

    root@debian9_S:/usr/local/sersync# bin/sersync -r -d -o /usr/local/sersync/conf/confxml-web.xml
    root@debian9_S:/usr/local/sersync# bin/sersync -r -d -o /usr/local/sersync/conf/confxml-django.xml
    

    启动参数说明

    在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步
    ./sersync -r

    如果需要将sersync运行前,已经存在的所有文件或目录全部同步到远程,要以-r参数运行sersync,将本地与远程整体同步一次。
    如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效。

    查看启动参数帮助
    ./sersync --help

    指定配置文件
    ./sersync -o XXXX.xml
    对于sersync使用可执行文件目录下的默认配置文件confxml.xml,如果需要使用另一个配置文件,可以使用-o参数指定其它配置文件。

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

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

    多个参数可以配合使用
    ./sersync -n 8 -o abc.xml -r -d
    表示,设置线程池工作线程为8个,指定abc.xml作为配置文件,在实时监控前作一次整体同步,以守护进程方式在后台运行。

    通常情况下,对本地到远程整体同步一遍后,在后台运行实时同步。
    ./sersync -d

    单独去运行插件

    插件也可以单独使用,即不对远程目标机进行同步,直接调用插件。

    只调用command插件sersync -d -m command

    只调用refreshCDS插件sersync -d -m refreshCDN

    只调用socket插件sersync -d -m socket

    只调用http插件sersync -d -m http

    sersync进程重启

    root@debian9_S:/home/user# ps -ef | grep sersync
    root       1304      1  0 01:07 ?        00:00:00 bin/sersync -r -d -o /usr/local/sersync/conf/confxml.xml
    root       1343   1341  0 01:10 pts/2    00:00:00 grep sersync
    root@debian9_S:/home/user# kill 1304
    

    配置sersync环境变量(有时不管用)

    root@debian9_S:/home/user# echo 'export PATH=$PATH:/usr/local/sersync/bin'>>/etc/profile
    root@debian9_S:/home/user# tail -1 /etc/profile
    export PATH=$PATH:/usr/local/sersync/bin
    root@debian9_S:/home/user# sersync
    bash: sersync: command not found
    root@debian9_S:/home/user# . /etc/profile
    root@debian9_S:/home/user# sersync
    则会显示一些值
    

    然后全局就可以通过sersync -r -d -o /usr/local/sersync/conf/confxml.xml 执行

    sersync开机自启

    root@debian9_S:/home/user# cat <<EOF >/etc/rc.local
    > #!/bin/sh -e
    > #
    > # rc.local
    > #
    > # This script is executed at the end of each multiuser runlevel.
    > # Make sure that the script will "exit 0" on success or any other
    > # value on error.
    > #
    > # In order to enable or disable this script just change the execution
    > # bits.
    > #
    > # By default this script does nothing.
    >
    > exit 0
    > EOF
    root@debian9_S:/home/user# chmod +x /etc/rc.local
    root@debian9_S:/home/user# vim /etc/rc.local
    root@debian9_S:/home/user# cat /etc/rc.local
    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    /usr/local/sersync/bin/sersync -r -d -o /usr/local/sersync/conf/confxml.xml
    exit 0
    root@debian9_S:/home/user# systemctl start rc-local
    

    ------------------------------------------

    rsync常见错误与解决方法整理

    chroot failed

    @ERROR: chroot failed
    rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3] 
    

    服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。

    auth failed on module tee

    @ERROR: auth failed on module tee 
    rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3] 
    

    服务器端该模块(tee)需要验证用户名密码,但客户端没有提供正确的用户名密码,认证失败。
    提供正确的用户名密码解决此问题。

    Unknown module ‘tee_nonexists'

    @ERROR: Unknown module ‘tee_nonexists' 
    rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3] 
    

    服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。

    在client上遇到问题

    auth failed on module backup

    rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/ 
    rsync: could not open password file "/etc/rsync.pas": No such file or directory (2) 
    Password: 
    @ERROR: auth failed on module backup 
    rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7] 
    

    遇到这个问题:client端没有设置/etc/rsync.pas这个文件,而在使用rsync命令的时候,加了这个参数--
    password-file=/etc/rsync.pas

    rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/ 
    @ERROR: auth failed on module backup 
    rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7] 
    

    遇到这个问题:client端已经设置/etc/rsync.pas这个文件,里面也设置了密码111111,和服务器一致,但是
    服务器段设置有错误,服务器端应该设置/etc/rsync.pas ,里面内容root:111111 ,这里登陆名不可缺少

    chdir failed

    rsync -auzv --progress --password-file=/etc/rsync.pas root@192.168.133.128::backup /home/ 
    @ERROR: chdir failed 
    rsync error: error starting client-server protocol (code 5) at main.c(1506) [Receiver=3.0.7] 
    

    遇到这个问题,是因为服务器端的/home/backup 其中backup这个目录并没有设置,所以提示:chdir failed

    No space left on device

    rsync: write failed on "/home/backup2010/wensong": No space left on device (28) 
    rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7] 
    rsync: connection unexpectedly closed (2721 bytes received so far) [generator] 
    rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7] 
    

    磁盘空间不够,所以无法操作。
    可以通过df /home/backup2010 来查看可用空间和已用空间

    Permission denied

    类似如下的提示:rsync: opendir "/kexue" (in dtsChannel) failed: Permission denied (13)注意查看同步的目录权限是否为755。

    time out

    rsync: failed to connect to 203.100.192.66: Connection timed out (110) 
    rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5] 
    

    检查服务器的端口netstat –tunlp,远程telnet测试。
    可能因为客户端或者服务端的防火墙开启 导致无法通信,可以设置规则放行 rsync(873端口) 或者直接关闭防火墙。

    还有一种在同步过程中可能会提示没有权限 (将同步目录加上SvcwRsync全部权限即可,更简单的方法就是将SvcwRsync设为管理员即可)

    Connection refused

    rsync: failed to connect to 10.10.10.170: Connection refused (111) 
    rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5] 
    

    启动服务:rsync --daemon --config=/etc/rsyncd.conf

    No space left on device

    rsync: recv_generator: mkdir "/teacherclubBackup/rsync……" failed: No space left on device (28) 
    *** Skipping any contents from this failed directory *** 
    

    received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544)

    rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [receiver=3.0.5] 
    rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(544) [generator=3.0.5] 
    

    说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器

    xnetid,onnection reset by peer

    rsync: read error: Connection reset by peer (104) 
    rsync error: error in rsync protocol data stream (code 12) at io.c(759) [receiver=3.0.5] 
    

    查看rsync日志
    rsync: unable to open configuration file "/etc/rsyncd.conf": No such file or directory
    xnetid查找的配置文件位置默认是/etc下,根据具体情况创建软链接。例如:
    ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
    或者更改指定默认的配置文件路径,在/etc/xinetd.d/rsync配置文件中。
    Rsync configure:

    配置一:
    ignore errors
    说明:这个选项最好加上,否则再很多crontab的时候往往发生错误你也未可知,因为你不可能天天去看每时每刻去看log,不加上这个出现错误的几率相对会很高,因为任何大点的项目和系统,磁盘IO都是一个瓶颈
    
    Rsync error: 
    错误一: 
    @ERROR: auth failed on module xxxxx 
    rsync: connection unexpectedly closed (90 bytes read so far) 
    rsync error: error in rsync protocol data stream (code 12) at io.c(150) 
    说明:这是因为密码设置错了,无法登入成功,检查一下rsync.pwd,看客服是否匹配。还有服务器端没启动rsync 服务也会出现这种情况。
    
    错误二: 
    password file must not be other-accessible 
    continuing without password file 
    Password: 
    说明:这是因为rsyncd.pwd rsyncd.sec的权限不对,应该设置为600。如:chmod 600 rsyncd.pwd
    
    错误三: 
    @ERROR: chroot failed 
    rsync: connection unexpectedly closed (75 bytes read so far) 
    rsync error: error in rsync protocol data stream (code 12) at io.c(150) 
    说明:这是因为你在 rsync.conf 中设置的 path 路径不存在,要新建目录才能开启同步
    
    错误四: 
    rsync: failed to connect to 218.107.243.2: No route to host (113) 
    rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9] 
    说明:防火墙问题导致,这个最好先彻底关闭防火墙,排错的基本法就是这样,无论是S还是C,还有ignore errors选项问题也会导致
    
    错误五:
    @ERROR: access denied to www from unknown (192.168.1.123)
    rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
    rsync error: error in rsync protocol data stream (code 12) at io.c(359)
    说明:此问题很明显,是配置选项host allow的问题,初学者喜欢一个允许段做成一个配置,然后模块又是同一个,致使导致
    
    错误六:
    rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]
    rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
    说明:导致此问题多半是服务端服务没有被正常启动,到服务器上去查查服务是否有启动,然后查看下 /var/run/rsync.pid 文件是否存在,最干脆的方法是杀死已经启动了服务,然后再次启动服务或者让脚本加入系统启动服务级别然后shutdown -r now服务器
    
    错误七:
    rsync: read error: Connection reset by peer (104)
    rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
    说明:原数据目录里没有数据存在
    

    相关文章

      网友评论

        本文标题:Debian9配置Sersync+rsync,自动同步

        本文链接:https://www.haomeiwen.com/subject/lxelkftx.html