day35 sersync实时同步

实时同步概述
  • 实时同步概念说明
    实时同步的概念:是一种只要当前目录发生变化则会触发一个事件,事件出发后会将变化的目录同步至远程服务器。

  • 实时同步的作用:
    保证数据的连续性;
    减少人力维护成本。

  • 数据备份的方法:
    定时任务备份:主要是内部人员备份数据,备份周期最短是1分钟。
    实时任务备份:主要是外部人员备份数据,一般是用户保存数据,当用户在网站提交保存数据时,没有同步等待时间。

  • 实时同步的原理:
    1.监控存储服务器,查看数据的信息变化,主要用到的工具为inotify;
    2.利用同步传输数据软件,将变化的数据增量传输给远程服务器;
    3.实现实时同步。

实时同步的工具:sersync+rsync,inotify+rsync
Inotify是一个通知接口,用来监控文件系统的各种变化,如果文件存取,删除,移动。可以非常方便地实现文件异动告警,增量备份功能,并针对目录或文件的变化及时作出响应。Inotify+rsync可以实出发式实时同步增量备份。
inotify的作用:监控目录中数据信息变化;
sersync是国人基于rsync+inotify-tools开发的工具,不仅保留了inotify的优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高了运行效率,节省时间和网络资源。

sersync服务安装部署
  • 第一步:安装软件,将软件推送到指定的目录
[root@nfs01 ~]# yum install rsync inotify -y
[root@nfs01-server ~]# cd /server/tools/
[root@nfs01-server tools]# ls
sersync_installdir_64bit.zip
[root@nfs01-server tools]# mv sersync_installdir_64bit.zip  /usr/local/
[root@nfs01-server tools]# unzip /usr/local/sersync_installdir_64bit.zip 
Archive:  /usr/local/sersync_installdir_64bit.zip
   creating: sersync_installdir_64bit/
   creating: sersync_installdir_64bit/sersync/
   creating: sersync_installdir_64bit/sersync/bin/
  inflating: sersync_installdir_64bit/sersync/bin/sersync  
   creating: sersync_installdir_64bit/sersync/conf/
  inflating: sersync_installdir_64bit/sersync/conf/confxml.xml  
   creating: sersync_installdir_64bit/sersync/logs/
  • 第二步:修改配置文件信息
<inotify>
 13         <delete start="true"/>
 14         <createFolder start="true"/>
 15         <createFile start="false"/>
 16         <closeWrite start="true"/>
 17         <moveFrom start="true"/>
 18         <moveTo start="true"/>
 19         <attrib start="false"/>
 20         <modify start="false"/>
 21     </inotify>
 <sersync>
 24         <localpath watch="/backup"> ## 目录信息
 25           <remote ip="172.16.1.41" name="backup"/> ## 备份服务器IP,backup模块信息
 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="-avz"/> ## 选项参数,默认有-delete
 31             <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>                 ## 开启认证用户功能   
 32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
 33             <timeout start="false" time="100"/><!-- timeout=100 -->
 34             <ssh start="false"/>
 35         </rsync>

[root@nfs01-server local]# ll /usr/local/sersync_installdir_64bit/sersync/bin/sersync        
-rwxr--r-- 1 root root 1810128 Oct 26  2011 /usr/local/sersync_installdir_64bit/sersync/bin/sersync

[root@nfs01-server bin]# ./sersync -dro  /usr/local/sersync_installdir_64bit/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_installdir_64bit/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 /backup && 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: /backup ## 成功!!
实现实时同步的脚本

[root@nfs01 scripts]# cat sersync.sh

#!/bin/bash
#1. jiankong date
inotifywait -mrq /backup --format "%w%f" -e create,delete,close_write,move | while read line

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

推荐阅读更多精彩内容