声明:esrsync客户端不用安装rsync软件

           rsync+inotify客户端需要分别安装软件rsync、inotify


服务器地址分配:

Web1:192.168.1.103 (rsync 服务节点)

Web2:192.168.1.104 (rsync 服务节点)

Web3:192.168.1.105  (rsync 内容发布节点、inotify)


同步的目录是 /mnt ,自动同步顺序为 web3向web1、web2同步

sersync配置_sersync


一、配置 rsync 服务端(web1)


1.安装rsync软件

# yum installrsync


2.手动创建rsync配置文件

# vim /etc/rsyncd.conf

uid = root                           #全局配置开始,运行rsync的用户

gid = root                           #运行rsync的用户组

usechroot = no                    #是否让进程离开工作目录

max connections= 20            #最大并发数

timeout = 600                      #连接超时时间

pid file =/var/run/rsyncd.pid            #指定rsync的pid存放路径

lock file =/var/run/rsync.lock           #指定rsync的锁文件存放路径

log file =/var/log/rsyncd.log             #指定rsync的日志存放路径

[web_log]                          #模块配置开始

path =/data/web_log/             #需要备份的文件路径

ignore errors                       #忽略一些无关的I/O错误

read only = false                  #false关闭,true表示开启表示只读,步允许上传文件

write only = false                   #不允许下载

list = false                          #客户请求可以使用模块列表时是否被列出

hosts allow =192.168.1.0/24           #可以访问此模块的主机主机,*表示任何主机,此处是网段

hosts deny =0.0.0.0/32                    #禁止访问此模块主机地址

auth users =backuser                       #自定义连接该模块的用户名,多个用户用逗号分隔

secrets file = /etc/rsync.password#指定一个包含“用户名:密码”格式的文件


3.创建备份目录

# mkdir -p/data/web_log/


4.建立 rsync 用户名和密码文件

# echo "backuser:123">> /etc/rsync.password        #backuser:123前不能有空格


5.为 /etc/rsync.password授权为 600(这个文件的权限必须是 600)

# chmod 600/etc/rsync.password


6.启动rsync并添加开启自动启动

# /usr/bin/rsync--daemon  &

# echo "/usr/bin/rsync--daemon" >> /etc/rc.local


二、配置 rsync 服务端(web2),和web1一样


# yum installrsync


# vim/etc/rsyncd.conf

uid = root

gid = root

usechroot = no

max connections= 20

timeout = 600

pid file =/var/run/rsyncd.pid

lock file =/var/run/rsync.lock  

log file =/var/log/rsyncd.log

[web_log]

path =/data/web_log/             #定义你需要备份的路径,可以跟web1上不一样

ignore errors

read only = false

writeonly = false

list =false

hosts allow = *

hosts deny =0.0.0.0/32

auth users =backuser

secrets file =/etc/rsync.password


# mkdir -p/data/web_log/


# echo "backuser:123">> /etc/rsync.password            #backuser:123前不能有空格


# chmod 600/etc/rsync.password


# /usr/bin/rsync--daemon  &

# echo "/usr/bin/rsync--daemon" >> /etc/rc.local


到此,服务器端配置完成


三、配置sersync客户端(web3),不用安装rsync软件


1.设置 rsync 客户端的密码文件,客户端只需要设置 rsync 同步的密码即可,不用设置用户名

# echo "123"> /etc/rsync.password            #123前不能有空格


2.将密码文件的权限设置成 600(这个文件的权限必须是600)

# chmod 600/etc/rsync.password


3.安装sersync软件包

# wget http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz

注:若在64位平台安装则可下载64位sersync源码包,本例用32位

# wget http://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz

# tar xvf sersync2.5_64bit_binary_stable_final.tar.gz

# mkdir /usr/local/sersync

# mv GNU-Linux-x86/* /usr/local/sersync/

# cd/usr/local/sersync/

# vimconfxml.xml        //红色部分都需要修改

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

