inotify+rsync实现实时同步备份

第一个里程:将inotify软件安装成功
yum install -y inotify-tools

[root@nfs01 ~]# rpm -ql inotify-tools
/usr/bin/inotifywait                <--- 实现对数据目录信息变化监控(重点了解的命令)
/usr/bin/inotifywatch               <--- 监控数据信息变化,对变化的数据进行统计

[root@nfs01 ~]# cd /proc/sys/fs/inotify/
[root@nfs01 inotify]# ll
总用量 0
-rw-r--r-- 1 root root 0 2018-02-25 19:45 max_queued_events    
-rw-r--r-- 1 root root 0 2018-02-25 19:45 max_user_instances
-rw-r--r-- 1 root root 0 2018-02-25 19:45 max_user_watches
max_user_watches:   设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
                    默认只能监控8192个文件

max_user_instances: 设置每个用户可以运行的inotifywait或inotifywatch命令的进程数
                    默认每个用户可以开启inotify服务128个进程

max_queued_events:  设置inotify实例事件(event)队列可容纳的事件数量
                    默认监控事件队列长度为16384

第二个里程:将rsync守护进程模式部署完毕
rsync服务端部署
a 检查rsync软件是否已经安装
b 编写rsync软件主配置文件
c 创建备份目录管理用户
d 创建备份目录,并进行授权
e 创建认证文件,编写认证用户和密码信息,设置文件权限为600
f 启动rsync守护进程服务

rsync客户端部署
a 检查rsync软件是否已经安装   
b 创建认证文件,编写认证用户密码信息即可,设置文件权限为600
c 利用客户端进行数据同步测试

第三个里程:要让inotify软件和rsync软件服务建立连接(shell脚本)
rsync软件应用命令:
rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password

inotify软件应用命令:
inotifywait 
-m|--monitor             始终保持事件监听状态
-r                       进行递归监控
-q|--quiet               将无用的输出信息,不进行显示
--timefmt <fmt>          设定日期的格式
                         man strftime 获取更多时间参数信息
--format <fmt>           命令执行过程中,输出的信息格式

-e                       指定监控的事件信息
man inotifywait 查看所有参数说明和所有可以监控的事件信息

总结主要用到的事件信息:
create创建、delete删除、moved_to移入、close_write修改

inotifywait -mrq --timefmt "%F" --format "%T %w%f 事件信息:%e" /data  <-- 相对完整的命令应用
inotifywait -mrq --timefmt "%F" --format "%T %w%f 事件信息:%e" -e create /data   <-- 指定监控什么事件信息

inotifywait -mrq --format "%w%f" -e create,delete,moved_to,close_write  /data   
以上为实现实时同步过程,所需要的重要监控命令

编写脚本:实现inotify与rsync软件结合
#!/bin/bash
####################    
inotifywait -mrq --format "%w%f" -e create,delete,moved_to,close_write  /data|\
while read line
do
rsync -az --delete /data/ [email protected]::backup --password-file=/etc/rsync.password
done

shell循环语法总结:
for循环       for xx in 循环条件内容信息;do xxx;done
while循环     while 循环条件;do xx ;done    <-- 只要条件满足,就一直循环
              while true;do xx ;done         <-- 死循环

运维工作中编写自动化脚本规范:
1. 先完成基本功能需求
2. 优化完善脚本内容
3. 写上一些注释说明信息
4. 进行反复测试

第四个里程:最终的测试
sh -x intify.sh

猜你喜欢

转载自blog.51cto.com/tangyong/2124323