企业实时同步方案----Rsync+Sersync

简介:

    在博文企业实时同步方案----Sersync介绍中我们详细介绍了Sersync的原理,设计架构以及和 Inotify 等等的优势区别。这里我就带大家一起来做一下 Rsync +Sersync 这个同步分发架构案例。



实验环境介绍:

1
2
3
4
5
6
7
内核版本:2.6.32-431.el6.x86_64
系统采用最小化安装,系统经过了基本优化,selinux为关闭状态,iptables为无限制模式
源码包存放位置: /root
Rsync客户端+Sersync服务器(SERSYNC),承担角色MASTER,IP:172.16.100.3,主机名: rsync -client-sersync
SERSYNC_SLAVE,作为SERSYNC的从机,如果SERSYNC宕机,SERSYNC_SLAVE来接管服务,保证业务不中断,本实验不包括它!
Web服务器A(即Rsync服务端)(SWEB1),承担角色S1,IP:172.16.100.1,主机名: rsync -server-1
Web服务器B(即Rsync服务端)(SWEB2),承担角色S2,IP:172.16.100.2,主机名: rsync -server-2



架构图:

wKioL1OzzHjAZChiAAIEogZk1GI457.jpg


下面,就开始我们的实验! 


一、在两台 SWEB 服务器上部署 Rsync 服务端程序

特别提醒:本文的SWEB服务器即为SWEB1(172.16.100.1),SWEB2(172.16.100.2)。此处仅以SWEB1的 Rsync服务端部署为例,SWEB2的部署和SWEB1一样,此处不再敖述。

1、安装 Rsync

1
[root@SWEB1 ~] # yum install rsync -y


2、修改 Rsync 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@SWEB1 ~] # cat > /etc/rsyncd.conf << EOF
#Rsync server
#created by sunsky 00:17 2013-06-28
##rsyncd.conf start##
uid = root       # rsync对后面模块中的path路径拥有什么权限
gid = root       # rsync对后面模块中的path路径拥有什么权限
use chroot = no       # 安全操作
max connections = 2000       # 定义连接数2000
timeout = 600       # 600秒超时
pid  file  /var/run/rsyncd .pid
lock  file  /var/run/rsync .lock
log  file  /var/log/rsyncd .log
ignore errors       # 忽略错误
read  only =  false     # false才能上传文件,true不能上传文件
list =  false     # 文件列表
hosts allow = 172.16.100.0 /24
hosts deny = *
auth  users  = rsync_bak     # 虚拟用户,同步时需要用这个用户
secrets  file  /etc/rsync .password       # 密码文件
#####################################
[web]       # 模块名称
comment = redhat.sx site files by sunsky 00:17 2013-06-28     # 注释
path =  /data/web                  # 模块的路径
####################################
[download]
comment = redhat.sx site sit data files  by sunsky 00:17 2013-06-28
path =  /data/download
#####################################
EOF




特别提示:
此处,使用一个多目录同步的案例。大家可以看需求,如果需求只有一个,那仅仅做一个目录即可。


       上面的Rsync服务的配置文件,表明允许 172.16.100.0 网段的主机访问,Rsync同步模块名为[web]和[download],将同步过来的文件分别放入对应path指定的目录/data/web,/data/download下。

       如果有多台目标服务器,则每一台都需要进行类似的rsync服务端配置,上面的uid和gid需要换成你服务器的相应的同步用户。注意,Rsync服务账户(本文用root)要有对被同步目录(/data/web和/data/download)的写入和更新权限。

 

3、创建相关待同步目录

1
2
3
4
5
6
[root@SWEB1 ~] # mkdir /data/{web,download} -p
[root@SWEB1 ~] # tree /data
/data
├── download
└── web
2 directories, 0 files

提示:此步在S1,S2上都要执行,否则,rsync服务会因为没有PATH路径而无法启动。


4、相关认证和权限项配置

1
2
3
4
5
6
[root@SWEB1 /] # echo 'rsync_bak:redhat' > /etc/rsync.password
[root@SWEB1 /] # chmod 600 /etc/rsync.password
[root@SWEB1 /] # cat /etc/rsync.password
rsync_bak:redhat
[root@SWEB1 /] # ll /etc/rsync.password
-rw-------. 1 root root 7 Jun  4 00:20  /etc/rsync .password



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

1
[root@SWEB1 ~] # rsync --daemon


6、查看rsync服务状态

