day 35综合架构实时同步

课程介绍部分

1.实时同步原理概念
2.实现实时同步服务部署 inotify
3.实现实时同步方式
    a利用脚本实现实时同步
    b利用软件实现实时同步 sersync  lsync

(补充扩展)

课程总结

一:网站实时同步服务

数据备份方法:
 1.定时任务备份数据:内部人员备份数据  备份数据最短周期是1分钟  
 2.实时同步备份数据:外部人员备份数据(用户)  没有同步等待时间
实时同步数据原理:
  01.监视存储服务器上指定目录   数据信息变化  inotify(监控软件)
  02.利用同步传输数据软件  将变化数据传输 rsync(传输数据)
  03.实现实时传输数据   inotify+rsync    实时同步
数据同步原理图

二.数据监控软件 inotify

作用:监控目录中数据信息变化
第一个里程
yum install -y inotify-tools     如果安装不上   查看一下  系统中epel源是否优化
/usr/bin/inotifywait  重要    监控目录数据信息变化命令   修改  创建  删除  移动
/usr/bin/inotifywatch 了解     统计目录产生变化的信息   修改多少次  创建多少次  删除多少次  移动多少次
第二个 里程:掌握inotifywait监控命令用法
--exclude      --- 进行监控数据时,指定哪些数据信息不要进行监控
--excludei     --- 进行监控数据时,指定哪些数据信息不要进行监控  根据指定数据名称信息 无论大小写进行排除监控
-m|--monitor            --- 一直对指定目录进行监控
-r|--recursive          --- 递归监控目录中数据变化
--format           --- 定义输出信息格式
          %w 监控目录路径信息
          %f 监控触发事件数据信息
          %e 相应事件信息
          %T 定时触发事件时间信息(调用--timefmt所定义时间格式)
--timefmt          --- 定义时间格式信息 date "+%F"
-q|--quiet              --- 将某些信息不要进行显示输出  >/dev/null
-e|--event              --- 指定监控的事件信息

inotify所有事件信息:
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
                    在监控目录中,有文件或目录数据信息删除操作

inotifywait -mr /data --format "%T %w %f %e" --timefmt "%F %T" -e create,delete,move,close_write                                        
Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.
2019-08-19 17:07:22 /data/oldboy100/ oldboy06.txt CREATE
2019-08-19 17:07:22 /data/oldboy100/ oldboy06.txt CLOSE_WRITE,CLOSE

[root@nfs01 /]# inotifywait -mrq /data --format "%T %w %f %e" --timefmt "%F %T" -e create,delete,move,close_write
2019-08-19 17:10:10 /data/oldboy100/ oldboy07.txt CREATE
2019-08-19 17:10:10 /data/oldboy100/ oldboy07.txt CLOSE_WRITE,CLOSE

inotify监控数据变化命令 -- 实现数据变化实时同步
inotifywait -mrq /data --format "%w%f" -e create,delete,move,close_write
inotify命令格式信息

三.实现实时同步数据过程

方法一:编写脚本
#01.监控目录数据信息变化
inotifywait -mrq /data --format "%w%f" -e create,delete,move,close_write|\while read line 
#02.将变化数据进行实时同步
rsync -avz 变化数据信息 [email protected]::backup --password-file=/etc/rsync.password
脚本信息:
#!/bin/bash
inotifywait -mrq /data --format "%w%f" -e create,delete,move,close_write|\while read line 
do
rsync -avz --delete /data/ [email protected]::backup --password-file=/etc/rsync.password
done
问题:
01.如何让脚本文件始终运行
nohup sh 脚本信息 &
02.利用脚本数据同步完毕后,脚本会依旧持续运行?
cd /data && rsync -az -R "./oldboy02.txt" [email protected]::backup --password-file=/etc/rsync.password
cd /data && rsync -az -R --delete ./   --include="oldboy01.txt" --exclude=*  [email protected]::backup --password-file=/etc/rsync.password
方法二:利用软件
第一个里程:下载部署实时同步软件 sersync-->inotify+rsync
https://github.com/wsgzao/sersync
mkdir /server/tools -p
将软件保存在此目录中
unzip sersync_installdir_64bit.zip 
cd /server/tools/sersync_installdir_64bit

第二个里程: 将解压好目录保存到指定目录中
mv sersync/ /usr/local/

第三个里程: 修改软件配置信息
vim conf/confxml.xml
# 定义 在同步传输数据时,哪些数据不要进行传输同步
6     
7         
8         
9         
 10         
 11     
 # 定义监控事件信息
 12     
 13         
 14         
 15         
 16         
 17         
 18         
 19         
 20         
 21     
 24         
 25             
 26             
 27             
 28         
 29         
 30             
 31             
 32             
第四个里程: sersync服务如何启动
cd /usr/local/sersync/bin/
chmod +x sersync
sersync -dro /usr/local/sersync/conf/confxml.xml    

显示数据同步过程方法:
修改配置文件:

sersync服务配置说明
补充:脚本循环用法:
01.for循环   for 变量 in 循环信息;do 操作命令;done  有限制循环
02.while循环    while  条件表达式;do 操作命令;done  死循环    当条件满足时,条件为真
03.until循环  until循环   until 条件表达式;do操作命令;done   死循环    当条件满足时,条件为假

四.课程知识点总结

1) 网站实时同步原理概念  1. 监控数据 2. 传输数据 3. 监控+传输
2) 掌握inotify监控软件使用方法
3) 掌握实现实时同步方法
   a 利用脚本实现 
     循环方式  shell内置变量 $# $*  脚本如何在后台一直运行 nohup 脚本信息 &
   b 利用软件实现
     sersync软件
     1) 软件部署过程 --- 二进制方式部署 解压
     2) 编写软件配置     结合rsync传输数据命令   
     3) 如何启动服务     sersync命令参数       
作业
01. lsync实时同步软件       周五
02. 如何编写sersync启动脚本 sersyncd stop/start/restart   二组

你可能感兴趣的:(day 35综合架构实时同步)