rsync远程同步+inotify监控

一、基本理论概述

1.1 前言概述

  • rsync是一款快速增量备份工具(Remote Sync 远程同步)
  • 支持本地复制或者与其他SSH、rsync主机同步

1.2 rsync服务器概述

  • 1.rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具,并且可以不进行改变原有数据的属性信息来实现数据的备份迁移特性
  • 2.rsync软件适用于unix/linux/windows等多种操作系统平台
  • 3.rsync是一个快速和非常通用的文件复制工具。能进行本地复制,远程复制或者远程守护进程方式复制。它提供了大量的参数来控制其行为的各个方面,并且允许以非常灵活的方式来实现文件的传输复制
  • 4.以其delta-transfer算法闻名。rsync监听873端口,允许模式:C/S

1.3 rsync同步方式

  • 1.全量备份
    • 把所有数据全部传送
    • 把原来的文件和新的文件一起统一传送
    • 全量复制,效率低
  • 2.增量备份
    • 在除数数据之前使用一些算法来对比A主机的数据和B主机的数据,把不一样的数据通过网络传输
    • 增量复制,效率高
  • 3.本地复制
    • 类似于cp命令,将qz目录及里面的文件归档、压缩拷贝到/opt目录里,并显示详细的信息
    • 基本格式:rsync [选项] 原始位置 目标位置
    • 常用选项如下表
选项说明
-r递归模式。包含目录及子目录中的所有文件
-l对于符号链接的文件仍然复制为符号链接文件
-v显示同步过程的详细信息
-z在传输文件时进行压缩
-a归档模式,保留文件的权限、属性等信息。等同于组合选项“-rlptgoD”
-p保留文件的权限标记
-t保留文件的时间标记
-g保留文件的属组标记(仅超级用户使用)
-o保留文件的属主标记(仅超级用户使用)
-H保留硬链接文件
-A保留ACL属性信息
-D保留设备文件及其特殊文件
–delete删除目标位置有,但是原始位置没有的文件
–checksum根据校验和(而不是文件大小、修改时间)来决定是否跳过文件

二、实验操作

2.1 rsync本地复制

[root@localhost ~]# mkdir /qz
[root@localhost ~]# cd /qz/
[root@localhost qz]# touch q.txt z.txt  
[root@localhost qz]# ls
q.txt  z.txt
[root@localhost qz]# cd /opt/
[root@localhost opt]# ls
rh
[root@localhost opt]# cd -
/qz
[root@localhost qz]# rsync -avz /qz/ /opt/  【a:归档模式;z:在传输文件时进行压缩;v:显示同步过程的详细信息】
sending incremental file list
./
q.txt
z.txt

sent 125 bytes  received 53 bytes  356.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost qz]# cd /opt/
[root@localhost opt]# ls
q.txt  rh  z.txt
[root@localhost opt]# rm -rf /qz/
[root@localhost opt]# rsync -avz /opt/ /qz/
sending incremental file list
created directory /qz
./
q.txt
z.txt
rh/

sent 151 bytes  received 57 bytes  416.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost opt]# cd /qz/
[root@localhost qz]# ls
q.txt  rh  z.txt

2.2 rsync服务器和客户端

  • rsync:192.168.131.10
  • client:192.168.131.11

2.2.1 配置rsync源服务器

[root@rsync /]# systemctl stop firewalld.service 
[root@rsync /]# setenforce 0
[root@rsync /]# systemctl disable firewalld.service 
[root@rsync /]# yum -y install rsync
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
软件包 rsync-3.0.9-18.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@rsync /]# rpm -q rsync                     【一般系统已默认安装rsync】
rsync-3.0.9-18.el7.x86_64
[root@rsync /]# vim /etc/rsyncd.conf             【将原有配置删除,重新添加】

uid = nobody                                     【用户ID】
gid = nobody                                     【组ID】
use chroot = yes                                 【禁锢在源目录】
address = 192.168.131.10                         【监听地址】
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.131.0/24                   【允许访问的客户机地址(可为网段)】
[wwwroot]                                        【共享模块名称】
path = /var/www/html                             【源目录的实际路径】
comment = Document Root of www.qz.com
read only = yes                                  【是否为只读】
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z 【同步时不再压缩的文件类型】
auth users = qz                                  【授权账号,多个账号以空格分隔】
secrets file = /etc/rsyncd_users.db              【存放账户信息的数据文件】
【如果采用匿名的方式,只要将“auth users”和“secrets file”配置项去掉即可】
[root@rsync /]# vim /etc/user.db                 【编辑用户账号文件】