1
2
3
4
[root@SWEB1 /] # lsof -i tcp:873
COMMAND   PID USER   FD   TYPE DEVICE SIZE /OFF  NODE NAME
rsync    20982 root    3u  IPv4  88170      0t0  TCP *: rsync  (LISTEN)
rsync    20982 root    5u  IPv6  88171      0t0  TCP *: rsync  (LISTEN)


7、为rsync添加开机自启动

1
2
3
4
5
[root@SWEB1 /] # echo "# rsyncd service daemon by sun 20140702" >>/etc/rc.local
[root@SWEB1 /] # echo "/usr/bin/rsync --daemon"  >> /etc/rc.local
[root@SWEB1 /] # grep daemon /etc/rc.local
# rsyncd service daemon by sun 20140702
/usr/bin/rsync  --daemon


这里顺带附上重启的命令,rsync重启有点麻烦,需要以先杀掉后台守护进程,然后再启动的方式来重启服务。

1
2
[root@SWEB1 /] # pkill rsync
[root@SWEB1 /] # rsync --daemon


 

二、在 SERSYNC 上配置rsync客户端

1、安装Rsync并配置相关权限

在 SERSYNC 上配置 RSYNC 客户端相关权限认证:

1
2
3
4
5
6
7
[root@SERSYNC /] # yum install rsync -y
[root@SERSYNC /] # echo "redhat" > /etc/rsync.password
[root@SERSYNC /] # chmod 600 /etc/rsync.password
[root@SERSYNC /] # cat /etc/rsync.password
Redhat
[root@SERSYNC ~] # ll /etc/rsync.password
-rw-------. 1 root root 7 Jun  4 00:20  /etc/rsync .password



2、在SERSYNC上手动测试rsync的同步情况

特别强调:此步很关键,如果这不能同步,后面的 SERSYNC 配好了也不会同步数据。

1)分别创建待同步数据

1
2
3
4
5
6
7
8
9
[root@SERSYNC ~] # mkdir /data/{web,download} -p
[root@SERSYNC ~] # touch /data/{web/index.html,download/a.jpg}
[root@SERSYNC ~] # tree /data
/data
├── download
│   └── a.jpg
└── web
     └── index.html
2 directories, 2 files



2)执行同步命令

