Rsync+Inotify 实时同步化场景部署

一、rsync(Remote Sync,远程同步)

(1)rsync概述

是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。

在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步源负责提供文件的原始位置,发起端应对该位置具有读取权限。

(2)rsync特性

  • 可以镜像保存整个目录树和文件系统。
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
  • 无须特殊权限即可安装。
  • 优化的流程,文件传输效率高。
  • 可以使用rcp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
  • 支持匿名传输。

(3)rsync使用参数

格式: rsync -参数 用户名@同步服务器的IP::本地存放路径
 / :  rsync://用户名@主机地址/共享模块名
-z 传输时压缩
-P 传输进度
-v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档
-v, --verbose 详细模式输出
-q, --quiet 精简输出模式
-c, --checksum 打开校验开关,强制对文件传输进行校验
-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD
-r, --recursive 对子目录以递归模式处理
-R, --relative 使用相对路径信息
-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。
--backup-dir 将备份文件(如~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀
-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)
-l, --links 保留软链结
-L, --copy-links 像对待常规文件一样处理软链接
--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链接
--safe-links 忽略指向SRC路径目录树以外的链接
-H, --hard-links 保留硬链接
-p, --perms 保持文件权限
-o, --owner 保持文件属主信息
-g, --group 保持文件属组信息
-D, --devices 保持设备文件信息
-t, --times 保持文件时间信息
-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间
-n, --dry-run显示哪些文件将被传输
-W, --whole-file 拷贝文件,不进行增量检测
-x, --one-file-system 不要跨越文件系统边界
-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节
-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息
-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件
--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件
--delete 删除那些DST中SRC没有的文件
--delete-excluded 同样删除接收端那些被该选项指定排除的文件
--delete-after 传输结束以后再删除
--ignore-errors 即使出现IO错误也进行删除
--max-delete=NUM 最多删除NUM个文件
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输
--force 强制删除目录,即使不为空
--numeric-ids 不将数字的用户和组ID匹配为用户名和组名
--timeout=TIME IP超时时间,单位为秒
-I, --ignore-times 不跳过那些有同样的时间和长度的文件
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0
-T --temp-dir=DIR 在DIR中创建临时文件
--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份
-P 等同于 --partial
--progress 显示备份过程
-z, --compress 对备份的文件在传输时进行压缩处理
--exclude=PATTERN 指定排除不需要传输的文件模式
--include=PATTERN 指定不排除而需要传输的文件模式
--exclude-from=FILE 排除FILE中指定模式的文件
--include-from=FILE 不排除FILE指定模式匹配的文件
--version 打印版本信息
--address 绑定到特定的地址
--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件
--port=PORT 指定其他的rsync服务端口
--blocking-io 对远程shell使用阻塞IO
-stats 给出某些文件的传输状态
--progress 在传输时显示传输过程
--log-format=formAT 指定日志文件格式
--password-file=FILE 从FILE中得到密码
--bwlimit=KBPS 限制I/O带宽,KBytes per second
-h, --help 显示帮助信息

二、Inotify监控

(1)inotify概述

inotify是一种强大的、细颗粒的、异步的文件系统监控机制,内核从2.6.13起,加入Inotify可以监控文件系统中添加、删除、修改移动等各种事件,利用这个内核接口,就可以监控文件系统下文件的各种变化情况。

(2)inotifywait 参数

参数名称	                参数说明
-m,–monitor	         始终保持事件监听状态
-r,–recursive	     递归查询目录
-q,–quiet	         只打印监控事件的信息
–excludei	         排除文件或目录时,不区分大小写
-t,–timeout	         超时时间
–timefmt	         指定时间输出格式
–format	             指定时间输出格式
-e,–event	         后面指定删、增、改等事件

(3)inotifywait events 事件说明

事件名称	                 事件说明
access	              读取文件或目录内容
modify	              修改文件或目录内容
attrib	              文件或目录的属性改变
close_write	          修改真实文件内容
close_nowrite	
close	
open	              文件或目录被打开
moved_to	          文件或目录移动到
moved_from	          文件或目录从移动
move	              移动文件或目录移动到监视目录
create	              在监视目录下创建文件或目录
delete	              删除监视目录下的文件或目录
delete_self	
unmount	              卸载文件系统

