美文网首页
备份服务之实时同步备份(sersync)

备份服务之实时同步备份(sersync)

作者: 一只打不死的小强 | 来源:发表于2019-07-19 17:01 被阅读0次

实时同步备份方法
1)利用脚本实现实时同步
2)利用软件实现实时同步


实时同步备份原理

存储服务器 ---> 实时同步 ---> 备份服务器


一 实时备份的服务该如何建立

1.如何将存储上的用户产生的数据进行实时监控,然后进行实时传送到备份服务器上。(inotify)
2.存储服务器要进行数据传输同步 如何进行数据传输同步 (rsync备份服务)

  1. 如何让监控数据变化程序+rsync传输数据程序 建立关系.

二 如何进行实时监控目录数据的变化

监控目录数据变化软件:inotify
yum install -y inotify-tools

[root@nfs01 ~]# rpm -ql inotify-tools 
/usr/bin/inotifywait
/usr/bin/inotifywatch
/usr/lib64/libinotifytools.so.0
/usr/lib64/libinotifytools.so.0.4.1
/usr/share/doc/inotify-tools-3.14
/usr/share/doc/inotify-tools-3.14/AUTHORS
/usr/share/doc/inotify-tools-3.14/COPYING
/usr/share/doc/inotify-tools-3.14/ChangeLog
/usr/share/doc/inotify-tools-3.14/NEWS
/usr/share/doc/inotify-tools-3.14/README
/usr/share/man/man1/inotifywait.1.gz
/usr/share/man/man1/inotifywatch.1.gz

2.1 监控目录数据变化的命令

/usr/bin/inotifywait *****(重要)
/usr/bin/inotifywatch 监控事件(了解)
监控事件: 创建事件(6次) 删除事件(10) 修改事件 移动事件
监控这些东西意义不是很大


2.2 监控命令的应用及参数的含义

  • -m|--monitor ***** Keep listening for events forever. Without this option, inotifywait will exit after one
    //让监控过程始终保持下去;没有这个参数,只监控一次
  • -r|--recursive ***** Watch directories recursively.
    递归监控目录,对目录及目录的子目录也进行监控。
  • --format <fmt> Print using a specified printf-like format string; read the man page for more details.
    //定义输出信息格式
    //时间信息 创建什么文件 事件信息:创建文件
  • --timefmt <fmt> strftime-compatible format string for use with %T in --format string.
    //具体定义时间的格式
  • -q|--quiet ** Print less (only print events) 利用脚本使用同步备份
    //输出更少的信息,只输出事件信息
  • -e|--event <event1> [ -e|--event <event2> ... ]
    Listen for specific event(s). If omitted, all events are listened for.
    //只监控什么事件
  • -exclue //排除监控目录的指定子目录
  • -iexclude //排除的监控目录不区分大小写。

2.3 监控的事件

  • 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
    监控目录中有一个挂载点,包含文件或目录的文件系统被卸载

PS:重要事件 close_write move create delete

2.4 监控命令语法结构

inotifywait 参数信息 监控目录
inotifywait -mrq --timefmt "%F %T" --format "%T %w%f %e" /data
默认把所有事件都监控
%T
%F %T %w %f
2019-07-20 22:13:20 /data/oldboy_dir/oldboy04.txt CREATE

%w --- 监控的目录
%f --- 触发事件文件数据信息
%e --- 触发时间信息


图片.png

三 实现实时同步脚本编写过程

第一个历程:进行数据监控
inotifywait -mrq --format "%w%f" -e "close_write,move,create,delete" /data
/data/file01.txt
/data/file01.txt


第二个历程:部署好rsync守护进程服务
服务端:安装软件 --> 编辑文件 --> 创建用户 --> 创建密码文件(授权)--> 创建备份目录(权限属主属组)--> 启动服务

客户端:创建密码文件(600)--> 测试传输

第三个历程:编写脚本
#!/bin/bash
/data/ 1000个文件
oldboy01.txt
inotifywait -mrq --format "%w%f" -e "close_write,move,create,delete" /data|
while read line
//这种情况直接调用会出现问题,在目录的子目录创建文件 或者重命名的时候都无法正常创建,会出现问题
//原因:是因为当删除的时候,rsync进行增量备份,删除的信息已经删除无法做同步。
do
rsync -az $line--delete /date/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
//使用 --delete /date 传送rsync的传送的时候 使用无差异同步会解决上述问题。
done


3.2 当执行脚本写完出现的问题

  • 问题一:执行脚本总有输出信息,并且脚本不能持续运行 & 让脚本后台运行
    如何实现脚本在后台一直运行:

  • 方法一: 执行脚本后面加上 &
    sh /server/scripts/inotify_watch.sh & 连接断开,就后台进程会消失

  • 方法二: 利用专用命令 nohup
    nohup sh /server/scripts/inotify_watch.sh &

  • 问题二:批量创建文件的时候,数据已经同步完毕,但是脚本还在一直运行
    (touch oldboy{01..10}.txt,创建完就目录就会有的,当脚本读取完直接就传到备份服务器。后续while每一行读到的都属于增量备份。)

四.利用软件实现实时同步(专业的编写)