qz:5514                                          【格式:名称:密码,一行一个】
[root@rsync /]# chmod 600 /etc/user.db           【官方要求赋权600,否则将报错】


[root@rsync /]# mkdir -p /var/www/html           【这里不按照apache,直接创建源目录】
[root@rsync /]# chmod +r /var/www/html/          【给源目录赋予可读权限】
[root@rsync /]# ls -ld /var/www/html/             
drwxr-xr-x. 2 root root 6 519 20:13 /var/www/html/


[root@rsync /]# rsync --daemon                   【启动rsync程序。以独立监听服务的方式(守护进程)运行】
[root@rsync /]# netstat -natp | grep rsync
tcp        0      0 192.168.131.10:873      0.0.0.0:*               LISTEN      44388/rsync
[root@rsync /]# cat /var/run/rsyncd.pid 
44388
[root@rsync /]# kill $(cat /var/run/rsyncd.pid)  【关闭rsync服务的话只需直接杀死pid号即可】
[root@rsync /]# netstat -natp | grep rsync

2.2.2 发起端

  • 格式:rsync [选项] 原始位置 目标位置
【先在rsync端创建源目录/文件用于同步实验】
[root@rsync html]# echo "this is rsync" >> q.txt
[root@rsync html]# echo "this is rsync2" >> q2.txt
[root@rsync html]# ls
q2.txt  q.txt

【同步格式一】
[root@client ~]# systemctl stop firewalld.service 
[root@client ~]# setenforce 0
[root@client ~]# systemctl disable firewalld.service 
[root@client /]# rsync -avz qz@192.168.131.10::wwwroot /qz/
Password: 
receiving incremental file list
./
q.txt

sent 77 bytes  received 164 bytes  160.67 bytes/sec
total size is 14  speedup is 0.06
[root@client /]# cat /qz/q.txt            【查看,验证同步结果】
this is rsync



【同步格式二】
[root@client /]# rsync -vaz rsync://qz@192.168.131.10/wwwroot /qz/
Password: 
receiving incremental file list
./
q2.txt

sent 77 bytes  received 184 bytes  104.40 bytes/sec
total size is 29  speedup is 0.11
[root@client /]# cat /qz/q2.txt 
this is rsync2

2.2.3 免交互格式配置

【rsync端创建文件用于测试】
[root@rsync html]# echo "this is rsync3" >> q3.txt
[root@rsync html]# ls
q2.txt  q3.txt  q.txt

【client端】
[root@client /]# echo "5514" > /etc/server.pass
[root@client /]# cat /etc/server.pass 
5514
[root@client /]# chmod 600 /etc/server.pass 
[root@client /]# rsync -zva --password-file=/etc/server.pass qz@192.168.131.10::wwwroot /qz/
receiving incremental file list
./
q3.txt

sent 77 bytes  received 199 bytes  552.00 bytes/sec
total size is 44  speedup is 0.16  
[root@client /]# cat /qz/q3.txt 
this is rsync3

2.2.4 周期性计划任务

【每天的05分执行同步任务】
[root@client /]# crontab -e
no crontab for root - using an empty one

5 0 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass qz@192.168.131.10::wwwroot /qz/
[root@client /]# systemctl restart crond.service 

三、Inotify基本概述

  • 使用inotify通知接口,可以用来监控文件系统的各种变化情况(例如:文件存取、删除、移动、修改等等)。利用这一机制,可以非常方便的实现文件异动告警、增量备份,并针对目录或文件的变化即使作出相应
  • 将inotify机制与rsync工具相结合,可以实现触发式备份(实时同步)。也就是说只要原始位置的文档发生变化,则立即启动增量备份操作;没有发生变化时则处于静默等待状态
  • 因为inotify通知机制由Linux内核提供,所以在触发式备份应用时更适合上行同步

四、实验操作

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