<head version="2.5">

   <host hostip="localhost" port="8008"></host>     #本机ip和要监听的端口,可以不修改

   <debugstart="false"/>

   <fileSystemxfs="false"/>

   <filterstart="false">

   <excludeexpression="(.*)\.svn"></exclude>

   <excludeexpression="(.*)\.gz"></exclude>

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

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

   </filter>

   <inotify>

   <deletestart="true"/>

   <createFolderstart="true"/>

   <createFilestart="false"/>

   <closeWritestart="true"/>

   <moveFromstart="true"/>

   <moveTostart="true"/>

   <attribstart="false"/>

   <modifystart="false"/>

   </inotify>


   <sersync>

   <localpath watch="/mnt">    #本地需要同步的目录

       <remote ip="192.168.1.103" name="web_log"/> #远程端服务ip,模块

       <remote ip="192.168.1.104" name="web_log"/>

       <!--<remoteip="192.168.8.40" name="tongbu"/>-->

   </localpath>


   <rsync>

       <commonParamsparams="-artuz"/>    

       <auth start="ture" users="backuser" passwordfile="/etc/rsync.password"/>    #修改连接的用户名和密码

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

       <timeout start="false"time="100"/><!-- timeout=100 -->

       <ssh start="false"/>

   </rsync>

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

   <crontabstart="false" schedule="600"><!--600mins-->

       <crontabfilterstart="false">

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

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

       </crontabfilter>

   </crontab>

   <pluginstart="false" name="command"/>

   </sersync>


   <pluginname="command">

   <paramprefix="/bin/sh" suffix=""ignoreError="true"/>  <!--prefix/opt/tongbu/mmm.sh suffix-->

   <filterstart="false">

       <includeexpression="(.*)\.php"/>

       <includeexpression="(.*)\.sh"/>

   </filter>

   </plugin>


   <pluginname="socket">

   <localpathwatch="/opt/tongbu">

       <deshost ip="192.168.138.20"port="8009"/>

   </localpath>

   </plugin>

   <pluginname="refreshCDN">

   <localpathwatch="/data0/htdocs/cms.xoyo.com/site/">

       <cdninfodomainname="ccms.chinacache.com" port="80"username="xxxx" passwd="xxxx"/>

       <sendurlbase="http://pic.xoyo.com/cms"/>

       <regexurl regex="false"match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/p_w_picpaths"/>

   </localpath>

   </plugin>

</head>



启动sersync守护进程,注意下面红色部分

#/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

set the systemparam

execute:echo 50000000> /proc/sys/fs/inotify/max_user_watches

execute:echo 327679 >/proc/sys/fs/inotify/max_queued_events

parse thecommand param

option: -d      run as a daemon

option: -r       rsync all the local files to the remoteservers before the sersync work

option: -o      config xml name:  /usr/local/sersync/confxml.xml

daemon threadnum: 10

parse xml configfile

host ip : localhost   host port: 8008

daemon start,sersync runbehind the console

use rsyncpassword-file :

user is     backuser

passwordfile is     /etc/rsync.password

config xml parse success

please set/etc/rsyncd.conf max connections=0 Manually

sersync workingthread 12  = 1(primary thread) + 1(failretry thread) + 10(daemon sub threads)

Max threadsnumbers is: 22 = 12(Thread pool nums) + 10(Sub threads)

please accordingyour cpu ,use -n param to adjust the cpu rate

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

rsync thedirectory recursivly to the remote servers once

working pleasewait...

execute command: cd/mnt && rsync -artuz -R --delete ./ backuser@192.168.1.103::web_log--password-file=/etc/rsync.password >/dev/null 2>&1

run the sersync:

watch path is: /mnt


双向同步思路:双向同步只需要在把主从配置中的反过来配置一遍就行了,xml配置文件中的配置也为对方ip地址和目录.



以下是esrsync命令参数介绍

参数-d:启用守护进程模式

参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍

参数-n: 指定开启守护线程的数量,默认为10个

参数-o:指定配置文件,默认使用confxml.xml文件

参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块

参数-m:单独启用其他模块,使用 -m socket 开启socket模块

参数-m:单独启用其他模块,使用 -m http 开启http模块

不加-m参数,则默认执行同步程序


以下是confxml.xml配置文件参数介绍

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