针对SWEB1(172.16.100.1):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@SERSYNC ~] # rsync -avzP /data/web rsync_bak@172.16.100.1::web/ --password-file=/etc/rsync.password
sending incremental  file  list
web/
web /index .html
            0 100%    0.00kB /s     0:00:00 (xfer #1, to-check=0/2)
sent 92 bytes  received 31 bytes  246.00 bytes /sec
total size is 0  speedup is 0.00
[root@SERSYNC ~] # rsync -avzP /data/download/ rsync_bak@172.16.100.1::download/ --password-file=/etc/rsync.password
sending incremental  file  list
./
a.jpg
            0 100%    0.00kB /s     0:00:00 (xfer #1, to-check=0/2)
sent 75 bytes  received 30 bytes  210.00 bytes /sec
total size is 0  speedup is 0.00


针对SWEB2(172.16.100.2):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@SERSYNC ~] # rsync -avzP /data/web rsync_bak@172.16.100.2::web/ --password-file=/etc/rsync.password
sending incremental  file  list
web/
web /index .html
            0 100%    0.00kB /s     0:00:00 (xfer #1, to-check=0/2)
sent 92 bytes  received 31 bytes  246.00 bytes /sec
total size is 0  speedup is 0.00
[root@SERSYNC ~] # rsync -avzP /data/download/ rsync_bak@172.16.100.2::download/ --password-file=/etc/rsync.password
sending incremental  file  list
./
a.jpg
            0 100%    0.00kB /s     0:00:00 (xfer #1, to-check=0/2)
sent 75 bytes  received 30 bytes  70.00 bytes /sec
total size is 0  speedup is 0.00


同步完之后,分别对SWEB1,SWEB2的相应目录进行查看!此处以 SWEB1 为例:提示:在后面进行部署SERSYNC之前,SERSYNC主服务器(即SERSYNC)上必须要确保手动可以把文件推送到SWEB1,SWEB2上,这样后续SERSYNC才能调用这些命令来自动推送。

1
2
3
4
5
6
7
8
[root@SWEB1 ~] # tree /data/
/data/
├── download
│   └── a.jpg
└── web
     └── web
         └── index.html
3 directories, 2 files



三、在 SERSYNC 上配置 SERSYNC 服务

    上面的工作做好之后呢,下面就开始正式配置我们的Sersync了!

    我们在Sersync安装过程中所用到包均是从谷歌Sersync项目组取得的,地址:https://code.google.com/p/sersync/downloads/list

       这里,我所用的将是这个 sersync2.5.4_64bit_binary_stable_final.tar.gz 的64位可执行文件版本,里面已经包含了配置文件与可执行文件。下面的操作也都是基于该版本去做的。

1、准备 Sersync 的安装包

1
2
3
4
5
6
7
[root@SERSYNC ~] # mkdir /source/
[root@SERSYNC ~] # cd /source/
[root@SERSYNC ~] # wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@SERSYNC  source ] # tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz 
GNU-Linux-x86/
GNU-Linux-x86 /sersync2
GNU-Linux-x86 /confxml .xml



2、安装 Sesync 

1
2
3
4
5
6
[root@SERSYNC  source ] # cp -r GNU-Linux-x86 /usr/local/sersync
[root@SERSYNC  source ] # tree /usr/local/sersync
/usr/local/sersync
├── confxml.xml
└── sersync2
0 directories, 2 files



3、规范 Sersync 目录结构

1
2
3
4
5
6
7
8
9
10
11
12
[root@SERSYNC  source ] # cd /usr/local/sersync
[root@SERSYNC sersync] # mkdir conf bin logs
[root@SERSYNC sersync] # mv confxml.xml conf
[root@SERSYNC sersync] # mv sersync2 bin/sersync
[root@SERSYNC sersync] # tree
.
├── bin
│   └── sersync
├── conf
│   └── confxml.xml
└── logs
3 directories, 2 files



4、配置 Sersync

[root@SERSYNC sersync]# cp conf/confxml.xml conf/confxml.xml.bak.$(date +%F)

[root@SERSYNC sersync]# ls

bin  conf  logs

[root@SERSYNC sersync]# ls conf/

confxml.xml  confxml.xml.bak.2014-06-04

初始化的配置文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
[root@SERSYNC sersync] # cat conf/confxml.xml -n
      1<?xml version= "1.0"  encoding= "ISO-8859-1" ?>
      2< head  version= "2.5" >
      3    <host hostip= "localhost"  port= "8008" >< /host >
      4    <debug start= "false" />
      5    <fileSystem xfs= "false" />
      6    <filter start= "false" >
      7<exclude expression= "(.*)\.svn" >< /exclude >
      8<exclude expression= "(.*)\.gz" >< /exclude >
      9<exclude expression= "^info/*" >< /exclude >
     10<exclude expression= "^static/*" >< /exclude >
     11    < /filter >
     12    <inotify>
     13<delete start= "true" />
     14<createFolder start= "true" />
     15<createFile start= "false" />
     16<closeWrite start= "true" />
     17<moveFrom start= "true" />
     18<moveTo start= "true" />
     19<attrib start= "false" />
     20<modify start= "false" />
     21    < /inotify >
     22
     23    <sersync>
     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 >
     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 >
     36<failLog path= "/tmp/rsync_fail_log.sh"  timeToExecute= "60" /><!--default every 60mins execute once-->
     37< crontab  start= "false"  schedule= "600" ><!--600mins-->
     38    <crontabfilter start= "false" >
     39<exclude expression= "*.php" >< /exclude >
     40<exclude expression= "info/*" >< /exclude >
     41    < /crontabfilter >
     42< /crontab >
     43<plugin start= "false"  name= "command" />
     44    < /sersync >
     45
     46    <plugin name= "command" >
     47<param prefix= "/bin/sh"  suffix= ""  ignoreError= "true" /><!--prefix  /opt/tongbu/mmm .sh suffix-->
     48<filter start= "false" >
     49    <include expression= "(.*)\.php" />
     50    <include expression= "(.*)\.sh" />
     51< /filter >
     52    < /plugin >
     53
     54    <plugin name= "socket" >
     55<localpath  watch = "/opt/tongbu" >
     56    <deshost ip= "192.168.138.20"  port= "8009" />
     57< /localpath >
     58    < /plugin >
     59    <plugin name= "refreshCDN" >
     60<localpath  watch = "/data0/htdocs/cms.xoyo.com/site/" >
     61    <cdninfo domainname= "ccms.chinacache.com"  port= "80"  username= "xxxx"  passwd = "xxxx" />
     62    <sendurl base= "http://pic.xoyo.com/cms" />
     63    <regexurl regex= "false"  match= "cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images" />
     64< /localpath >
     65    < /plugin >
     66< /head >


为了满足我们的需求,我们需要修改如下几处设置:

1)修改24-28行的内容,原内容为:

1
2
3
4
5
  <localpathwatch= "/opt/tongbu" >        #定义本地要同步的目录
               <remoteip= "127.0.0.1"  name= "tongbu1" />    #
               <!--<remoteip= "192.168.8.39"  name= "tongbu" />-->     #同步到哪个机器,,同步到机器的哪个模块
               <!--<remoteip= "192.168.8.40"  name= "tongbu" />-->
           < /localpath >


修改后内容为:

1
2
3
4
5
<localpathwatch= "/data/web" >
               <remoteip= "172.16.100.1"  name= "web" />     #这里取掉了两旁的注释
               <remoteip= "172.16.100.2"  name= "web" />     #这里取掉了两旁的注释
           < /localpath >
该文件中分隔符形式为:<!-- ###########################-->


2)修改31-34行,认证相关部分:

1
2
3
4
5
<commonParamsparams= "-artuz" />
<auth start= "false" users = "root"  passwordfile= "/etc/rsync.pas" />
               <userDefinedPortstart= "false"  port= "874" /><!-- port=874 -->
               <timeoutstart= "false"  time = "100" /><!-- timeout=100 -->
               <sshstart= "false" />


修改后内容为:

1
2
3
4
5
<commonParamsparams= "-aruz" />
               <auth start= "true" users = "rsync_bak" passwordfile= "/etc/rsync.password" />
               <userDefinedPortstart= "false"  port= "874" /><!-- port=874 -->
               <timeoutstart= "true"  time = "100" /><!-- timeout=100 -->
               <sshstart= "false" />


提示:上面的几个配置就是在拼接

1
2
3
4
rsync  -aruz --timeout=100  /data/web 
rsync_bak@172.16.100.1::www/
  
--password- file = /etc/rsync .password



3)修改36-37行

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