[root@rsync html]# vim /etc/rsyncd.conf
read only = no                             【关闭只读,因为上行同步需要可以写操作】
[root@rsync html]# kill $(cat /var/run/rsyncd.pid)
[root@rsync html]# rsync --daemon
[root@rsync html]# netstat -natp | grep rsync
tcp        0      0 192.168.131.10:873      0.0.0.0:*               LISTEN      45146/rsync 

4.2 调整inotify内核参数(优化)

  • 在linux内核中,默认的inotify机制提供了三个调控参数
    • 1.max_queue_events(监控事件队列,默认值为16384)
    • 2.max_user_instances(最多监控实例数,默认值128)
    • 3.max_user_watches(每个实例最多监控文件数,默认值8192)
  • 当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值
[root@rsync html]# cat /proc/sys/fs/inotify/max_queued_events 
16384
[root@rsync html]# cat /proc/sys/fs/inotify/max_user_instances 
128
[root@rsync html]# cat /proc/sys/fs/inotify/max_user_watches 
8192
[root@rsync html]# vim /etc/sysctl.conf   【加大参数的值,做inotify内核参数优化】
fs.inotify.max_queued_events = 22768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 148576
[root@rsync html]# sysctl -p              【加载内核参数配置文件,使其生效】
fs.inotify.max_queued_events = 22768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 148576

4.3 安装inotify-tools

  • 用inotify机制还需要安装inotify-tools,以便提供inotifywait、inotifywatch辅助工具程序
    • inotifywait:用于持续监控,实时输出结果。可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等等各种事件,一旦有变动立即输出结果
    • inotifywatch:用于短期监控,任务完成后再输出结果。可用来手机文件系统变动情况,并在运行结束后输出汇总的变化情况
[root@rsync html]# yum -y install gcc gcc-c++ make        【安装依赖环境】
[root@rsync opt]# ls
inotify-tools-3.14.tar.gz  q.txt  rh  z.txt
[root@rsync opt]# tar zxvf inotify-tools-3.14.tar.gz      【解压安装包】
[root@rsync opt]# cd inotify-tools-3.14/
[root@rsync inotify-tools-3.14]# ./configure 
[root@rsync inotify-tools-3.14]# make -j2 & make install

4.4 执行“inotifywait”命令,并另开个终端测试

在这里插入图片描述

选项说明
-e指定要监控哪些事件
-m表示持续监控
-r表示递归整个目录
-q简化输出信息
modify修改
delete删除
create创建
move移动

4.5 在rsync client编写触发式同步脚本

[root@client html]# yum -y install gcc gcc-c++ make        
[root@client opt]# ls
inotify-tools-3.14.tar.gz  q.txt  rh  z.txt
[root@client opt]# tar zxvf inotify-tools-3.14.tar.gz      
[root@client opt]# cd inotify-tools-3.14/
[root@client inotify-tools-3.14]# ./configure 
[root@client inotify-tools-3.14]# make -j2 & make install


[root@client opt]# vim /opt/inotify.sh                           【编辑自动执行脚本】
#!/bin/bash
【INOTIFY_CMD变量=持续监控/qz/目录中的创建,删除,移动,修改和改变事件】
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /qz/"


【RSYNC_CMD变量=使用qz用户,并在/etc/server.pass文件里获取秘钥文件。将/qz/目录下的文件进行压缩,归档,保留硬链接文件,
并同步至192.168.131.10的共享源目录下(/var/www/html),并且删除差异内容】
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /qz/ qz@192.168.131.10::wwwroot"

$INOTIFY_CMD | while read DIRECTORY EVENT FILE                  【持续监控(遍历目录,增删改文件)】
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then                  【如果服务未启动,执行同步】
        $RSYNC_CMD
        fi
done


[root@client opt]# chmod +x /opt/inotify.sh
[root@client opt]# chmod 777 /qz/
[root@client opt]# chmod +x /etc/rc.d/rc.local 
[root@client opt]# echo '/opt/inotify.sh' >> /etc/rc.d/rc.local 【加入开机自启动】
[root@client opt]# sh -x inotify.sh                             【执行脚本】

4.6 测试脚本执行情况

[root@rsync html]# vim /etc/rsyncd.conf 

uid = root
gid = root

在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TaKe___Easy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值