linux运维笔记:sersync实现数据实时同步

linux运维笔记:sersync实现数据实时同步

1-Sersync简介

Sersync是利用 inotify 技术实现所有服务器数据实时同步的解决方案,其中监控 sersync 所在服务器上文件系统的事件变化,rsync 是目前广泛使用的本地及异地数据同步工具,其优点是只对发生变化的目录数据操作,甚至是一个文件不同的部分进行同步,所以其优势大大超过使用挂接文件系统或scp 等方式进行镜像同步。

目前使用比较多的同步程序版本是 inotify-tools,另外一个是 Google 开源项目 openduckbill(依赖于 inotify-tools),这两个都是基于脚本语言编写的,其设计思路同样是采用 inotify + rsync 命令。

2-sersync优点

1.使用 C++编写,对 linux 系统文件产生的临时文件和重复的文件操作会进行过滤,在结合rsync同步的时候,会减少运行时消耗的本地及网络资源,因此速度更快。

2.相比其他项目,sersync 配置起来简单:在 http://code.google.com/p/sersync/downloads/list 处下载源码(分为 32 位版本和 64 位版本),其中 bin 目录下是已经编译好的二进制文件,配合 bin目录下的 xml 文件直接使用即可。

3.使用多线程同步(即可以并发同步多个不同文件),尤其在同步较大文件时,能够保证多个服务器实时保持同步状态。

4.Sersync自带出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则每 10 个小时对同步失败的文件再重新同步。

5.Sersync自带crontab功能,只需在xml 配置文件中开启,即可按预先的配置,隔一段时间整体同步一次。

6.Sersync自带socket与http的协议扩展,可以满足有特殊需求的公司二次开发。

3-环境准备:

主机backup,配置好rsync服务端

主机nfs01,配置好rsync客户端

sersync安装在nfs01

4-软件包上传到/usr/local/下

[root@nfs01 ~]# cd /usr/local/sersync/bin/

[root@nfs01 bin]# tree /usr/local/sersync/

/usr/local/sersync/

├── bin

│   └── sersync

├── conf

│   ├── confxml.xml

│   └── confxml.xml.ori

└── logs

5-编辑配置文件

[root@nfs01 bin]# vim /usr/local/sersync/conf/confxml.xml

6-11行表示排除同步的数据,等价于 --exclude 功能,表示排除

<filter start="false">

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

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

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

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

</filter>

12-21行是利用inotify的功能监控指定的事件,等价于 -e create,delete…… 表示指定监控事件信息

 <inotify>

     <delete start="true"/>

     <createFolder start="true"/>

     <createFile start="false"/>

     <closeWrite start="true"/>

     <moveFrom start="true"/>

     <moveTo start="true"/>

     <attrib start="false"/>

     <modify start="false"/>

 </inotify>

24-28行:推送到哪里 ,name=模块,ip是rsync服务器地址

 <localpath watch="/ning"> #监控nfs01客户端目录

     <remote ip="172.16.1.41" name="backup"/> #服务端backup的共享目录

     <!--<remote ip="192.168.8.39" name="tongbu"/>-->

     <!--<remote ip="192.168.8.40" name="tongbu"/>-->

 </localpath>

29-35行 定义rsync推送时的参数信息,注意:不要有单词拼写错误 (true),否则程序不能正常启动,卡死

<rsync>

    <commonParams params="-az"/>

    <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>

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

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

<ssh start="false"/>

</rsync>

6-启动sersync

[root@nfs01 bin]# chmod a+x sersync

[root@nfs01 bin]# ll

total 1768

-rwxr-xr-x. 1 root root 1810128 Oct 26  2011 sersync

[root@nfs01 bin]# ./sersync -h

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

_______________________________________________________

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

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

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

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

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

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

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

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

[root@nfs01 bin]# ./sersync -dro /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: -d run as a daemon

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

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_backup

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: 22 = 12(Thread pool nums) + 10(Sub threads)

please according your cpu ,use -n param to adjust the cpu rate

chmod: cannot access `/application/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 /ning && rsync -avz -R --delete ./ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password >/dev/null 2>&1

run the sersync:

watch path is: /ning

7-测试

[root@nfs01 bin]# touch /ning/ccc.png

[root@nfs01 bin]# ls /ning/

111.txt  aaa.jpg  bbb.jpg  ccc.png

[root@backup ~]# ls /backup/

111.txt  aaa.jpg  bbb.jpg  ccc.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好知识传播者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值