<head version="2.5">

   <host hostip="localhost" port="8008"></host>     #本机ip和要监听的端口,可以不修改

   <debugstart="false"/>     #开户debug信息,会在sersync当前运行台,打印Debug信息

   <fileSystemxfs="false"/>    #是否支持xfs文件系统

   <filterstart="false">          #是否开户文件过滤,可以在下面添加过滤类型

   <excludeexpression="(.*)\.svn"></exclude>

   <excludeexpression="(.*)\.gz"></exclude>

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

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

   </filter>

   <inotify>    #inotify监控的事件

   <deletestart="true"/>    #是否保持Sersync和同步端两端文件完全一致

   <createFolderstart="true"/>  #创建目录的支持,如果不开户,不能监控子目录

   <createFilestart="false"/>     #是否监控文件的创建

   <closeWritestart="true"/>    #是否监控文件关闭,开户可保证文件的完整性

   <moveFromstart="true"/>

   <moveTostart="true"/>

   <attribstart="false"/>

   <modifystart="false"/>

   </inotify>


   <sersync>

   <localpath watch="/mnt">    #这里定义要监控的本地目录,这个很重要

       <remote ip="192.168.1.103" name="web_log"/> #要同步到哪台服务器,远程主机ip和要同步的模块。

       <remote ip="192.168.1.104" name="web_log"/>

       <!--<remoteip="192.168.8.40" name="tongbu"/>-->

   </localpath>


   <rsync>      #配置Rsync信息

       <commonParamsparams="-artuz"/>     #rsync的参数

       <auth start="ture" users="backuser" passwordfile="/etc/rsync.password"/>    #连接远程服务的用户名,在rsync服务端已定义;密码为本地设置的密码

       <userDefinedPort start="false"port="874"/><!-- port=874 -->   #定义rsync端口

       <timeout start="false"time="100"/><!-- timeout=100 -->    #定义传输超时时间

       <ssh start="false"/>       #Rsync的时候,是否使用ssh加密

   </rsync>

   <failLogpath="/tmp/rsync_fail_log.sh"timeToExecute="60"/><!--default every 60mins execute once-->    #sersync传输失败脚本路径,每隔60分钟重新执行此脚本,执行完毕自动清空

   <crontabstart="false" schedule="600"><!--600mins-->    #定义crontab定期完全同步两端文件

       <crontabfilterstart="false">   #crontab同步时候的过滤条件,上面的过滤部开头要开

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

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

       </crontabfilter>

   </crontab>

   <pluginstart="false" name="command"/>    # 下面就是一些插件的设置了

   </sersync>


   <pluginname="command">

   <paramprefix="/bin/sh" suffix=""ignoreError="true"/>  <!--prefix/opt/tongbu/mmm.sh suffix-->

   <filterstart="false">

       <includeexpression="(.*)\.php"/>

       <includeexpression="(.*)\.sh"/>

   </filter>

   </plugin>


   <pluginname="socket">

   <localpathwatch="/opt/tongbu">

       <deshost ip="192.168.138.20"port="8009"/>

   </localpath>

   </plugin>

   <pluginname="refreshCDN">

   <localpathwatch="/data0/htdocs/cms.xoyo.com/site/">

       <cdninfodomainname="ccms.chinacache.com" port="80"username="xxxx" passwd="xxxx"/>

       <sendurlbase="http://pic.xoyo.com/cms"/>

       <regexurl regex="false"match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/p_w_picpaths"/>

   </localpath>

   </plugin>

</head>



以下是rsyncd.conf配置文件参数介绍

全局参数说明

在文件中[modlue]之前的所有参数都是全局参数,当然也可以在全局参数部分定义模块参数,这时候该参数的值就是所有模块的默认值。


motd file

"motd file"参数用来指定一个消息文件,当客户连接服务器时该文件的内容显示给客户,默认是没有motd文件的。

pid file

指定rsyncpid文件。

socket options

设置socket配置

syslog facility

指定rsync发送日志消息给syslog时的消息级别,常见的消息级别是:uth, authpriv,  cron, daemon, ftp, kern, lpr, mail, news, security,  sys-log, user,  uucp, local0, local1, local2, local3,local4, local5, local6local7。默认值是daemon

