浅谈数据同步实现rsync+inotify

❎ 数据的实时同步介绍

在生产环境,有时会需要两台主机的特定目录实现实时同步。比如,将NFS共享目录的数据文件,自动实时同步到备份的服务器特定目录中
在这里插入图片描述
实时同步技术介绍

  • 实现实时同步的方法
    • inotify + rsync 方式实现数据同步
    • sersync :金山公司周洋在 inotify 软件基础上进行开发的,功能更加强大
  • 工作原理:
    要利用监控服务(inotify),监控同步数据服务器目录中信息的变化
    发现目录中数据产生变化,就利用rsync服务推送到备份服务器上

❎ 数据的实时同步实现

inotify

🌐 inotify的介绍

  • 异步的文件系统事件监控机制,利用事件驱动机制,而无须通过诸如cron等的轮询机制来获取事件
  • linux内核从2.6.13起支持 inotify,通过inotify可以监控文件系统中添加、删除,修改、移动等各种事件
[root@Centos7 ~]$grep -i inotify /boot/config-3.10.0-1062.el7.x86_64 
CONFIG_INOTIFY_USER=y

🌐 实现inotify软件:

  • inotify-tools
  • sersync
  • lrsyncd

🌐 inotify+rsync使用方式

  • inotify 对同步数据目录信息的监控
  • rsync 完成对数据的同步
  • 利用脚本进行结合

🌐 实现inotify

1️⃣ 查看内核情况

  • 内核是否支持inotify
    Linux支持inotify的内核最小版本为 2.6.13,参看man 7 inotify
[root@Centos7 ~]$ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Dec 20 14:59 max_queued_events
-rw-r--r-- 1 root root 0 Dec 20 14:59 max_user_instances
-rw-r--r-- 1 root root 0 Dec 20 14:59 max_user_watches
  • inotify内核参数说明:

    • max_queued_events:inotify事件队列最大长度,如值太小会出现 Event Queue Overflow 错误,默认值:16384
    • max_user_instances:每个用户创建inotify实例最大值,默认值:128
    • max_user_watches:可以监视的文件数量(单进程),默认值:8192
  • 示例:

/ 修改内存配置文件
[root@Centos7 ~]$vim /etc/sysctl.conf 
[root@Centos7 ~]$sysctl -p
fs.inotify.max_queued_events = 66666
fs.inotify.max_user_watches = 100000
[root@Centos7 ~]$cat /proc/sys/fs/inotify/*
66666
128
100000

2️⃣ inotify-tools工具

  • inotify-tools参考文档:https://github.com/rvoicilas/inotify-tools/wiki
  • 安装inotify-tools:基于epel源 yum -y install inotify-tools
  • inotify-tools包主要工具:
    inotifywait: 在被监控的文件或目录上等待特定文件系统事件(open ,close,delete等)发
    生,常用于实时同步的目录监控
    inotifywatch:收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计

3️⃣ inotifywait 命令常见选项

-m, --monitor 始终保持事件监听
-d, --daemon 以守护进程方式执行,和-m相似,配合-o使用
-r, --recursive 递归监控目录数据信息变化
-q, --quiet 输出少量事件信息
--exclude 指定排除文件或目录,使用扩展的正则表达式匹配的模式实现
--excludei 和exclude相似,不区分大小写
-o, --outfile 打印事件到文件中,相当于标准正确输出,注意:使用绝对路径
-s, --syslogOutput 发送错误到syslog相当于标准错误输出
--timefmt 指定时间输出格式
--format 指定的输出格式;即实际监控输出内容
-e 指定监听指定的事件,如果省略,表示所有事件都进行监听
  • inotifywait 的 --timefmt 时间格式
    参考 man 3 strftime
%Y 年份信息,包含世纪信息
%y 年份信息,不包括世纪信息
%m 显示月份,范围 01-12
%d 每月的第几天,范围是 01-31
%H 小时信息,使用 24小时制,范围 00-23
%M 分钟,范围 00-59
  • inotifywait 的 --format 格式定义
%T 输出时间格式中定义的时间格式信息,通过 --timefmt option 语法格式指定时间信息
%w 事件出现时,监控文件或目录的名称信息
%f 事件出现时,将显示监控目录下触发事件的文件或目录信息,否则为空
%e 显示发生的事件信息,不同的事件默认用逗号分隔
%Xe显示发生的事件信息,不同的事件指定用X进行分隔
  • inotifywait -e 选项指定的事件类型
create 文件或目录创建
delete 文件或目录被删除
modify 文件或目录内容被写入
attrib 文件或目录属性改变
close_write 文件或目录关闭,在写入模式打开之后关闭的
close_nowrite 文件或目录关闭,在只读模式打开之后关闭的
close 文件或目录关闭,不管读或是写模式
open 文件或目录被打开
moved_to 文件或目录被移动到监控的目录中
moved_from 文件或目录从监控的目录中被移动
move 文件或目录不管移动到或是移出监控目录都触发事件
access 文件或目录内容被读取
delete_self 文件或目录被删除,目录本身被删除
unmount 取消挂载

4️⃣ 使用inotifywait示例

  • 安装inotify-tools
yum -y install epel-release
yum -y install inotify-tools
  • 开启持续监控
    inotifywait -mrq /data 持续监控 递归监控/data目录变化,并输出少量信息
    在这里插入图片描述

  • 开始后台持续监控,并设定其输出格式
    inotifywait -o /root/inotify.log -drq /data --timefmt "%Y-%m-%d %H:%M" --format "%T %w%f event: %e"
    在这里插入图片描述

  • 持续前台监控特定事件
    inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f event: %;e" -e create,delete,moved_to,close_write,attrib
    在这里插入图片描述

rsync

1️⃣ rsync基本概述

  • rsync 常用于做为 linux系统下的数据镜像备份工具,
    • 实现实现远程同步,支持本地复制,或者与其他
    • SSH、rsync主机同步数据,支持增量备份,配合任务计划,
    • rsync能实现定时或间隔同步,配合inotify或sersync,可以实现触发式的实时数据同步
  • 官方网站: http://rsync.samba.org/
  • 软件包:rsync,rsync-daemon(CentOS 8)
  • 服务文件:/usr/lib/systemd/system/rsyncd.service
  • 配置文件:/etc/rsyncd.conf
  • 端口:873/tcp

2️⃣ rsync工作模式

1️⃣ 本地模式

  • 语法格式:rsync [OPTION...] SRC... [DEST]
[root@Centos7 ~]$echo 123 > test.txt
[root@Centos7 ~]$cat test.txt 
123
[root@Centos7 ~]$rsync test.txt /tmp/
[root@Centos7 ~]$cat /tmp/test.txt 
123

2️⃣ 远程shell模式

  • 本地主机使用远程shell和远程主机通信。
  • 命令行语法格式
Pull:
rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push:
rsync [OPTION...] SRC... [USER@]HOST:DEST
  • 示例1,推送到远程主机
    rsync test.txt [email protected]:/tmp
    在这里插入图片描述
  • 示例2,拉取下载远程主机文件
    rsync -avz [email protected]/root/test2.txt /tmp
    在这里插入图片描述
  • 示例3,拉取目录
[root@Centos7 ~]$mkdir /data/backup
/ 拉取远程文件
[root@Centos7 ~]$rsync -avz root@172.20.54.2:/etc/hostname /data/backup
root@172.20.54.2's password: 
receiving incremental file list
hostname

sent 43 bytes  received 107 bytes  42.86 bytes/sec
total size is 16  speedup is 0.11
/ 拉取远程主机目录下内容,不要目录本身
[root@Centos7 ~]$rsync -avz root@172.20.54.2:/root/ /data/backup/
root@172.20.54.2's password: 
receiving incremental file list
./
.bash_history
.bash_logout
.bash_profile
.bashrc
.cshrc
.tcshrc
.viminfo
anaconda-ks.cfg
test2.txt

sent 198 bytes  received 3,412 bytes  1,031.43 bytes/sec
total size is 8,414  speedup is 2.33

/ 拉取远程主机目录以及目录下内容
[root@Centos7 ~]$rsync -avz root@172.20.54.2:/root /data/backup/
root@172.20.54.2's password: 
receiving incremental file list
root/
root/.bash_history
root/.bash_logout
root/.bash_profile
root/.bashrc
root/.cshrc
root/.tcshrc
root/.viminfo
root/anaconda-ks.cfg
root/test2.txt

sent 199 bytes  received 3,428 bytes  1,036.29 bytes/sec
total size is 8,414  speedup is 2.32

3️⃣ 网络套接字连接远程主机

  • 本地主机通过网络套接字连接远程主机上的rsync daemon。
  • 命令行语法格式
Pull:            / 两种写法
rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push:
rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

4️⃣ 后台服务模式区别

  • Rsync 借助 SSH 协议同步数据存在的缺陷
    1.使用系统用户(不安全)
    2.使用普通用户(会导致权限不足情况)
    3.守护进程传输方式: rsync 自身非常重要的功能(不使用系统用户,更加安全)
  • 前两种模式的本质是通过本地或远程shell,而第3种方式则是让远程主机上运行rsyncd服务,使其监听在一个端口上,等待客户端的连接。

3️⃣ rsync 命令参数解释

rsync #命令
[OPTION...] #选项
SRC... #远程主机模块(不是目录)
[USER@] #远程主机用户(虚拟用户)
HOST:: #远程主机地址
[DEST] #将远程主机模块备份至本地什么位置
-a #归档模式传输, 等于-tropgDl
-v #详细模式输出, 打印速率, 文件数量等
-z #传输时进行压缩以提高效率
-r #递归传输目录及子目录,即目录下得所有目录都同样传输。
-t #保持文件时间信息
-o #保持文件属主信息
-p #保持文件权限
-g #保持文件属组信息
-l #保留软连接
-P #显示同步的过程及传输时的进度等信息
-D #保持设备文件信息
-L #保留软连接指向的目标文件
-e #使用的信道协议,指定替代 rsh 的 shell 程序
--exclude=PATTERN #指定排除不需要传输的文件模式
--exclude-from=file #文件名所在的目录文件
--bwlimit=100 #限速传输
--delete #让目标目录和源目录数据保持一致

4️⃣ rsync 后台服务模式-服务端配置

1️⃣ 安装rsync
yum - y install rsync
2️⃣ 修改配置文件

uid = root                         # 用户UID
gid = root
use chroot = no                    # 禁锢推送的数据至某个目录, 不允许跳出该目录
max connetctions = 0               # 最大连接数,为0不限制
ignore errors                      # 忽略错误信息
exlude = lost+found/               # 排除掉这两个目录的文件
read only = no                     # 设置rsync服务端文件为读写权限
timeout = 600                      # 超时时长
list = false                       # 不显示rsync服务端资源列表
auth users = rsyncuser             # 定义虚拟用户,作为连接认证用户
secrets file = /etc/rsync.passwd   # 虚拟用户密码文件
log file = /var/log/rsyncd.log     # 日志文件启动rsync后自动产生这个文件,无需提前创建
pid file = /var/run/rsyncd.pid     # pid文件
lock file = /var/run/rsyncd.lock   # 支持max connections参数的锁文件
host allow = 172.20.54.0/24        # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
[backup]                           # 名称自定义
path = /backup                     # 服务端备份数据存放目录
comment = welcome to test backup!  # 描述信息

3️⃣ 服务端准备其他文件

/ 创建备份数据存放目录
mkdir /backup
/ 准备用户验证密码文件
echo "rsyncuser:centos" > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd

/ 启动rsync 服务
systemctl start rsyncd
[root@Centos7 ~]$ss -ntlp
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      5             *:873                       *:*                   
users:(("rsync",pid=2216,fd=3))

5️⃣ rsync 后台服务模式-客户端配置

/ 安装rsync服务
yum -y install rsync

/ 配置密码文件
echo "centos" > /etc/rsync.passwd
chomd 600 /etc/rsync.passwd

6️⃣ rsync 客户端测试

  • 启动同步命令
    rsync -avz --delete --password-file=/etc/rsync.passwd /data/ [email protected]::backup
    在这里插入图片描述
  • 创建inotify_rsync.sh脚本,使用inotify+rsync组合
#!/bin/bash
SRC='/data/'             # 只同步目录内容
DEST='[email protected]::backup'
/usr/bin/inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w%f%e' -e \
create,delete,moved_to,close_write,attrib ${SRC} | while read file;do \
/usr/bin/rsync -az --delete --password-file=/etc/rsync.passwd ${SRC} ${DEST} 
echo "${file} was backuped " >> /var/log/rsync.log
done

在这里插入图片描述

  • 把脚本加入开启启动,并后台运行
vim /etc/rc.local
nohup sh /root/inotify_rsync.sh &
  • 查看日志文件
    在这里插入图片描述

发布了107 篇原创文章 · 获赞 20 · 访问量 6369
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章