sersync软件实现实时同步:先安装好inotify+rsync
或者是lsyncd
git软件仓库

如何部署安装sersync软件
第一个历程:下载获取软件
[git软件仓库](https://github.com/wsgzao/sersync) 发明人--周洋 金山公司
第二个历程:上传软件到linux系统
yum install -y lrzsz
rz -y  windows数据 ---> linux系统
sz -y  linux数据   ---> windows系统
第三个历程:安装部署sersync软件(二进制包)
    unzip sersync_installdir_64bit.zip
    mv|cp -a /server/tools/sersync_installdir_64bit/sersync/ /usr/local/ 
//正规下载的软件只有两个目录 一个是/conf 一个是/bin
第四个历程:修改配置文件
    vim /usr/local/sersync/conf/confxml.xml //66行内容
说明:排除指定信息不要进行实时同步
<debug start="false"/> //开启调试功能。
    <fileSystem xfs="false"/>
    <filter start="false">

    6     <filter start="false">
                 过滤  开始
    7         <exclude expression="(.*)\.svn"></exclude>
    8         <exclude expression="(.*)\.gz"></exclude>
    9         <exclude expression="^info/*"></exclude>
   10         <exclude expression="^static/*"></exclude>
   11     </filter>
   //说明:指定监控的事件信息
   12     <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>

//重点说明:实现实时同步关键配置
    24         <localpath watch="/opt/tongbu">
    25             <remote ip="127.0.0.1" name="tongbu1"/>
    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="-artuz"/> //后面不需要加/线
    31             <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
    32             <userDefinedPort start="false" port="874"/><!-- port=874 -->//如果端口有变化的时候可以更改为true

可参见此图可将配置文件输入相应的内容


第五个历程:启动实时同步服务,并检查测试
需要进入到此软件的/bin下目录,但注意的是不能用systemctl启动
    chmod +x /usr/local/sersync/bin/sersync
//不能直接执行此命令,环境变量中便没有环境变量的路径
    # ln -s /usr/local/sersync/bin/sersync /usr/local/sbin/sersync
    # ll /usr/local/sbin/sersync 
    lrwxrwxrwx 1 root root 30 Jul 21 00:28 /usr/local/sbin/sersync -> /usr/local/sersync/bin/sersync
第六个历程:使用命令启动服务
sersync -dro//o必须放在最后面   /usr/local/sersync/conf/confxml.xml //运行前先把之前运行的脚本先停止。pkill innotify

4.2 sersync命令的相关参数

命令重要参数:
参数-d:   启用守护进程模式
参数-r:   在监控前,将监控目录与远程主机用rsync命令推送一遍//测试 
参数-o:   指定配置文件,默认使用confxml.xml文件//可以使用多个文件 可以监控多个目录 一个配置文件监控一个目录
          /usr/local/sersync/conf/confxml.xml
          /usr/local/sersync/conf/confxml01.xml
          /usr/local/sersync/conf/confxml02.xml
sersync -dro /usr/local/sersync/conf/confxml.xml

五 实现弃用NFS服务方案:

图片.png
原理:预防单点故障的问题,在存储服务器上监控数据数据目录,通过sersync进行反向同步,当存储服务器宕机的时候用户可以从web01上的读取数据。

相关文章

  • day36 实时备份wenben

    实时同步备份 定义:通过 sersync 服务将 nfs 服务器的存储,实时备份到 backup 服务器中,以此来...

  • 备份服务之实时同步备份(sersync)

    实时同步备份方法1)利用脚本实现实时同步2)利用软件实现实时同步 实时同步备份原理 存储服务器 --...

  • 基本网站架构

    分类链接备份服务rsync服务网络存储服务NFS服务实时同步服务Sersync服务SSH服务SSH服务批量管理服务...

  • sersync实时备份服务

    一、如何提高网站并发访问量高的方法? 二、sersync介绍 1>实时同步是一种只要当前目录发生变化则会触发一个事...

  • day36-2019年4月18日

    数据实时备份 准备环境:nfs01存储服务器 和 backup备份服务器 1、什么是实时备份? 实时备份是利用实时...

  • 实现实时同步备份总结

    实现实时同步备份总结 \ 一. 实时同步备份原理1.inotify实时监控2.rsync实时传输3.sersy...

  • sersync 对网站数据实时同步备份

    sersync 实现实时同步数据 1 sersync实时同步原理 rsync守护进程服务,实现数据传输 inoti...

  • 课堂笔记day36——inotify以及sersync实时备份服

    1. inotify+sersync实时复制应用简介 1.1 实时复制介绍 利用实时复制方式,实现实时备份重要数据...

  • day36 文件实时同步

    备份服务方案( 利用实时复制方式,实现实时备份数据等重要信息) 2.1、实时复制细节实现说明 2.2、实时复制软件...

  • Linux学习-week12-综合架构实时同步

    老男孩教育64期--week12--综合架构实时同步 综合架构知识概述说明存储服务客户端配置参数实时同步备份数据方...

网友评论

      本文标题:备份服务之实时同步备份(sersync)

      本文链接:https://www.haomeiwen.com/subject/jrhslctx.html