一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Sersync实现数据同步的配置教程

时间:2015-12-03 编辑:简简单单 来源:一聚教程网

  Sersync 基于 boost1.43.0,inotify api,rsync command 开发,主要用于服务器同步,web镜像等功能。软件作者:周洋。项目官网;https://code.google.com/p/sersync/

  Sersync 优点:

  1、sersync 是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,所以在结合 rsync 同步的时候,节省了运行时耗和网络资源。因此更快。

  2、相比较 inotify-tools + rsync 和 Openduckbill 两个项目,sersync配置起来很简单,其中bin目录下已经有基本上静态编译的2进制文件,配合 bin 目录下的 xml 配置文件直接使用即可。

  3、另外 sersync 相比较其他脚本开源项目,使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。

  4、sersync 有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对同步失败的文件重新同步。

  5、sersync 自带 crontab 功能,只需在xml配置文件中开启,即可按您的要求,隔一段时间整体同步一次。无需再额外配置crontab功能。

  6、sersync socket 与 http 插件扩展,满足二次开发的需要。

  可见 sersync + rsync 要比 inotify-tools + rsync 数据同步效率高。(文件更新频率和数量在一定范围内)

  一、规划:

  数据同步源: nfs-s1 IP:172.16.1.10 目录:/data/images

  数据同步目的:nfs-s2 IP:172.16.1.20 目录:/data/images

  二、配置步骤:

  1、在 nfs-s2 配置 rsync daemon ,并启动。nfs-s1 配置 rsync client。

  2、在 nfs-s1 配置 sersync 。

  三、nfs-s2 配置 rsync daemon :(注意目录对于相应其他程序的读写权限)

[root@nfs-s2 ~]# ifconfig eth0|awk -F '[ :]+' 'NR==2{print $4}'
172.16.1.20
[root@nfs-s2 ~]# uname -r
2.6.32-504.el6.x86_64
[root@nfs-s2 ~]#

[root@nfs-s2 ~]# useradd -s /sbin/nologin -M www
[root@nfs-s2 ~]# mkdir /data/images -p
[root@nfs-s2 ~]# chown -R www.www /data/images/

  编辑 /etc/rsyncd.conf

[root@nfs-s2 ~]# cat /etc/rsyncd.conf
uid=www
gid=www
use chroot=true
read only=false
list=false
ignore errors=true
hosts allow=172.16.1.0/24
hosts deny=0.0.0.0/0
max connections=1000
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/log/rsyncd.log
auth users=sersync
secrets file=/etc/rsync.passwd
[images]
comment=images from nfs-s1
path=/data/images
[root@nfs-s2 ~]#

  编辑 rsync daemon 用户密码文件 :

[root@nfs-s2 ~]# echo 'sersync:123456'>/etc/rsync.passwd
[root@nfs-s2 ~]# cat /etc/rsync.passwd
sersync:123456
[root@nfs-s2 ~]# chmod 700 /etc/rsync.passwd

  启动 rsync daemon :

[root@nfs-s2 ~]# rsync --daemon

  在 nfs-s1 创建 rsync daemon 密码文件 :

[root@nfs-s1 ~]# ifconfig eth0|awk -F '[ :]+' 'NR==2{print $4}'
172.16.1.10
[root@nfs-s1 ~]# uname -r
2.6.32-504.el6.x86_64
[root@nfs-s1 ~]#

[root@nfs-s1 ~]# echo '123456'>/etc/rsync.passwd
[root@nfs-s1 ~]# chmod 700 /etc/rsync.passwd
[root@nfs-s1 ~]#

  测试 :

[root@nfs-s1 ~]# echo 'test rsync daemon from nfs-s1'>rsync.test
[root@nfs-s1 ~]# rsync -avz rsync.test sersync@172.16.1.20::images --password-file /etc/rsync.passwd
sending incremental file list
rsync.test

sent 68 bytes  received 33 bytes  40.40 bytes/sec
total size is 30  speedup is 0.30
[root@nfs-s1 ~]#

  rsync daemon 配置成功。

