Linux63期day35

网站实时同步服务:

数据备份方法:
定时任务备份数据: 内部人员备份数据        备份数据最短周期1分钟          
实时同步备份数据: 外部人员备份数据(用户)  没有同步等待时间

实时同步数据原理:
01. 监视存储服务器上指定目录  数据信息变化      inotify 监控软件
02. 利用同步传输数据软件      将变化数据传输    rsync   传输数据
03. 实现实时传输数据          inotify+rsync             实时同步

数据监控软件 inotify
作用: 监控目录中数据信息变化
第一个里程: 部署安装软件
yum install -y inotify-tools --- 系统中epel源是否优化

/usr/bin/inotifywait   重要  --- 监控目录数据信息变化命令  修改       创建       删除       移动
/usr/bin/inotifywatch  了解  --- 统计目录产生变化的信息    修改多少次 创建多少次 删除多少次 移动多少次

第二个里程: 掌握inotifywait监控命令用法:
--exclude  <pattern>    --- 进行监控数据时,指定哪些数据信息不要进行监控
--excludei <pattern>    --- 进行监控数据时,指定哪些数据信息不要进行监控  根据指定数据名称信息 无论大小写进行排除监控
-m|--monitor            --- 一直对指定目录进行监控
-r|--recursive          --- 递归监控目录中数据变化
--format <fmt>          --- 定义输出信息格式
                            %w 监控目录路径信息 
                            %f 监控触发事件数据信息 
                            %e 相应事件信息
                            %T 定时触发事件时间信息(调用--timefmt所定义时间格式)
--timefmt <fmt>         --- 定义时间格式信息 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

实现实时同步数据过程:

方法一: 编写脚本
# 01. 监控目录数据信息变化
inotifywait -mrq /data --format "%w%f" -e create,delete,move,close_write|\
while read line

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

# 02. 将变化数据进行实时同步
rsync -avz $line  rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

脚本信息:
#!/bin/bash
inotifywait -mrq /data --format "%w%f" -e create,delete,move,close_write|\
while read line=oldboy03.txt
do 
   rsync -avz --delete /data/  rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
done

问题:
01. 如何让脚本文件始终运行
nohup sh /server/scripts/inotify.sh &

02. 利用脚本数据同步完毕后, 脚本会依旧持续运行?
cd /data && rsync -az -R "./oldboy02.txt" rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
cd /data && rsync -az -R --delete ./   --include="oldboy01.txt" --exclude=*  rsync_backup@172.16.1.41::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     <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
27
28 </localpath>
29 <rsync>
30 <commonParams params="-artuz"/>
31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
32 <userDefinedPort start="false" port="874"/>

第四个里程: sersync服务如何启动
cd /usr/local/sersync/bin/
chmod +x sersync
sersync -dro /usr/local/sersync/conf/confxml.xml    

显示数据同步过程方法:
修改配置文件:
<debug start="true"/>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容