修改后内容为

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


当同步失败后,日志记录到/usr/local/sersync/logs/rsync_fail_log.sh,并且每60分钟对失败的log进行重新同步。

修改后的完整配置文件为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[root@SERSYNC sersync] # cat -n /usr/local/sersync/conf/confxml.xml
      1<?xml version= "1.0"  encoding= "ISO-8859-1" ?>
      2< head  version= "2.5" >
      3    <host hostip= "localhost"  port= "8008" >< /host >
      4    <debug start= "false" />
      5    <fileSystem xfs= "false" />
      6    <filter start= "false" >
      7<exclude expression= "(.*)\.svn" >< /exclude >
      8<exclude expression= "(.*)\.gz" >< /exclude >
      9<exclude expression= "^info/*" >< /exclude >
     10<exclude expression= "^static/*" >< /exclude >
     11    < /filter >
     12    <inotify>
     13<delete start= "true" />
     14<createFolder start= "true" />
     15<createFile start= "false" />
     16<closeWrite start= "true" />
     17<moveFrom start= "true" />
     18<moveTo start= "true" />
     19<attrib start= "false" />
     20<modify start= "false" />
     21    < /inotify >
     22
     23    <sersync>
     24<localpath  watch = "/data/web" >
     25    <remote ip= "172.16.100.1"  name= "web" />
     26    <remote ip= "172.16.100.2"  name= "web" />
     27< /localpath >
     28< rsync >
     29    <commonParams params= "-aruz" />
     30    <auth start= "true"  users = "rsync_bak"  passwordfile= "/etc/rsync.password" />
     31    <userDefinedPort start= "false"  port= "874" /><!-- port=874 -->
     32    <timeout start= "true"  time = "100" /><!-- timeout=100 -->
     33    < ssh  start= "false" />
     34< /rsync >
     35<failLog path= "/usr/local/logs/rsync_fail_log.sh"  timeToExecute= "60" /><!--default every 60mins execute once-->
     36< crontab  start= "false"  schedule= "600" ><!--600mins-->
     37    <crontabfilter start= "false" >
     38<exclude expression= "*.php" >< /exclude >
     39<exclude expression= "info/*" >< /exclude >
     40    < /crontabfilter >
     41< /crontab >
     42<plugin start= "false"  name= "command" />
     43    < /sersync >
     44
     45    <plugin name= "command" >
     46<param prefix= "/bin/sh"  suffix= ""  ignoreError= "true" /><!--prefix  /opt/tongbu/mmm .sh suffix-->
     47<filter start= "false" >
     48    <include expression= "(.*)\.php" />
     49    <include expression= "(.*)\.sh" />
     50< /filter >
     51    < /plugin >
     52
     53    <plugin name= "socket" >
     54<localpath  watch = "/opt/tongbu" >
     55    <deshost ip= "192.168.138.20"  port= "8009" />
     56< /localpath >
     57    < /plugin >
     58    <plugin name= "refreshCDN" >
     59<localpath  watch = "/data0/htdocs/cms.xoyo.com/site/" >
     60    <cdninfo domainname= "ccms.chinacache.com"  port= "80"  username= "xxxx"  passwd = "xxxx" />
     61    <sendurl base= "http://pic.xoyo.com/cms" />
     62    <regexurl regex= "false"  match= "cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images" />
     63< /localpath >
     64    < /plugin >
     65< /head >

 