Log file

"log file"指定rsync的日志文件,而不将日志发送给syslog


模块参数说明

在全局参数之后就需要定义一个或多个模块了,模块中可以定义以下参数:
红色的为必须设置绿色的为涉及到安全问题,应该使用默认配置;其他的为可以不进行设置。

comment

给模块指定一个描述,该描述连同模块名在客户连接得到模块列表时显示给客户。默认没有描述定义。

path

指定该模块的供备份的目录树路径,该参数是必须指定的。

use  chroot

如果"use chroot"指定为true,那么rsync在传输文件以前首先chrootpath参数所指定的目录下。这样做的原因是实现额外的安全防护,但是缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true

numeric ids


munge symlinks

使传进来的文件的链接失效,但是可以恢复。只有在use chroot =  true and inside-chroot path"/"时,为disable,其它默认为enable。防止传进的链接文件指向其不应该指向的文件,从而造成安全漏洞。

charset

字符转换,如果不设置客户端—iconv将会被禁止。

max  connections

指定该模块的最大并发连接数量以保护服务器,超过限制的连接请求将被告知随后再试。默认值是0,也就是没有限制。

max verbosity

Log信息数量控制,防止出现过多的verboselog信息,默认为level 1

lock file

指定支持max  connections参数的锁文件,默认值是/var/run/rsyncd.lock

read  only

该选项设定是否允许客户上载文件。如果为true那么任何上载请求都会失败,如果为false并且服务器目录读写权限允许那么上载是允许的。默认值为true

write only

设置是否禁止客户端下载数据。默认为false

list

该选项设定当客户请求可以使用的模块列表时,该模块是否应该被列出。如果设置该选项为false,可以创建隐藏的模块。默认值是true

uid

该选项指定当该模块传输文件时守护进程应该具有的uid,配合gid选项使用可以确定哪些可以访问怎么样的文件权限,默认值是"nobody"

gid

该选项指定当该模块传输文件时守护进程应该具有的gid。默认值为"nobody"

fake super

和客户端--fake-user的参数相似,=true是无论守护进程启动权限是否为root,存储的文件将所有属性存储。

filter

文件访问权限控制。daemon filter  chain is  built from the "filter", "include from",  "include",  "exclude from", and "exclude"  parameters

exlude

用来指定多个由空格隔开的多个模式列表,并将其添加到exclude列表中。这等同于在客户端命令中使用--exclude来指定模式,不过配置文件中指定的exlude模式不会传递给客户端,而仅仅应用于服务器。一个模块只能指定一个exlude选项,但是可以在模式前面使用"-""+"来指定是 exclude还是include。但是需要注意的一点是该选项有一定的安全性问题,客户很有可能绕过exlude列表,如果希望确保特定的文件不能被访问,那就最好结合uid/gid选项一起使用。

exlude from

指定一个包含exclude模式的定义的文件名,服务器从该文件中读取exlude列表定义。

include

用来指定多个由空格隔开的多个rsync并应该exlude的模式列表。这等同于在客户端命令中使用--include来指定模式,结合include exlude可以定义复杂的exlude/include规则。一个模块只能指定一个include选项,但是可以在模式前面使用"-""+"来指定是exclude还是include

include from

指定一个包含include模式的定义的文件名,服务器从该文件中读取include列表定义。

incoming chmod

设置“set of comma-separated chmod   strings “来修改所有传入的文件,并且将在所有权限设置完成后执行,除非客户端指定—perms。格式同chmod一致。

outgoing chmod

同上,在文件传出前首先运行设置参数。

auth  users

该选项指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块。这里的用户和系统用户没有任何关系。如果"auth  users"被设置,那么客户端发出对该模块的连接请求以后会被rsync请求challenged进行验证身份这里使用的  challenge/response认证协议。用户的名和密码以明文方式存放在"secrets  file"选项指定的文件中。默认情况下无需密码就可以连接模块(也就是匿名方式)

secrets  file

