36.三种中小规模实时同步方案------ 1. inotify + rsync |2. lsyncd | 3. sersync |

几大实时同步工具比较

1.inotify + rsync

inotify +
rsync,随着文件数量的增大到100W+,目录下的文件列表就达20M,在网络状况不佳或者限速的情况下,变更的文件可能10来个才几M,却因此要发送的文件列表就达20M,严重减低的带宽的使用效率以及同步效率;更为要紧的是,加入inotifywait在5s内监控到10个小文件发生变化,便会触发10个rsync同步操作,结果就是真正需要传输的才2-3M的文件,比对的文件列表就达200M。使用这两个组合的好处在于,它们都是最基本的软件,可以通过不同选项做到很精确的控制,比如排除同步的目录,同步多个模块或同步到多个主机。

2.lsyncd

Lysncd 实际上是lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的
inotify 触发机制,然后通过rsync去差异同步,达到实时的效果。我认为它最令人称道的特性是,完美解决了 inotify +
rsync海量文件同步带来的文件频繁发送文件列表的问题 ——
通过时间延迟或累计触发事件次数实现。另外,它的配置方式很简单,lua本身就是一种配置语言,可读性非常强。lsyncd也有多种工作模式可以选择,本地目录cp,本地目录rsync,远程目录rsyncssh。
实现简单高效的本地目录同步备份(网络存储挂载也当作本地目录),一个命令搞定。

3.sersync

sersync 这么个工具可以提高同步的性能,也解决了同步大文件时出现异常的问题,
sersync是国内的一个开发者开源出来的,使用c++编写,采用多线程的方式进行同步,失败后还有重传机制,对临时文件过滤,自带crontab定时同步功能。国产开源,文档不是很全,在2011年之后就没更新了网上关于它的使用和讨论都止于10年了

  • sersync的功能:

1.支持配置文件管理

2.真正的守护进程socket

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

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

5.默认多线程rsync同步

优点:

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

2.相比其他项目,sersync配置起来简单

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

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

5.Sersync自带crontab功能,只需在xml配置文件中开启,即可按预先的配置,隔一段时间整体同步一次
6.Sersync自带socket与http的协议扩展,可以满足有特殊需求的公司二次开发。

缺点:

sersync有重传机制(内置的定时任务),但是只重传一次,如果重传失败就会清空重传队列,即清空/tmp/rsync_fail_log.sh文件。这样就会造成sersync服务恢复正常后(即可以同步文件),数据同步的两个服务器数据不一致的现象。因为,/tmp/rsync_fail_log.sh文件为空,而sersync只对发生变化的文件做实时同步(ps:sersync默认每隔十个小时做一次完整的数据同步)。


一,基于inotify脚本部署全网备份服务:

1. 安装
1) 从内核和目录里面查看是否支持inotify

[root@nfs01 ~]# uname -r
2.6.32-573.el6.x86_64

2)检查是否有安装inotify 如果没有就安装
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

rpm -qa inotify-tools

3)了解怎么使用

/usr/bin/inotifywait        — 实现监控目录数据变化信息 添加 修改 删除 关注
/usr/bin/inotifywatch       — 实时统计目录数据变化信息 添加10 修改20 i

2.notifywait手册:

inotifywait [ options ]  /data  日志

--exclude Exclude all events on files matching the extended regular expression .
实时同步数据时, 将部分文件排除进行同步操作

--excludei Like --exclude but case insensitive.
实时同步数据时, 忽略文件大小写进行同步排除操作

-m|--monitor Keep listening for events forever. Without this option, inotifywait will exit after one event is received.
维持事件永久监听状态. 如果没有此参数, 一次事件被接收后就退出监视过程

-r|--recursive Watch directories recursively
递归监控目录数据信息变化

-q|--quiet Print less (only print events).
输出很少的信息 (只输出事件信息)

-qq Print nothing (not even events).
什么都没有(甚至事件信息也不要输出)

--format Print using a specified printf-like format string; read the man page for more details.
设计规划命令输出信息格式

--timefmt strftime-compatible format string for use with %T in --format string.
规划时间信息格式

-e|--event [ -e|--event ... ]
Listen for specific event(s). If omitted, all events are listened for.
指定监听事件信息, 如果忽略参数配置, 所有事件都要监视

同步时需要监控事件信息 close_write move create delete

access file or directory contents were read
modify file or directory contents were written 修改
attrib file or directory attributes changed
close_write file or directory closed, after being opened in writeable mode

文件或目录被写入新的信息后, 进行关闭操作
close_nowrite file or directory closed, after being opened in read-only mode

文件或目录没有写入新的信息后, 进行关闭操作
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory

文件或目录有数据变化, 数据信息是从其他目录转移过来
moved_from file or directory moved from watched directory

文件或目录有数据变化, 数据信息是从监控服务转到其它目录
move file or directory moved to or from watched directory
create file or directory created within watched directory

文件或目录进行创建时需要被监控
delete file or directory deleted within watched directory

文件或目录进行删除时需要被监控
delete_self file or directory was deleted
unmount file system containing file or directory unmounted

3. inotify部署全网备份:

  • 基础使用方法 :

inotifywait -mrq /data
扩展 :
inotifywait -mrq /data --format “%T %w%f %e” --timefmt="%F_%A %T"
或者
inotifywait -mrq /data --format “%T %w%f %e” --timefmt="%F_%A %T" -e “close_write,move,create,delete”

  • 编写inotifly实时同步脚本
#!/bin/bash
   inotifywait -mrq /data  --format "%w%f" -e "close_write,move,create,delete" | while read line
   do
    rsync -az /data/ --delete rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.pass
   done
  • 使用脚本
    nohup sh /server/scripts/inotify.sh & #nohup
    该命令可以在你退出帐户/关闭终端之后继续运行相应的进程,& 后台运行