以上的配置文件,我们仅仅配置了针对 SWEB1 和 SWEB2 上web这个模块,即/data/web目录的自动同步。由于我们还有一个download模块,即/data/download目录需要进行自动同步。因此这就意味着,我们需要配置多实例了。

Sersync的多实例和往常的apache,nginx一样,你只需要把配置文件拷贝一份出来,然后针对不同的实例做不同的修改,然后在最后开启 sersync 服务的时候指定不同的配置文件做启动即可!


配置针对download的实例配置文件:

1
[root@SERSYNC sersync] # cp /usr/local/sersync/conf/confxml.xml /usr/local/sersync/conf/download_confxml.xml


修改方法如上,这里仅贴出和上个实例之间不一样的地方:

1
2
3
4
5
6
     23    <sersync>
     24<localpath  watch = "/data/download" >
     25    <remote ip= "172.16.100.1"  name= "download" />
     26    <remote ip= "172.16.100.2"  name= "download" />
     27< /localpath >
     28< rsync >



5、开启 Sersync 守护进程进行数据同步

a、配置 Sersync 环境变量

1
2
3
4
5
6
[root@SERSYNC sersync] # echo 'export PATH=$PATH:/usr/local/sersync/bin'>>/etc/profile
[root@SERSYNC sersync] # tail -1 /etc/profile
export  PATH=$PATH: /usr/local/sersync/bin
[root@SERSYNC sersync] # . /etc/profile
[root@SERSYNC sersync] # which sersync
/usr/local/sersync/bin/sersync


启动的过程及结果:

注意:我们这里要启动的是多实例,即confxml.xml和download_confxml.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[root@SERSYNC ~] # ls /usr/local/sersync/conf/*
/usr/local/sersync/conf/confxml .xml
/usr/local/sersync/conf/confxml .xml.bak.2014-06-04
/usr/local/sersync/conf/download_confxml .xml
[root@SERSYNC ~] # sersync -r -d -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: -r  rsync  all the  local  files to the remote servers before the sersync work
option: -d run as a daemon
option: -o config xml name:   /usr/local/sersync/conf/confxml .xml
daemon thread num: 10
parse xml config  file
host ip : localhosthost port: 8008
daemon start,sersync run behind the console 
use  rsync  password- file  :
user isrsync_bak
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
chmod : cannot access ` /usr/local/logs/rsync_fail_log .sh': No such  file  or directory
------------------------------------------
rsync  the directory recursivly to the remote servers once
working please wait...
execute  command cd  /data/web  &&  rsync  -aruz -R --delete ./  --timeout=100 rsync_bak@172.16.100.1::web --password- file = /etc/rsync .password > /dev/null  2>&1 
run the sersync: 
watch  path is:  /data/web
[root@SERSYNC ~] # sersync -r -d -o /usr/local/sersync/conf/download_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: -r  rsync  all the  local  files to the remote servers before the sersync work
option: -d run as a daemon
option: -o config xml name:   /usr/local/sersync/conf/download_confxml .xml
daemon thread num: 10
parse xml config  file
host ip : localhosthost port: 8008
daemon start,sersync run behind the console 
use  rsync  password- file  :
user isrsync_bak
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
chmod : cannot access ` /usr/local/logs/rsync_fail_log .sh': No such  file  or directory
------------------------------------------
rsync  the directory recursivly to the remote servers once
working please wait...
execute  command cd  /data/download  &&  rsync  -aruz -R --delete ./  --timeout=100 rsync_bak@172.16.100.1::download --password- file = /etc/rsync .password > /dev/null  2>&1 
run the sersync: 
watch  path is:  /data/download


