rsync远程同步+inotify

  • 一、基本理论概述
    • 1.1 前言概述
    • 1.2 rsync服务器概述
    • 1.3 rsync同步方式
  • 二、实验操作
    • 2.1 rsync本地复制
    • 2.2 rsync服务器和客户端
      • 2.2.1 配置rsync源服务器
      • 2.2.2 发起端
      • 2.2.3 免交互格式配置
      • 2.2.4 周期性计划任务
  • 三、Inotify基本概述
  • 四、实验操作
    • 4.1 修改rsync源服务器配置文件
    • 4.2 调整inotify内核参数(优化)
    • 4.3 安装inotify-tools
    • 4.4 执行“inotifywait”命令,并另开个终端测试
    • 4.5 在rsync client编写触发式同步脚本
    • 4.6 测试脚本执行情况

一、基本理论概述

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.txtsent 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 5月  19 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.txtsent 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.txtsent 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.txtsent 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 周期性计划任务

【每天的0点5分执行同步任务】
[root@client /]# crontab -e
no crontab for root - using an empty one5 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                  【持续监控(遍历目录,增删改文件)】
doif [ $(pgrep rsync | wc -l) -le 0 ] ; then                  【如果服务未启动,执行同步】$RSYNC_CMDfi
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

rsync远程同步+inotify监控相关推荐

  1. Rsync远程同步+inotify监控实时同步概述,部署

    文章目录 一,rsync 概述 1.rsync服务器 2.rsync同步方式 3.rsync特性 二.rsync与cp.scp对比 三,rsync命令 四,rsync 本地复制实例 五,rsync同步 ...

  2. 如何实现rsync远程同步和inotify实时同步

    目录 一:rsync介绍 1.1:rsync 服务的模式 1.2:rsync服务原理 1.3:配置rsync源思路 二.搭建rsync服务 2.1:建立rsync配置文件 2.2:客户机服务器B测试 ...

  3. rsync远程同步及结合inotify实现实时同步

    rsync远程同步及结合inotify实现实时同步 rsync 命令格式 常用选项 实现免交互 rsync同步配置 环境准备 同步源编写rsync配置文件 创建数据文件 确保读取权限 开启服务 sla ...

  4. rsync远程同步和inotify实时同步

    目录 引言 一.rsync远程同步 1.Rsync介绍 2.同步方式 3.增量备份 4

  5. rsync远程同步的基本配置与使用

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

  6. Rsync下行同步+inotify实时同步介绍和部署

    Rsync下行同步+inotify实时同步介绍和部署 一.Rsync 1.Rsync同步源 2.Rsync服务器 3.同步方式 二.配置rsync源 三.rsync命令 四.配置源的两种表示方法 1. ...

  7. rsync 远程同步

    rsync 远程同步 前言 一.Rsync简介 (1)rsync介绍 (2)rsync同步方式 二.rsync同步源 (1)配置rsync源 ①基本思路 ②配置文件rsync.conf ③独立的账号密 ...

  8. 快速入门rsync远程同步

    文章目录 一.rsync同步简介 二.配置rsync源服务器 三.rsync远程同步实验 3.1实验环境 3.2实验步骤 四.rsync实时同步 4.1为什么要实时同步 4.2关于inotify 4. ...

  9. rsync 远程同步部署 上行下行同步

    rsync远程同步 一.rsync (Remote Sync,远程同步) 二.部署rsync源服务器 三.发起端 四.发起端配置 rsync+inotify 一.rsync (Remote Sync, ...

最新文章

  1. 2022-2028年中国互联网+不良资产处置行业深度调研及投资前景预测报告
  2. 逻辑模型设计步骤-粒度层次划分
  3. R语言ggplot2可视化使用vjust和hjust参数对齐图像中的文本注释信息(左对齐、右对齐、居中)实战
  4. AngularJS学习笔记(二) 表单验证案例(ng-repeat/filter)
  5. python3.7和3.8的区别-python3.8.0与3.7.0哪个好?
  6. Oracle用户管理学习总结
  7. gis根据范围批量分开图斑_基于BIM-GIS技术的公路预防性养护研究
  8. WPS for MacOS如何设置自动句首字母大写
  9. 杰和科技多款商显方案亮相2017英特尔RCA论坛
  10. c语言api_C语言现在好找工作吗?我开始学了这么久的C语言,应该怎么办?
  11. 181223每日一句
  12. Kafka从上手到实践-Zookeeper CLI:CRUD zNode | 凌云时刻
  13. 一次idea上使用logback+slf4j乱码的解决,问题不在logback
  14. 6条开会清单,教你组织一场高效会议!
  15. 百度地图-根据起终点经纬度驾车导航
  16. 鸟哥的Linux私房菜基础学习篇 第2章的重点探索
  17. Linux下tftp服务器/客户端安装
  18. 怎么调出全局搜索_eclipse全局搜索快捷键是什么
  19. 微信小程序:校验真实姓名和身份证号
  20. ue4 开发动作游戏_ue4游戏开发虚幻4学习教程资源素材合集

热门文章

  1. 中原建业冲刺上市的喜与忧:外拓小有成就,增长却逐渐放缓
  2. 秀一下大连TimesTen 18.1培训的结业证
  3. 弦线上的波动方程推导
  4. github上十二款最著名的Android播放器开源项目
  5. 阅读笔记——软件工程的瀑布、教堂和集市
  6. excel如何将数字由文字格式转换为数字格式
  7. Codeforces 577B
  8. Ajax 完整教程-(三)
  9. Android手机之间不消耗流量互传文件
  10. 5000词学英语——DAY7