sersync实现数据实时同步

1.1 第一个里程碑:安装sersync软件

1.1.1 将软件上传到服务器当中并解压

1、上传软件到服务器上 rz -E

为了便于管理上传位置统一设置为 /server/tools 中

2、解压软件包

[root@backup sersync_installdir_64bit]# tree

.

└── sersync

    ├── bin

    │  └── sersync

    ├── conf

    │  └── confxml.xml

    └── logs

1.1.2 二进制包安装方法

二进制包安装软件方法(绿色软件安装方法):

 直接解压就可以使用

[root@nfs01 sersync_installdir_64bit]# mv sersync/ /usr/local/

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

  /usr/local/sersync/

  ├── bin

  │  └── sersync

  ├── conf

  │  └── confxml.xml

  └── logs


  3 directories, 2 files

1.2 第二个里程碑:编写sersync配置文件

1.2.1 常见的语法格式

rsync 配置文件编写:ini语法

sersync配置文件编写:xml语法

ansible配置文件编写:yml 语法

1.2.2 修改配置文件

编写前备份

[root@backup conf]# ll

total 4

-rw-r--r-- 1 root root 2214 Oct 26  2011 confxml.xml

[root@backup conf]# cp confxml.xml{,.bak}

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=模块 是rsync服务器的地址

<localpath watch="/data"> #监控那个目录

    <remote ip="172.16.1.41" name="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>

配置文件最终内容:


1 [root@nfs01 tools]# cat /usr/local/sersync/conf/confxml.xml

2  <?xml version="1.0" encoding="ISO-8859-1"?>

3  <head version="2.5">

4    <host hostip="localhost" port="8008"></host>

5    <debug start="false"/>

6    <fileSystem xfs="false"/>

7    <filter start="false">

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

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

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

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

12    </filter>

13    <inotify>

14    <delete start="true"/>

15    <createFolder start="true"/>

16    <createFile start="false"/>

17    <closeWrite start="true"/>

18    <moveFrom start="true"/>

19    <moveTo start="true"/>

20    <attrib start="false"/>

21    <modify start="false"/>

22    </inotify>

23

24    <sersync>

25    <localpath watch="/data">

26        <remote ip="172.16.1.41" name="nfsbackup"/>

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

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

29    </localpath>

30    <rsync>

31        <commonParams params="-az"/>

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

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

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

35        <ssh start="false"/>

36    </rsync>

37    <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

38    <crontab start="false" schedule="600"><!--600mins-->

39        <crontabfilter start="false">

40        <exclude expression="*.php"></exclude>

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

42        </crontabfilter>

43    </crontab>

44    <plugin start="false" name="command"/>

45    </sersync>

46

47    <plugin name="command">

48    <param prefix="/bin/sh" suffix="" ignoreError="true"/>    <!--prefix /opt/tongbu/mmm.sh suffix-->

49    <filter start="false">

50        <include expression="(.*)\.php"/>

51        <include expression="(.*)\.sh"/>

52    </filter>

53    </plugin>

54

55    <plugin name="socket">

56    <localpath watch="/opt/tongbu">

57        <deshost ip="192.168.138.20" port="8009"/>

58    </localpath>

59    </plugin>

60    <plugin name="refreshCDN">

61    <localpath watch="/data0/htdocs/cms.xoyo.com/site/">

62        <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>

63        <sendurl base="http://pic.xoyo.com/cms"/>

64        <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>

65    </localpath>

66    </plugin>

67 </head>

View Code 配置文件最终内容


1.3 第三里程碑: 启动sersync

1.3.1 修改文件的权限(可执行)

首先让程序让文件有执行权限

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

[root@nfs01 bin]# ll

total 1768

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

1.3.2 查看软件的帮助信息

[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命令推送一遍

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

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

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

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

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

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

________________________________________________________________

1.3.3 在程序的bin目录下启动程序

./sersync -dro /usr/local/sersync/conf/confxml.xml

1.3.4 启动方法二

将/usr/local/sersync/bin/程序的bin目录添加到PATH中

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sersync/bin/

然后sersync命令就能直接使用

[root@nfs01 scripts]# 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

------------------------------------------

rsync the directory recursivly to the remote servers once

working please wait...

execute command: cd /data && rsync -az -R --delete ./ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password >/dev/null 2>&1

run the sersync:

watch path is: /data

1.4 Inotify与 sersync总结对比

1.4.1 Inotify实时并发:

  结论:经过测试,每秒200文件并发,数据同步几乎无延迟(小于1秒)

1.4.2 inotify 优点:

  1)监控文件系统事件变化,通过同步工具实现实时数据同步。

1.4.3 inotify 缺点

  1)并发如果大于200个文件(10-100k),同步就会有延迟

  2)我们前面写的脚本,每次都是全部推送一次,但确实是增量的。也可以只同步变化的文件,不变化的不理。

  3)监控到事件后,调用rsync同步是单进程的,而sersync为多进程同步。既然有了inotify-tools,为什么还要开发sersync?

1.4.4 serysync功能多:(inotify+rsync命令)

  1)支持通过配置文件管理

  2)真正的守护进程socket

  3)可以对失败文件定时重传(定时任务功能)

  4)第三方的HTTP接口(例如:更新cdn缓存)

  5)默认多进程rsync同步

1.4.5 高并发数据实时同步方案小结:

  1)inotify(sersync)+ rsync,是文件级别的。

  2)drbd文件系统级别,文件系统级别,基于block块同步,缺点:备节点数据不可用

  3)第三方软件的同步功能:mysql同步(主从复制),oracle,mongodb

  4)程序双写,直接写两台服务器。

  5)利用产品业务逻辑解决(读写分离,备份读不到,读主)


2.1 man命令的级别

centos6

[root@nfs01 ~]# man man

      The standard sections of the manual include:

      1      User Commands                                #用户命令

      2      System Calls                                  #系统调用

      3      C Library Functions                          # Ç库函数

      4      Devices and Special Files                  #设备和特殊文件

      5      File Formats and Conventions                #文件格式和约定

      6      Games et. Al.                                  #游戏等。

      7      Miscellanea                                    #杂记

      8      System Administration tools and Daemons  #系统管理工具和程序

      Distributions  customize  the  manual section to their specifics,

      which often include additional sections.

centos7

[root@clsn tuichu]# man ~

1  Executable programs or shell commands

2  System calls (functions provided by the kernel)

3  Library calls (functions within program libraries)

4  Special files (usually found in /dev)

5  File formats and conventions eg /etc/passwd

6  Games

7  Miscellaneous  (including  macro  packages  and conventions), e.g.

    man(7), groff(7)

8  System administration commands (usually only for root)

9  Kernel routines [Non standard]

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,227评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,755评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,899评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,257评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,617评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,757评论 1 221
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,982评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,715评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,454评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,666评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,148评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,512评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,156评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,112评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,896评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,809评论 2 279
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,691评论 2 272

推荐阅读更多精彩内容