b、开启后查看进程

1
2
3
4
[root@SERSYNC ~] # ps -ef |grep sersync
root      2114     1  0 01:56 ?        00:00:00 sersync -r -d -o  /usr/local/sersync/conf/confxml .xml
root      2223     1  0 02:03 ?        00:00:00 sersync -r -d -o  /usr/local/sersync/conf/download_confxml .xml
root      2295  2244  0 02:08 pts /2     00:00:00  grep  sersync


C、配置开机自启动

[root@SERSYNC ~]# cp /etc/rc.local /etc/rc.local.bak._$(date +%F)

1
2
3
4
5
[root@SERSYNC ~] # cat >>/etc/rc.local<< 'EOF'
# sync data to 172.16.100.1,172.16.100.2
> sersync -d -o  /usr/local/sersync/conf/confxml .xml
> sersync -d -o  /usr/local/sersync/conf/download_confxml .xml
> EOF


6、测试 Sersync 是否正常

我们在 Sersync 服务端批量创建文件,然后去SWEB1和SWEB2上查看同步效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@SERSYNC web] # for i in {1..10000};do echo 123456 > /data/web/$i &>/dev/null;done
[root@SERSYNC web] # for i in {1..10000};do echo 123456 > /data/download/$i &>/dev/null;done
[root@SERSYNC web] # tree /data/
/data/
├── download
│   ├── 1
......   #中间信息省略
......   #中间信息省略
     ├── 9997
     ├── 9998
     ├── 9999
     └── index.html
2 directories, 20001 files

 

以上,我们是写入了20000个文件,然后批量测试同步效率:

这种情况下,同步情况不是很令人乐观

此时,我们可以通过ps -ef |grep rsync|wc -l来查看Sersync的服务端 Rsync的进程数,最好多次统计,你会发现这个进程是动态变化的!


1
2
3
4
5
6
7
8
9
10
[root@SERSYNC ~] # ps -ef |grep rsync|wc -l
52
[root@SERSYNC ~] # ps -ef |grep rsync|wc -l
63
[root@SERSYNC ~] # ps -ef |grep rsync|wc -l
20
[root@SERSYNC ~] # ps -ef |grep rsync|wc -l
83
[root@SERSYNC ~] # ps -ef |grep rsync|wc -l
83

此时,我们在去SWEB1和SWEB2上tree或者ls下,/data/web和/data/download的同步情况!

需要注意:生产中,写的越快,并发的进程也就越多!当写的过多的时候,也是没法实现实时同步,因此这个也是受写入数据量大小影响的。

在生产环境中,一般来说以 DELL R710 服务器为主,在千兆网卡情况下,每秒钟同步 10-100k 的文件能达到同步 40 到 50 个。


以上就是我们的 Rsync + Sersync 的架构实验!


后面,我就补充一些说明信息。


一、Sersync参数说明

Sersync参数

说明

./sersync  -r

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

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

./sersync  –o old.xml

不指定-o参数时,sersync会使用sersync可执行文件目录下的默认配置文件confxml.xml,如果需要使用其他的配置文件,可以使用-o参数制定其它配置文件,通过-o参数,我们可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步,本文的例子就是如此。

./sersync  –n num

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

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

./sersync  –d

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

./sersync  –m plugnName

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

./sersync  –m pluginName

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

-n  8 –o sun.xml –r –d

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

通常情况下,首先要对本地到远程整体同步一遍之后,再在后台运行实时同步操作!

 

 

二、Sersync配置文件说明

Sersync 的可选功能是通过 xml 配置文件来实现的,下面来为大家解释该文件!

