Linux Debian8 Rsync+Sersync实现数据实时同步

Rsync+sersync实现数据实时同步
Rsync
  Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。Rsync使用所谓的“Rsync算法”来使本地和远 程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快。
Rsync的基本特点如下:
1.可以镜像保存整个目录树和文件系统;
2.可以很容易做到保持原来文件的权限、时间、软硬链接等;
3.无须特殊权限即可安装;
4.优化的流程,文件传输效率高;
5.可以使用rsh、ssh等方式来传输文件,当然也可以通过直接的socket连接;
6.支持匿名传输。
inotify
  Inotify是一种文件变化通知机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加,删除,修改,移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施这样监控的软件。 
inotify 的实现有几款软件 
注意:大前提rsync daemon 服务配置成功,可以在rsync客户端推送拉取数据,然后才能配置inotify服务。 

  • 1)inotify-tools, 
  • 2)sersync(金山周洋) 
  • 3)lsyncd
    Rsync+inotify组合的起源
      Rsync(remote sync)远程同步工具,通过rsync可以实现对远程服务器数据的增量备份同步,但rsync自身也有瓶颈,同步数据时,rsync采用核心算法对远程服务器的目标文件进行比对,只进行差异同步。我们可以想象一下,如果服务器的文件数量达到了百万甚至千万量级,那么文件对比将是非常耗时的。而且发生变化的往往是其中很少的一部分,这是非常低效的方式。inotify的出现,可以缓解rsync不足之处,取长补短
    为什么要用Rsync+sersync架构
    Rsync+Inotify-tools 
    (1)Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来; 
    (2)rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。 
    Rsync+sersync 
    (1)sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字; 
    (2)rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。
    小结: 
      当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。 

Rsync+sersync项目实战
服务器角色 IP 服务 配置
源服务器 192.168.2.1 sersync 64位debian
目标服务器 192.168.2.2 rsync 64位debian

一.在目标服务器部署rsync服务
前提:两台服务器的防火墙出入规则都要放开873端口
root@VM-139-168-debian:~# apt-get install rsync -y
root@VM-139-168-debian:~# cd /etc/
root@VM-139-168-debian:/etc# vim rsyncd.conf #创建配置文件
uid = root
gid = root
max connections = 600
timeout = 200
use chroot = no
log file = /var/log/rsyncd.log

[rsync] #模块名
path = /data/www #配置共享的目录
auth users = rsync #配置虚拟用户
ignore errors = yes
read only = no
hosts allow =
secrets file = /etc/rsync.password #配置虚拟用户密码存放文件
root@VM-139-168-debian:/etc# vim rsync.password
rsync:rsync #虚拟用户名和密码
root@VM-139-168-debian:/etc# chmod 600 rsyncd.conf
root@VM-139-168-debian:/etc# chmod 600 rsync.password
root@VM-139-168-debian:~# rsync --daemon #启动rsync服务
root@VM-139-168-debian:~# ps -ef|grep rsync
root 964 411 0 16:08 pts/0 00:00:00 grep rsync
root 11141 1 0 Sep14 ? 00:00:01 rsync --daemon
root@VM-139-168-debian:~# netstat -anpt|grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:
LISTEN 11141/rsync
tcp6 0 0 :::873 :::* LISTEN 11141/rsync
二.手动同步测试
在源服务器部署rsync,但不创建配置文件。
weipengcheng@5b-conchZ:~$ sudo apt-get install rsync -y
weipengcheng@5b-conchZ:~$ cd /etc/
weipengcheng@5b-conchZ:/etc$ sudo vim password.txt
rsync #虚拟用户密码
由于源服务器用户为普通用户,所以一定将password.txt文件属主属组修改为当前的普通用户,不然后续做免密测试连接会报错无法读取password.txt文件。
weipengcheng@5b-conchZ:/etc$ sudo chown weipengcheng:weipengcheng password.txt
weipengcheng@5b-conchZ:/etc$ sudo chmod 600 password.txt
weipengcheng@5b-conchZ:~$ rsync -avz /data/www/ [email protected]::rsync --password-file=/etc/password.txt #此命令将本地/data/www目录下文件同步到目标服务器下,第一个rsync为虚拟用户,第二个rsync为模块名。
三.源服务器部署sersync服务
weipengcheng@5b-conchZ:~$ wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
weipengcheng@5b-conchZ:~$ tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/src
weipengcheng@5b-conchZ:~$ cd /usr/local/src/
weipengcheng@5b-conchZ:~$ mv GNU-Linux-x86 /usr/local/sersync
weipengcheng@5b-conchZ:/usr/local/src$ cd ../sersync/
weipengcheng@5b-conchZ:/usr/local/sersync$ ls
confxml.xml sersync2
weipengcheng@5b-conchZ:/usr/local/sersync$ sudo mkdir conf bin log
weipengcheng@5b-conchZ:/usr/local/sersync$ mv confxml.xml conf/
weipengcheng@5b-conchZ:/usr/local/sersync$ mv sersync2 bin/sersync
weipengcheng@5b-conchZ:/usr/local/sersync$ cd conf/
weipengcheng@5b-conchZ:/usr/local/sersync$ sudo cp confxml.xml confxml.xml.bak
weipengcheng@5b-conchZ:/usr/local/sersync$ sudo vim confxml.xml
23 <sersync>
24 <localpath watch="/data/www"> #这里是rsync服务器要同步的目录
25 <remote ip="192.168.2.2" name="rsync"/> #rsync_client的IP地址和模块名,有多个客户端便写多行
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="true" users="rsync" passwordfile="/etc/password.txt"/> #开启认证,填写用户名和密码文件
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="false" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
weipengcheng@5b-conchZ:~$ sudo echo "export PATH=$PATH:/usr/local/sersync/bin" >>/etc/profile
weipengcheng@5b-conchZ:~$ source /etc/profile
weipengcheng@5b-conchZ:~$ sersync -r -d -o /usr/local/sersync/conf/confxml.xml #启动sersync服务
-o:指定配置文件  
-d:在后台启动 
-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
-n:指定默认的线程池的线程总数,如果不指定,默认启动线程池数量是10(适用于四核服务器)
#如果cpu使用过高,可以通过这个参数调低,如果机器配置较高,可以用-n调高线程总数。
 
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 : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is rsync
passwordfile is /etc/password.txt
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 /data/www && rsync -artuz -R --delete ./ [email protected]::rsync --password-file=/etc/password.txt >/dev/null 2>&1
run the sersync:
watch path is: /data/www
weipengcheng@5b-conchZ:~$ ps -ef|grep sersync
994 12106 1 0 Sep14 ? 00:01:20 sersync -r -d -o /usr/local/sersync/conf/confxml.xml
994 21711 17980 0 17:24 pts/5 00:00:00 grep sersync
四.实时同步测试
在源服务器/data/www目录下创建一个空文件。
weipengcheng@5b-conchZ:/data/www$ sudo touch test.txt
查看目标服务器/data/www目录下是否同步过去。
root@VM-139-168-debian:/data/www# ll
total 0
-rw-r--r-- 1 root root 0 Sep 17 17:32 test.txt

猜你喜欢

转载自blog.51cto.com/13531948/2176268