[root@nfs-s2 ~]# echo 'rsync --daemon'>>/etc/rc.local
[root@nfs-s2 ~]# tail -1 /etc/rc.local
rsync --daemon
[root@nfs-s2 ~]#


  四、在 nfs-s1 安装配置 sersync :(目录权限根据实际情况设置)

[root@nfs-s1 ~]# useradd -s /sbin/nologin -M www
[root@nfs-s1 ~]# mkdir -p /data/images
[root@nfs-s1 ~]# chown -R www.www /data/images/
[root@nfs-s1 ~]#

  下载 sersync :

[root@nfs-s1 src]# wget -q http://www.111com.net/repos/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs-s1 src]# ls
sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs-s1 src]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs-s1 src]# ls
GNU-Linux-x86  sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs-s1 src]#

[root@nfs-s1 src]# mv GNU-Linux-x86 /usr/local/sersync
[root@nfs-s1 src]# cd /usr/local/sersync/
[root@nfs-s1 sersync]# ls
confxml.xml  sersync2
[root@nfs-s1 sersync]# mkdir {bin,conf,log}
[root@nfs-s1 sersync]# ls
bin  conf  confxml.xml  log  sersync2
[root@nfs-s1 sersync]# mv confxml.xml conf
[root@nfs-s1 sersync]# mv sersync2 bin/sersync
[root@nfs-s1 sersync]# tree .
.
├── bin
│   └── sersync
├── conf
│   └── confxml.xml
└── log

3 directories, 2 files
[root@nfs-s1 sersync]#

  编辑 confxml.xml :(可根据实际需求进行细粒度的配置)

[root@nfs-s1 sersync]# cat conf/confxml.xml


   
   
   
   
       
   
   
       
       
       
       
       
       
       
       
   
   
       
            remote name="images" ip="172.16.1.20" />
       
       
           
           
           
           
           
       
       
       
           
               
           
       
        
   

[root@nfs-s1 sersync]#

  启动 sersync :

[root@nfs-s1 sersync]# /usr/local/sersync/bin/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 : localhost     host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is sersync
passwordfile is         /etc/rsync.passwd
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/images && rsync -artuz -R --delete ./ sersync@172.16.1.20::images --password-file=/etc/rsync.passwd >/dev/null 2>&1
run the sersync:
watch path is: /data/images
[root@nfs-s1 sersync]#

  测试 :

[root@nfs-s1 sersync]# touch /data/images/{1..100}.png

[root@nfs-s2 ~]# ls /data/images/
100.png  19.png  28.png  37.png  46.png  55.png  64.png  73.png  82.png  91.png
10.png   1.png   29.png  38.png  47.png  56.png  65.png  74.png  83.png  92.png
11.png   20.png  2.png   39.png  48.png  57.png  66.png  75.png  84.png  93.png
12.png   21.png  30.png  3.png   49.png  58.png  67.png  76.png  85.png  94.png
13.png   22.png  31.png  40.png  4.png   59.png  68.png  77.png  86.png  95.png
14.png   23.png  32.png  41.png  50.png  5.png   69.png  78.png  87.png  96.png
15.png   24.png  33.png  42.png  51.png  60.png  6.png   79.png  88.png  97.png
16.png   25.png  34.png  43.png  52.png  61.png  70.png  7.png   89.png  98.png
17.png   26.png  35.png  44.png  53.png  62.png  71.png  80.png  8.png   99.png
18.png   27.png  36.png  45.png  54.png  63.png  72.png  81.png  90.png  9.png
[root@nfs-s2 ~]#

[root@nfs-s1 sersync]# rm -f /data/images/*.png
[root@nfs-s1 sersync]# mkdir /data/images/{png,jpeg,jpg}
[root@nfs-s1 sersync]#

[root@nfs-s2 ~]# ls /data/images/
jpeg  jpg  png
[root@nfs-s2 ~]#

  测试成功

[root@nfs-s1 sersync]# echo '/usr/local/sersync/bin/sersync -d -o /usr/local/sersync/conf/confxml.xml'>>/etc/rc.local   
[root@nfs-s1 sersync]# tail -1 /etc/rc.local
/usr/local/sersync/bin/sersync -d -o /usr/local/sersync/conf/confxml.xml
[root@nfs-s1 sersync]#

 

热门栏目