Rsync 下行同步 + Inotify 实时同步详解


一、rsync 同步简介

1. 关于 rsync

img
  rsync(Remote Sync,远程同步)是一款开源的快速增量备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
支持本地复制,或者与其他 SSH、rsync 主机同步。
官方网站:http://rsync.samba.org

2. rsync 同步源(备份源)

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

二、配置 rsync 备份源

1. 关闭防火墙

systemctl stop firewalld && systemctl disable firewalld
setenforce 0

2. 查看 rsync 是否已安装

一般系统已默认安装 rsync

rpm -q rsync

3. 建立 /etc/rsync.conf 配置文件

vim /etc/rsyncd.conf      		#添加以下配置

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

如采用匿名的方式,只要将其中的 auth users 和 secrets file 配置去掉即可。

4. 为备份账户创建数据文件

vim /etc/rsyncd_users.db
backuper:123456		#无须建立同名系统用户
 
chmod 600 /etc/rsyncd_users.db	#必须授权 600

5. 保证所有用户对源目录 /var/www/html 都有读取权限

chmod +r /var/www/html
ls -ld /var/www/html

6. 启动 rsync 服务程序

rsync --daemon	#启动 rsync 服务,以独立监听服务的方式(守护进程)运行
netstat -antp | grep rsync   

7. 关闭 rsync 服务

kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid
netstat -natp | grep rsync

8. 编写测试网页

echo "this is rsync test!" > /var/www/html/test.html 

三、rsync 命令基本用法

1. 基本格式

rsync [选项] 原始位置 目标位置

2. 常用选项

常用选项 说明
-r 递归模式,包含目录及子目录中的所有文件
-l 对于符号链接文件仍然复制为符号链接文件
-v 显示同步过程的详细(verbose)信息
-z 在传输文件时进行压缩(compress)
-a 归档模式,保留文件的权限、属性等信息,等同于组合选项 “-rlptgoD”
-p 保留文件的权限标记
-t 保留文件的时间标记
-g 保留文件的属组标记(仅超级用户使用)
-o 保留文件的属主标记(仅超级用户使用)
-H 保留硬链接文件
-A 保留ACL属性信息
-D 保留设备文件及其他特殊文件
--delete 删除目标位置有而原始位置没有的文件
--checksum 根据对象的校验和来决定是否跳过文件

四、配置发起端(backuper)

1. 环境准备

hostnamectl set-hostname backuper
su
systemctl stop firewalld && systemctl disable firewalld
setenforce 0

2. 查看 rsync 是否已安装,一般系统已默认安装 rsync

rpm -q rsync

3. 将指定的资源下载到本地 /opt 目录下进行备份

格式一:

rsync -avz [email protected]::rsync /var/www/html
yum -y install httpd && systemctl start httpd
curl http://192.168.10.30/test.html

格式二:

rm -rf /var/www/html/*
rsync -avz rsync://[email protected]/rsync /var/www/html
curl http://192.168.10.30/test.html

4. 免交互格式配置

echo "123456" > /etc/server.pass
chmod 600 /etc/server.pass
rm -rf /var/www/html/*
rsync -avz --password-file=/etc/server.pass [email protected]::rsync /var/www/html
curl http://192.168.10.30/test.html

5. 计划性定时同步

crontab -e 
0 1 * * * /usr/bin/rsync/ -az --delete --password-file=/etc/server.pass [email protected]::rsync /var/www/html 

systemctl restart crond && systemctl enable crond

五、rsync 实时同步

1. 定期同步的不足

  • 执行备份的时间固定,延迟明显、实时性差
  • 当同步源长期不变化时,密集的定期任务是不必要的

2. 实时同步的优点

  • 一旦同步源出现变化,立即启动备份
  • 只要同步源无变化,则不执行备份

3. Linux 内核的 inotify 机制

从版本 2.6.13 开始提供,可以监控文件系统的变动情况,并作出通知响应。

辅助软件:inotify-tools
在这里插入图片描述

4. 发起端配置 rsync + inotify

  使用 inotiify 通知接口,可以用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。
  将 inotify 机制与 rsync 工具相结合,可以实现触发式备份(实时同步),即只要原始位置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
  因为 inotify 通知机制由 Linux 内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步。

4.1 修改 rsync 源服务器配置文件(rsync)

vim /etc/rsyncd.conf  
#修改 read only 为 no 
read only = no 

kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid
rsync --daemon
netstat -natp | grep rsync
chmod 777 /var/www/html

4.2 调整 inotify 内核参数(backuper)

在 Linux 内核中,默认的 inotify 机制提供了三个调控参数:

max_queued_events	监控事件队列,默认值为 16384
max_user_instances	最多监控实例数,默认值为 128
max_user_watches	每个实例最多监控文件数,默认值为 8192

当要监控的目录、文件数较多或者变化较频繁时,建议加大这三个参数的值

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

可通过 /etc/sysctl.conf 文件进行配置,设定参数应符合服务器性能以及服务需要

[root@backuper ~]# vim /etc/sysctl.conf
#添加如下配置
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576 

sysctl -p
cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches

4.3 安装 inotify-tools(backuper)

用 inotify 机制需要安装 inotify-tools,以便提供 inotifywait、inotifywatch 辅助工具程序,用来监控、汇总改动情况。

  • inotifywait:可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一旦变动立即输出结果。
  • inotifywatch:可用来收集文件系统变动情况,并在运行结果后输出汇总的变化情况。
[root@backuper ~]# cd /opt
[root@backuper opt]# rz -E
#传入 inotify-tools 安装包
rz waiting to receive.
[root@backuper opt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt
[root@backuper opt]# cd inotify-tools-3.14/
[root@backuper inotify-tools-3.14]# ./configure
[root@backuper inotify-tools-3.14]# make -j 2 && make install

可以先执行 inotifywait 命令,然后另外再开启一个新终端向 /var/www/html 目录下添加文件、移动文件,在原来的终端上跟踪屏幕输出结果。

新终端操作:

[root@backuper ~]# cd /var/www/html
[root@backuper html]# touch test.php
[root@backuper html]# mv test.php test.txt
[root@backuper html]# echo 'test' > test.txt
[root@backuper html]# rm -rf test.txt 

原终端监控:

[root@backuper inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html
/var/www/html/ CREATE test.php
/var/www/html/ MOVED_FROM test.php
/var/www/html/ MOVED_TO test.txt
/var/www/html/ MODIFY test.txt
/var/www/html/ DELETE test.txt
inotifywait 常用选项 说明
-e 用来指定要监控哪些事件
-m 表示持续监控
-r 表示递归整个目录
-q 简化输出信息

4.4 在另一个终端编写触发式同步脚本(backuper)

[root@backuper html]# vim /opt/inotify.sh

#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ [email protected]::rsync/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
##while判断是否接收到监控记录
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done

[root@backuper opt]# chmod +x inotify.sh 
[root@backuper opt]# chmod 777 /var/www/html/
[root@backuper opt]# chmod +x /etc/rc.d/rc.local 
[root@backuper opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local
##加入开机自动执行

上述脚本用来检测本机 /var/www/html 目录的变动情况,一旦有更新触发 rsync 同步操作,上传备份至服务器 192.168.10.30 的 rsync 共享目录下。

4.5 验证

触发式上行同步的验证过程如下:

  1. 在本机运行 /opt/inotify_rsync.sh 脚本程序
[root@backuper opt]# ./inotify.sh &
[1] 5786
  1. 切换到本机的 /var/www/html 目录,执行增加、删除、修改文件等操作
[root@backuper opt]# cd /var/www/html
[root@backuper html]# ls
test.html  test.txt
[root@backuper html]# rm -rf *
[root@backuper html]# touch test.html
[root@backuper html]# echo 'this is inotify_rsync test!' > test.html
  1. 查看远端服务器中的 rsync 目录下的变化情况
[root@rsync ~]# cd /var/www/html
[root@rsync html]# ls
test.html
[root@rsync html]# cat test.html 
this is inotify_rsync test!

六、使用 rsync 实现快速删除大量文件

  假如要在 linux下删除大量文件,比如 100 万、1000 万,像 /usr/local/nginx/proxy_temp 的 nginx 缓存等,那么 rm -rf * 可能就不好使了,因为要等待很长一段时间。在这种情况下,我们可以利用 rsync 的 --delete 来巧妙处理。rsync 实际用的是替换原理。

1. 模拟垃圾文件

[root@rsync html]# touch {1..9999}.txt

2. 建立空文件夹

[root@rsync html]# mkdir /opt/test

3. 使用 rsync 进行替换删除

[root@rsync html]# rsync --delete-before -a -H -v --progress --stats /opt/test/ /var/www/html/
······
Number of files: 1
Number of files transferred: 0
Total file size: 0 bytes
Total transferred file size: 0 bytes
Literal data: 0 bytes
Matched data: 0 bytes
File list size: 19
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 29
Total bytes received: 15
 
sent 29 bytes  received 15 bytes  29.33 bytes/sec
total size is 0  speedup is 0.00
--delete-before:接受者在传输前进行删除操作
-a:归档模式,表示以递归方式传输文件,并保持所有文件属性
-H:保持硬链接文件
-v:详细输出模式
--progress:在传输室显示传输过程
--stats:给出某些文件的传输状态

参考

rsync + inotify

https://www.cnblogs.com/dingcong1201/p/15343955.html


本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>