在该文件中,使用<!--中间是注释内容-->的方式去注释信息,注意不是我们平时#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
[root@SERSYNC sersync] # cat conf/confxml.xml -n
      1<?xml version= "1.0"  encoding= "ISO-8859-1" ?>
      2< head  version= "2.5" >
      3    <host hostip= "localhost"  port= "8008" >< /host >    #hostip与port是针对插件的保留字段,对于同步功能没有任何作用,保留默认即可。
      4    <debug start= "false" />    #该行为Debug开启开关。true为开启debug模式,会在sersync正在运行的控制台,打印 inotify 事件与 rsync 同步命令,生产环境一般不开启。
      5    <fileSystem xfs= "false" /> #对于XFS文件系统的用户,需要将这个选项开启,才能使sersync正常工作
      对于sersync监控的文件,会默认过滤系统的临时文件(以“.”开头,以“~”结尾),除了这些文件外,在6-11行中,我们还可以自定义其它需要过滤的文件。
      通过将 start 设置为  true  后可开启过滤功能,在exclude标签中可使用正则表达式。默认给出的两个例子分别是过滤以“.gz”结尾的文件与过滤监控目录下的info路径(监控路径 /info/ *),可以根据需求自己添加。但在开启的时候,自己一定要测试下,如果正则表达式出现错误,控制台会有相应提示。相比较使用 Rsync 的 exclude 功能,被过滤的路径,不会加入监控,大大减少 Rsync 同步的通讯量
      6    <filter start= "false" >
      7<exclude expression= "(.*)\.svn" >< /exclude >
      8<exclude expression= "(.*)\.gz" >< /exclude >
      9<exclude expression= "^info/*" >< /exclude >
     10<exclude expression= "^static/*" >< /exclude >
     11    < /filter >
第12-23行用来定义 inotify 监控参数,我们可以根据项目的特点来优化 Sersync。
对于大多数应用,可以尝试把 createFile(监控文件事件选项)设置为 false 来提高性能,进一步减少 Rsync通讯。因为拷贝文件到监控目录会产生 create 事件与 close_write 事件,所以如果关闭create 事件,只监控文件拷贝结束时的事件 close_write,同样可以实现文件完整同步。
注意:要使得 createFolder 保持为 true ,如果将createFolder设为 false ,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控,所以除非特殊需要,请开启。默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将 delete 参数设置为  false ,则不对删除事件进行监控。
     12    <inotify>
     13<delete start= "true" />
     14<createFolder start= "true" />
     15<createFile start= "false" />
     16<closeWrite start= "true" />
     17<moveFrom start= "true" />
     18<moveTo start= "true" />
     19<attrib start= "false" />
     20<modify start= "false" />
     21    < /inotify >
     22
     23    <sersync>
第24-28行用来定义所要同步和监控的目录文件。
/opt/tongbu 目录为sersync主服务器本地待同步的目录,ip=“192.168.8.39”为从服务器的ip地址,如果有多个服务器,依次列出来即可。name=“tongbu”,这里的tongbu为rsyncd.conf中的模块名字,即中括号中的名称。
     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 >
第29-35行用来定义 rsync 的命令参数
在 commonParams 项,我们可以自定义 rsync 的同步参数,默认是-artuz,auth start=“ false ”设置为 true 的时候,使用 rsync 的认证模式传送,需要配置user与passwordfile(-password- file = /etc/rsync .pas)来使用。userDefinedPort 当远程同步目标服务器的 rsync 端口不是默认端口的时候使用(-port=874)。timeout设置 rsync 的timeout事件(-timeout=100)。< ssh  start=” false ”/>如果开启表示 ssh 使用 rsync  -e  ssh 的方式进行传输。
     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 >
第36行用来定义失败日志脚本配置
如果文件同步传输失败,会重新传送,再次失败就会写入 rsync_fail_log.sh,然后每隔一段时间(timeToExecute进行设置)执行该脚本再次重新传送,然后清空该脚本。可以通过path来设置日志路径
     36<failLog path= "/tmp/rsync_fail_log.sh"  timeToExecute= "60" /><!--default every 60mins execute once-->
第37-42行用来定义Crontab定期整体同步功能
Crontab可以对监控路径与远程目标主机每隔一段时间进行一次整体同步,可能由于一些原因两次失败重传都失败了,这个时候如果开启了  crontab  功能,还可以进行一次保证各个服务器文件一致,如果文件量比较大, crontab 的时间间隔要设的大一些,否则可能增加通讯开销,schedule这个参数是设置 crontab 的时间间隔,默认是600分钟。
如果开启了 filter 文件过滤功能,那么 crontab 整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但  crontab  整体同步的时候,如果不单独设置crontabfilter,还会将需过滤的文件同步到远程从服务器, crontab 的过滤正则与filter过滤的不同,也给出了两个实例分别对应与过滤文件与目录,总之如果同时开启了filter与 crontab ,则要开启 crontab 的crontabfilter,并按示例设置使其与filter的过滤一一对应。
     37< crontab  start= "false"  schedule= "600" ><!--600mins-->
     38    <crontabfilter start= "false" >
     39<exclude expression= "*.php" >< /exclude >
     40<exclude expression= "info/*" >< /exclude >
     41    < /crontabfilter >
     42< /crontab >
     43<plugin start= "false"  name= "command" />
     44    < /sersync >
     45
