sersync(客户端进程监 控指定目录更新事件的变化,触发式把变化数据推送到服务端)
rsync服务器: [email protected]
sersync2服务器 rsync客户端: [email protected]
 
#建立 rsync 服务器的配置文件/etc/rsyncd.conf
[root@sukaka ~]# cat /etc/rsyncd.conf
uid=root                                             #服务器端传输文件时,要发哪个用户和用户组来执行,默认是nobody。 如果用nobody 用户和用户组,可能遇到权限问题,有些文件从服务器上拉不下来。为了方便,用了root 。不过可以在定义要同步的目录时定义的模块中指定用户来解决权限的问题。
gid=root
max connections=100
use chroot=no                                        #用chroot,在传输文件之前,服务器守护程序在将chroot 到文件系统中的目录中,这样做的好处是可能保护系统被安装漏洞侵袭的可能。缺点是需要超级用户权限。另外对符号链接文件,将会排除在外。也就是说,你在rsync服务器上,如果有符号链接,你在备份服务器上运行客户端的同步数据时,只会把符号链接名同步下来,并不会同步符号链接的内容;这个需要自己来尝试。
log file=/var/log/rsyncd.log                         #日志
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
motd file=/etc/rsyncd.motd                           #欢迎文件位置
#transfer log
transfer logging = yes                               #启用传输日志
log format = %t %a %m %f %b                          #传输日志的格式
syslog facility = local3                             #日志等级
timeout = 300                                        #超时时间
[share]                                              #模块名称
path=/root                                           #需要同步的目录
comment = my htdocs                                  #备注
secrets file = /etc/rsyncd.pass                      #安全文件,格式用户名:密码不要多加任何字符 权限600
ignore errors                       
read only = no                                       #只读
hosts allow=10.0.0.0/24,192.168.68.0/24              #允许的IP范围
 
 

 

[root@sukaka ~]# cat /etc/rsyncd.pass    #rsync服务器  设置格式user:password不要有多余字符
root:test                
[root@sukaka1 ~]# cat /etc/rsync.pass     #客户端只需要有密码 现在的客户端就是sersync2的服务端
test
#启动 rsync服务
[root@sukaka ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@sukaka ~]# netstat -anlp |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2584/xinetd
tcp 0 0 :::873 :::* LISTEN 2784/rsync
 
[root@sukaka1 ~]# wget http://sersync.googlecode.com/files/sersync2.5_32bit_binary_stable_final.tar.gz     下载sersync2
[root@sukaka1 test]# ls
sersync.tar.gz
[root@sukaka1 test]# tar zxf sersync2.5_32bit_binary_stable_final.tar.gz                解压
[root@sukaka1 test]# ls
GNU-Linux-x86 sersync.tar.gz serysnc svndata
[root@sukaka1 test]# cd GNU-Linux-x86/
[root@sukaka1 GNU-Linux-x86]# ls
confxml.xml sersync2
[root@sukaka1 GNU-Linux-x86]# mkdir /etc/sersync2 -p                                      创建serync2的目录
[root@sukaka1 GNU-Linux-x86]# cp confxml.xml /etc/sersync2/
[root@sukaka1 GNU-Linux-x86]# cp sersync2 /usr/bin/
[root@sukaka1 GNU-Linux-x86]# cat /etc/sersync2/confxml.xml
 
#本地需要同步的目录
#远程同步的地址和同步的模块
#日志文件
 
 
 
[root@sukaka1 GNU-Linux-x86]# /usr/bin/sersync2 -r -d -o confxml.xml      启动sersync2 -rdo参数下面有具体的解释
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 confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start, sersync run behind the console
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu , use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /root && rsync -artuz -R --delete ./ 192.168.68.212::zhanglei >/dev/null 2>&1
run the sersync:
watch path is: /root
[root@sukaka1 GNU-Linux-x86]# cd ~
[root@sukaka1 ~]# ls
test
[root@sukaka ~]# ls                                           #远程rsync服务器
[root@sukaka1 ~]# touch 1 2 3 4 5 6 7 8            #回到sersync2服务器创建文件
[root@sukaka1 ~]# ls
1 2 3 4 5 6 7 8 test
[root@sukaka ~]# ls                                              #这时可以看到远程rsync服务器目录下也有了同样的文件,说明时 时同步成功
1 2 3 4 5 6 7 8
 
sersync2完全安装配置说明
    当前版本的 sersync依赖于rsync进行同步。如下图所示,在同步主服务器上开启sersync,将监控路径中的文件同步到目标服务器,因此需要在主服务器配置sersync,在同步目标服务器配置rsync。对于rsync配置,在google上可以找到很多资料,以下只是一些必要的配置和使用说明,仅供参考,请根据您的实际情况修改。
    如图所示,需要在同步主服务器上配置 sersync,在同步目标服务器配置rsync,并在目标服务器开启rsync守候进程,这样在主服务器产生的文 件,就会被sersync实时同步到多个目标服务器。在centos系统下默认已经安装了rsync,只需进行配置,并开启rsync守候进程即可。
 
配置同步目标服务器 rsync
在多台目标服务器上配置如下:
vi /etc/rsyncd.conf
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[tongbu1]
path=/opt/tongbu1
comment = xoyo video files
ignore errors = yes
read only = no
hosts allow = 192.168.0.100/24
hosts deny = *
    上面配置文件,表明允许主服务器(假设 ip为192.168.0.100)访问,rsync同步模块名为[tongbu1],将同步过来的文件放入path指定的目录/opt/tongbu1。如果有多台从服务器,则每一台都需要进行类似的rsync配置,上面的uid gid要换成您服务器的相应用户,主意rysnc要有对被同步目录的操作权限。配置好之后,使用如下命令,开启rsync守护进程:
rsync --daemon在主服务器上安装配置 sresync
google code下载sersync的可执行文件版本,里面有配置文件与可执行文件,这里用sersync2.5进行说明,新版本配置形式类似。
 
1.在主服务器上开启 sersync守护进程,使sersync在后台运行,开启实时同步。
./sersync -d
过程如下 :
[root@localhost GNU-Linux-x86]# ls
confxml.xml sersync2
[root@localhost GNU-Linux-x86]# ./sersync2 -d
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
daemon thread num: 10
parse xml config file
host ip : localhost      host port: 8008
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)
please according your cpu , use -n param to adjust the cpu rate
run the sersync:
watch path is: /opt/tongbu
表明, sersync已经开启,可以在本地监控路径下建立文件,查看远程是否同步成功。
 
2.在开启实时监控的之前对主服务器目录与远程目标机目录进行一次整体同步
 
./sersync -r如果需要将 sersync运行前,已经存在的所有文件或目录全部同步到远程,要以-r参数运行sersync,将本地与远程整体同步一次。
如果设置了过滤器,即在 xml文件中,filter为true,则暂时不能使用-r参数进行整体同步。-r参数将会无效。
 
3.查看启动参数帮助
./sersync --help
 
4.指定配置文件
./sersync -o XXXX.xml对于 sersync使用可执行文件目录下的默认配置文件confxml.xml,如果需要使用另一个配置文件,可以使用-o参数指定其它配置文件。
 
5.指定默认的线程池的线程总数
./sersync -n num例如 ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n跳高线程总数。
 
6.不进行同步,只运行插件
./sersync -m pluginName例如 ./sersync -m command,则在监控到文件事件后,不对远程目标服务器进行同步,而是直接运行command插件。
 
7.多个参数可以配合使用
./sersync -n 8 -o abc.xml -r -d表示,设置线程池工作线程为 8个,指定abc.xml作为配置文件,在实时监控前作一次整体同步,以守护进程方式在后台运行。
 
8.通常情况下,对本地到远程整体同步一遍后,在后台运行实时同步。
./sersync -d
 
XML文件说明
sersync可选功能是通过 xml配置文件来实现的,基本配置文件如下:
 
view sourceprint?01  
02  
03        
04        
05            
06            
07        
08        
09            
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      
 
下面做逐行的进行解释说明:
hostip与 port是针对插件的保留字段,对于同步功能没有任何作用,保留默认即可。
 
filter文件过滤功能
对于 sersync监控的文件,会默认过滤系统的临时文件(以“.”开头,以“~”结尾),除了这些文件外,可以自定义其他需要过滤的文件。
 
start设置为 true,在exclude标签中,填写正则表达式,默认给出两个例子分别是过滤以”.gz”结尾的文件与过滤监控目录下的info路径(监控路径/info /*),可以根据需要添加,但开启的时候,自己测试一下,正则表达式如果出现错误,控制台会有提示。相比较使用rsync 的exclude功能,被过滤的路径,不会加入监控,大大减少rsync的通讯量。
 
inotify监控参数设定(优化)
对于 inotify监控参数可以进行设置,根据您项目的特点优化srsync。
 
对于大多数应用,可以尝试把 createFile(监控文件事件选项)设置为false来提高性能,减少 rsync通讯。因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的事件close_write,同样可以实现文件完整同步。
注意:强将 createFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控。所以除非特殊需要,请开启。默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete 参数设置为false,则不对删除事件进行监控。
 
Debug开启
 
设置为 true,开启debug模式,会在sersync正在运行的控制台,打印inotify事件与rsync同步命令。
 
XFS文件系统
 
对于 xfs文件系统的用户,需要将这个选项开启,才能使sersync正常工作.
 
文件监控与远程同步设置
 
详见 sersync2 完全安装配置说明(一) —-基本功能使用
 
Rsync参数配置
 
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的方式进行传输。
失败日志脚步配置
 
 
对于失败的传输,会进行重新传送,再次失败就会写入 rsync_fail_log,然后每隔一段时间(timeToExecute进行设置)执行该脚本再次重新传送,然后清空该脚本。可以通过path来设置日志路径。
 
Crontab定期整体同步功能
 
crontab可以对监控路径与远程目标主机每隔一段时间进行一次整体同步,可能由于一些原因两次失败重传都失败了,这个时候如果开启了 crontab功 能,还可以进一步保证各个服务器文件一致,如果文件量比较大,crontab的时间间隔要设的大一些,否则可能增加通讯开销。schedule这个参数是设置crontab的时间间隔,默认是600分钟
如果开启了 filter文件过滤功能,那么crontab整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但crontab整体同步的时候 如果不单独设置crontabfilter,还会将需过滤的文件同步到远程,crontab的过滤正则与filter过滤的不同,也给出了两个实例分别对 应与过滤文件与目录。总之如果同时开启了filter与crontab,则要开启crontab的crontabfilter,并按示例设置使其与filter的过滤一一对应。
 
插件设置
 
当设置为 true的时候,将文件同步到远程服务器后会调用name参数指定的插件。详见请看插件设置。