该选项指定一个包含定义用户名:密码对的文件。只有在"auth   users"被定义时,该文件才有作用。文件每行包含一个username:passwd对。一般来说密码最好不要超过8个字符。没有默认的  secures  file名,需要限式指定一个。(例如:/etc/rsyncd.secrets)

strict  modes

该选项指定是否监测密码文件的权限,如果该选项值为true那么密码文件只能被rsync服务器运行身份的用户访问,其他任何用户不可以访问该文件。默认值为true

hosts allow

该选项指定哪些IP的客户允许连接该模块。客户模式定义可以是以下形式:

1 xxx.xxx.xxx.xxx,客户主机只有完全匹配该IP才允许访问。例如:192.167.0.1

2 a.b.c.d/n,属于该网络的客户都允许连接该模块。例如:192.168.0.0/24

3 a.b.c.d/e.f.g.h,属于该网络的客户都允许连接该模块。例如:192.168.0.0/255.255.255.0

4 一个主机名,客户主机只有拥有该主机名才允许访问,例如:backup.linuxaid.com.cn

5 *.linuxaid.com.cn,所有属于该域的主机都允许。

默认是允许所有主机连接。

hosts deny

指定不允许连接rsync服务器的机器,可以使用hosts allow的定义方式来进行定义。默认是没有hosts deny定义。

ignore errors

指定rsyncd在判断是否运行传输时的删除操作时忽略server上的IP错误,一般来说rsync在出现IO错误时将将跳过--delete操作,以防止因为暂时的资源不足或其它IO错误导致的严重问题。

ignore  nonreadable

指定rysnc服务器完全忽略那些用户没有访问权限的文件。这对于在需要备份的目录中有些文件是不应该被备份者得到的情况是有意义的。应该设置为true

transfer logging

使rsync服务器使用ftp格式的文件来记录下载和上载操作在自己单独的日志中。

log format

通过该选项用户在使用transfer  logging可以自己定制日志文件的字段。其格式是一个包含格式定义符的字符串,可以使用的格式定义符如下所示:

%a 远程IP地址;%b 实际传输的字节数;%B the  permission bits of  the file (e.g. rwxrwxrwt) %l 文件长度字符数;%c 当发送文件时,该字段记录该文件的校验码;%f 文件名;%G the gid of  the file  (decimal) or "DEFAULT" %h 远程主机名;   %p 该次rsync会话的进程id%o 操作类型:"send""recv"    %P 模块路径; %m 模块名;%i an itemized  list of what is being updated%U the uid of the file (decimal)

%t 当前时间;%u 认证的用户名(匿名时是null)

默认log格式为:"%o %h  [%a] %m (%u) %f  %l",一般来说,在每行的头上会添加"%t [%p]  "。在源代码中同时发布有一个叫rsyncstatsperl脚本程序来统计这种格式的日志文件。

timeout

通过该选项可以覆盖客户指定的IP超时时间。通过该选项可以确保rsync服务器不会永远等待一个崩溃的客户。超时单位为秒钟,0表示没有超时定义,这也是默认值。对于匿名rsync服务器来说,一个理想的数字是600

refuse options

通过该选项可以定义一些不允许客户对该模块使用的命令参数列表。这里必须使用命令全名,而不能是简称。但发生拒绝某个命令的情况时服务器将报告错误信息然后退出。如果要防止使用压缩,应该是:"dont  compress = *"

dont compress

用来指定那些不进行压缩处理再传输的文件,默认值是

*.gz *.tgz *.zip *.z *.rpm *.deb *.iso  *.bz2 *.tbz

pre-xfer exec,  post-xfer exec

在传输前指定要运行的命令行,如果命令运行失败,将停止传输。

注意:命令行将使用启动守护进程的用户权限执行!

syslog facility

指定rsync发送日志消息给syslog时的消息级别,常见的消息级别是:uth, authpriv,  cron, daemon, ftp, kern, lpr, mail, news, security,  sys-log, user,  uucp, local0, local1, local2, local3,local4, local5, local6local7。默认值是daemon

Log file

"log file"指定rsync的日志文件,而不将日志发送给syslog