从46行到行尾,都是插件的相关信息。当plugin标签设置为 true 时候,在同步文件或路径到远程服务器之后,会调用插件。通过name参数指定需要执行的插件。目前支持的有 command 、refreshCDN、socket、http四种插件。其中,http插件目前由于兼容性原因已经去除,以后会重新加入。
     46    <plugin name= "command" >
     47<param prefix= "/bin/sh"  suffix= ""  ignoreError= "true" /><!--prefix  /opt/tongbu/mmm .sh suffix-->
     48<filter start= "false" >
     49    <include expression= "(.*)\.php" />
     50    <include expression= "(.*)\.sh" />
     51< /filter >
     52    < /plugin >
     53
第54-58行为插件socket的相关配置
socket插件,开启该模块,则向指定ip与端口发送inotify所产生的文件路径信息
     54    <plugin name= "socket" >
     55<localpath  watch = "/opt/tongbu" >
     56    <deshost ip= "192.168.138.20"  port= "8009" />
     57< /localpath >
     58    < /plugin >
第59-65行为插件refreshCDN的相关配置
refreshCDN 用来在同步过程中将文件发送到目地服务器后,刷新cdn接口。如果不想使用,则将start属性设为 false 即可。该模块根据chinaCDN的协议,进行设计,当有文件产生的时候,就向cdn解耦发送需要刷新的路径为止。其中localpath  watch =“ /data0/htdocs/cms .xoyo.com /site/ ”是需要监控的目录。cdinfo标签指定了cdn接口的域名,端口号,以及用户名与密码。sendurl 标签是需要刷新的url的前缀。regexurl 标签中,regex属性为 true 时候,使用match属性的正则语句匹配inotify返回的路径信息,并将正则匹配到的部分作为url一部分
下面配置文件自带的意思为,如果产生文件事件为: /data0/htdoc/cms .xoyo.com /site/jx3 .xoyo.com /image/a/123 .txt
经过上面的match正则匹配后,最后刷新的路径是:http: //pic .xoyo.com /cms/a/123 .txt
如果regex属性为 false ,最后刷新的路径就是:http: //pic .xoyo.com /cms/jx3 .xoyo.com /images/a/123 .txt
     59    <plugin name= "refreshCDN" >
     60<localpath  watch = "/data0/htdocs/cms.xoyo.com/site/" >
     61    <cdninfo domainname= "ccms.chinacache.com"  port= "80"  username= "xxxx"  passwd = "xxxx" />
     62    <sendurl base= "http://pic.xoyo.com/cms" />
     63    <regexurl regex= "false"  match= "cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images" />
     64< /localpath >
     65    < /plugin >
     66< /head >


这里列举,如何单独去运行插件

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

只调用command插件

1
[root@SERSYNC ~] # sersync -d -m command


只调用refreshCDS插件

1
[root@SERSYNC ~] # sersync -d -m refreshCDN


只调用socket插件

1
[root@SERSYNC ~] # sersync -d -m socket


只调用http插件

1
[root@SERSYNC ~] # sersync -d -m http


OK,本文就到这里,希望能对广大51博友有所帮助!











本文转自 aaao 51CTO博客,原文链接:http://blog.51cto.com/nolinux/1433623,如需转载请自行联系原作者

目录
相关文章
|
存储 监控 安全
|
监控 测试技术 Linux
rsync+inotify实时同步案例
rsync+inotify实时同步案例 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。
1241 0
|
监控 JavaScript 测试技术
rsync+inotify实现实时同步(小业务场景解决方案)
一、rsync功能介绍 rsync同步操作• 命令用法– rsync [选项...] 源目录 目标目录 • 同步与复制的差异– 复制:完全拷贝源到目标– 同步:增量拷贝,只传输变化过的数据 • 本地同步– rsync [选项.
1495 0
|
网络协议 Shell 应用服务中间件