二,lsync部署实时同步服务

  • 安装部署lsync实时同步工具
    yum install -y lsyncd

  • 编写配置文件 (注释去掉不然会报错)

vim /etc/lsyncd.conf
settings {
 logfile = "/var/log/lsyncd/lsyncd.log",                #日志文件
 statusFile = "/var/log/lsyncd/lsyncd.status",          #状态信息
 inotifyMode = "CloseWrite",
 maxProcesses = 8,
}
sync {
 default.rsync,
 source = "/data",                  #监控NFS目录
 target = "rsync_backup@172.16.1.41::backup",   #实时同步到备份服务器 backup目录
 delete= true,                       #无差异同步
 exclude = { ".*" },
 delay = 1,
rsync = {
 binary = "/usr/bin/rsync",
 archive = true,
 compress = true,
 verbose = true,
 password_file = "/etc/rsync.password",        #NFs密码文件
 _extra = {"--bwlimit=200"}
}
}
  • 启动服务
    systemctl start lsyncd

三,sersync二进制部署实时同步服务。

接收机上的操作  Backup服务器

  • 安装rsync
    [root@backup ~]# yum install rsync -y

  • 配置rsync

[root@backup ~]# cat /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup

根据配置完成对应初始化操作

[root@backup ~]# groupadd -g 666 www
[root@backup ~]# useradd -u 666 -g666 www
[root@backup ~]# mkdir /backup /data
[root@backup ~]# chown -R www.www /backup/ /data/
[root@backup ~]# echo "rsync_backup:123456" > /etc/rsync.passwd   
[root@backup ~]# chmod 600 /etc/rsync.passwd
  • 重启Rsync服务
    [root@backup ~]# systemctl restart rsyncd

推送源机上的操作   NFS服务器:

1.下载并解压sersync软件包

如果大家网络不好上不去git,这里下载好的sersync二进制软件包。
https://files.cnblogs.com/files/hypj/sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# mv GNU-Linux-x86/ /usr/local/sersync

2.配置confxml.xml

[root@localhost conf]# cat /usr/local/sersync/confxml.xml 
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
   # 设置本地IP和端口
   <host hostip="localhost" port="8008"></host>
   # 开启DUBUG模式  
   <debug start="false"/>
   # 开启xfs文件系统
   <fileSystem xfs="false"/>
   # 同步时忽略推送的文件(正则表达式),默认关闭
   <filter start="false">
       <exclude expression="(.*)\.svn"></exclude>
       <exclude expression="(.*)\.gz"></exclude>
       <exclude expression="^info/*"></exclude>
       <exclude expression="^static/*"></exclude>
   </filter>
   <inotify>
   # 设置要监控的事件
       <delete start="true"/>
       <createFolder start="true"/>
       <createFile start="true"/>
       <closeWrite start="true"/>
       <moveFrom start="true"/>
       <moveTo start="true"/>
       <attrib start="true"/>
       <modify start="true"/>
   </inotify>
   <sersync>
   # 本地同步的目录路径
       <localpath watch="/home/data">
   # 远程IP和rsync模块名 
           <remote ip="192.168.1.35" name="rsyncmysql"/>  
           <!--<remote ip="192.168.8.39" name="tongbu"/>-->
           <!--<remote ip="192.168.8.40" name="tongbu"/>-->
       </localpath>
       <rsync>
   # rsync指令参数
           <commonParams params="-auvzP"/>
   # rsync同步认证
           <auth start="true" users="rsync" passwordfile="/etc/rsync.pas"/>
   # 设置rsync远程服务端口,远程非默认端口则需打开自定义
           <userDefinedPort start="false" port="873"/><!-- port=874 -->
   # 设置超时时间
           <timeout start="true" time="100"/><!-- timeout=100 -->
   # 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书
           <ssh start="false"/>
       </rsync>
    # sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。
       <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
    # 设置rsync+crontab定时传输,默认关闭
       <crontab start="false" schedule="600"><!--600mins-->
           <crontabfilter start="false">
               <exclude expression="*.php"></exclude>
               <exclude expression="info/*"></exclude>
           </crontabfilter>
       </crontab>
   # 设置sersync传输后调用name指定的插件脚本,默认关闭
       <plugin start="false" name="command"/>
   </sersync>
   # 插件脚本范例
   <plugin name="command">
       <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->
       <filter start="false">
           <include expression="(.*)\.php"/>
           <include expression="(.*)\.sh"/>
       </filter>
   </plugin>
   # 插件脚本范例
   <plugin name="socket">
       <localpath watch="/opt/tongbu">
           <deshost ip="192.168.138.20" port="8009"/>
       </localpath>
   </plugin>
   <plugin name="refreshCDN">
       <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
           <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxx"/>
           <sendurl base="http://pic.xoyo.com/cms"/>
           <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
       </localpath>
   </plugin>
</head>

3.启动
[root@nfs ~]# /usr/local/sersync/sersync2 -h
[root@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

4.重启操作如下:
killall sersync2 && sersync2 -dro /usr/local/sersync/conf/confxml.xml

ps 无法由systemctl启动,只能手动/脚本启停,pkill sersync2,sersync2 -dro
/usr/bin/xxxxx 可以通过多个配置文件实现,不同目录的备份服务

5.配置开机启动
echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local
echo "sersync2 -dro /usr/local/sersync/conf/confxml.xml" >> /etc/rc.local
NFS故障了,如何实现切换?

1.停止nfs服务 [root@nfs ~]# systemctl stop nfs

2.web切换操作
[root@web01 ~]# umount -lf /var/www/html/data/User/admin/home && mount -t nfs 172.16.1.41:/data /var/www/html/data/User/admin/home

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值