(4)优化 Inotify

在/proc/sys/fs/inotify目录下有三个文件,对inotify机制有一定的限制

[root@web ~]# ll /proc/sys/fs/inotify/
总用量0
-rw-r--r--1 root root 09月923:36 max_queued_events
-rw-r--r--1 root root 09月923:36 max_user_instances
-rw-r--r--1 root root 09月923:36 max_user_watches
[root@web ~]# ll /proc/sys/fs/inotify/
总用量0
-rw-r--r--1 root root 09月923:36 max_queued_events
-rw-r--r--1 root root 09月923:36 max_user_instances
-rw-r--r--1 root root 09月923:36 max_user_watches
max_user_watches      设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
max_user_instances    设置每个用户可以运行的inotifywait或inotifywatch命令的进程数
max_queued_events     设置inotify实例事件(event)队列可容纳的事件数量

三、Rsync下行同步

  • 实验环境
主机 IP 软件
Master 192.168.90.10 rsync
Slave 192.168.90.20 rsync / inotify-tools-3.14.tar.gz

下行同步:将master服务器数据备份到slave服务器

(1)Master (192.168.90.10)

systemctl stop firewalld.service 
setenforce 0

yum -y install httpd rsync 

vim /etc/rsyncd.conf 

uid = nobody
gid = nobody
use chroot = yes
#禁锢在源目录                                                
address = 192.168.90.10
#监听地址
port 873                 
#监听端口 tcp/udp                                                
log file = /var/log/rsyncd.log        
#日志文件位置          
pid file = /var/run/rsyncd.pid   
#存放进程 ID 的文件位置               
hosts allow = 192.168.90.0/24
#允许访问的客户机地址
[wwwroot]    
#共享模块名称                                                           
path = /var/www/html   
#源目录的实际路径                                 
comment = Document Root of www.benet.com
read only = yes       
#是否为只读                                           
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z    
#同步时不再压缩的文件类型    
auth users = popo         
#授权账户,多个账号以空格分隔                                      
secrets file = /etc/rsyncd_users.db  
#存放账户信息的数据文件           

在这里插入图片描述

vim /etc/rsyncd_users.db
popo:123123

chmod 600 /etc/rsyncd_users.db

rsync --daemon
以独立监听服务的方式(守护进程)运行

netstat -natp | grep rsync

cd /var/www/html
touch a.html b.html
ls

在这里插入图片描述
在这里插入图片描述

(2)Slave(192.168.90.20)

systemctl stop firewalld.service 
setenforce 0

yum -y install rsync

cd /opt
mkdir dodo
chmod 777 dodo

在这里插入图片描述

免交互密码文件:server.pass

vim /etc/server.pass
123123

chmod 600 /etc/server.pass 

rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/dodo

ls dodo

在这里插入图片描述

四、Rsync+Inotify实验搭建

(1)Master(192.168.90.10)

vim /etc/rsyncd.conf
read only = no
#关闭只读,上行同步需要可以写

kill `cat /var/run/rsyncd.pid`
netstat -natp | grep rsync

rsync --daemon
netstat -natp | grep rsync
 
chmod 777 /var/www/html

在这里插入图片描述

(2)Slave(192.168.90.20)

cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances 
cat /proc/sys/fs/inotify/max_user_watches 

vim /etc/sysctl.conf 

fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

sysctl -p

在这里插入图片描述

yum -y install gcc gcc-c++ 

#放入安装包
tar zxvf inotify-tools-3.14.tar.gz -C /opt


cd /opt/inotify-tools-3.14/

./configure
make && make install

编写一个持续化监控脚本

vim /opt/jk.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/dodo/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/dodo/ [email protected]::wwwroot"
 
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
	fi
done

在这里插入图片描述

cd /opt/
chmod +x jk.sh 
./jk.sh 

cd /opt/dodo
touch c.html
rm -rf a.html

在这里插入图片描述

Master验证结果

cd /var/www/html
ls

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_51468875/article/details/114276108