当前位置:网站首页>rsync+inotify远程同步
rsync+inotify远程同步
2022-04-23 14:07:00 【C和弦~】
目录
一.reync
1、rsync服务器
- rsync是一款开源的、快速的、多功能的、可实现全量及增量的本地或远程数据同步备份的优秀工具。并且可以不进行改变原有数据的属性信息,实现数据的备份迁移特性。
- rsync软件适用于unix/linux/windows等多种操作系统平台
- rsync是一个快速和非常同样的文件复制工具。它能本能复制,远程复制,或者远程守护进程方式复制,它提供了大量的参数来控制其行为的各个方面,并且允许非常灵活的方式来实现文件的传输复制
- 以其delta-transfer算法闻名。
- rsync监听端口:873
- rsync运行模式:C/S
2、同步方式
- 【1】全量备份:
- 原有的数据全部传送
- 把原来的文件和新的文件一起统一传送
- 全量复制,效率低
- 【2】增量备份
- 在传输数据之前通过一些算法通过你有的数据和我有的数据进行对比,把不一样的数据通过网络传输
- 增量复制,效率高
3.rsync命令格式及参数
rsync [选项] 原始位置 目标位置
常用选项 说明
-r 递归模式,包含目录及子目录中的所有文件
-l 对于符号链接文件仍然复制为符号链接文件
-v 显示同步过程的详细信息
-z 在传输文件时进行压缩goD
-p 保留文件的权限标记
-a 归档模式,递归并保留对象属性,等同于-rlpt
-t 保留文件的时间标记
-g 保留文件的属组标记(仅超级用户使用)
-o 保留文件的属主标记(仅超级用户使用)
-H 保留硬链接文件
-A 保留ACL属性信息
-D 保留设备文件及其他特殊文件
–-delete 删除目标位置有而原始位置没有的文件
–checksum 根据对象的校验和来决定是否跳过文件
rsync本地复制的两种方式
1.在原始位置后不加/
2.在原始位置后加上/
案例:
不加/:rsync -avz ssl shiyan #ssl后没加上/,表示将ssl目录包含内容复制到shiyan目录中
加/: rsync -avz ssl/ shiyan #ssl后加上/,表示将ssl目录中的内容复制到shiyan目录中
4.配置源的两种表示方法
格式一:
用户名@主机地址::共享模块名
rsync -avz [email protected]::wwwroot /root
格式二:
rsync://用户名@主机地址/共享模块名
rsync -avz rsync://[email protected]/wwwroot /root
5.案例
主机 IP地址
server 192.168。29.55 rsync源服务器
rsync 192.168.29.33 rsync目标服务器
案例部署
#老配方,关闭防火墙。。。。
systemctl stop firewalld.service
setenforce 0
#rsync服务器配置
rpm -q rsync
yum -y install rsync
#修改/etc/rsyncd.conf配置文件
vim /etc/rsyncd.conf
uid = nobody #root
gid = nobody #root
use chroot = yes #禁锢在源目录
address = 192.168.29.33 #监听地址
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.3.0/24 #允许访问的客户机地址 (上行同步中的访问策略)
[wwwroot] ##第一个共享模块
path = /var/www/html #源目录的实际路径
comment = Document Root of www.ljm.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”配置项去掉即可
#来个简洁版
uid = nobody
gid = nobody
use chroot = yes
address = 192.168.29.33
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.29.0/24
[wwwroot]
path = /var/www/html
comment = Document Root of www.ljm.com
read only = yes
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = backuper
secrets file = /etc/user.db
#为备份账户创建数据文件
vim /etc/user.db
backuper:abc123 #无需建立同名系统用户
chmod 600 /etc/user.db
#保证所有用户对源目录 /var/www/html 都有读的权限
mkdir -p /var/www/html
chmod +r /var/www/html/
ls -ld /var/www/html/
#启动 rsync 服务程序
rsync --daemon
netstat -natp | grep rsync
#若要关闭
kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid
#写入一个简单的html
cd /var/www/html
echo "hello" >> 1.txt
server配置
#同步
mkdir /abc
rsync -avz [email protected]::wwwroot /abc/ //同步,然后输入密码
#查看同步
cd /abc
ls
#设置免交互
echo "abc123" > /etc/server.pass
chmod 600 /etc/server.pass
rsync -avz --password-file=/etc/server.pass [email protected]::wwwroot /abc
#此时再去同步就不用输入密码了
二、inotify
1、简介
- 可以监控文件系统的变动情况,并做出通知响应
- 调整inotify内核参数(优化)
- /etc/sysctl.conf(内核参数配置文件)
- inotifywait:用于持续监控,实时输出结果
- inotifywatch:用于短期监控,任务完成后再输出结果
max_queue_events #监控事件队列大小
max_user_instances #最多监控实例数
max_user_watches #每个实例最多监控文件数
2、inotifywait
- 格式 inotifywait [参数]
常见参数 说明
-m 持续进行监控
-r 递归监控所有子对象
-q 简化输出信息
-e 指定要监控哪些事件类型
3.rsync+inotify案例
rsync配置
#1.先关闭rsync进程
kill $(cat /var/run/rsyncd.pid)
netstat -natp | grep rsync
#2.修改rsync配置文件
vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = yes
address = 192.168.29.33
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.29.33/24
[wwwroot]
path = /var/www/html
comment = Document Root of www.ljm.com
read only = no
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = backuper
secrets file = /etc/user.db
#3.启动rsync
rsync --daemon
netstat -natp | grep rsync
#4.配置inotify内核参数
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 32768 #监控时间队列,默认为16384
fs.inotify.max_user_instances = 1024 #最多监控实例数,默认为128
fs.inotify.max_user_watches = 1048576 #每个实例最多监控文件数,默认为8192
#当要监控的目录、文件数据量较多或者变化频繁时,建议加大参数值
sysctl -p //刷新
- server配置
#1.安装inotify
yum -y install gcc gcc-c++ //安装依赖
#把包拖到/opt目录下
tar zxvf inotify-tools-3.14.tar.gz -C /opt
#2.编译安装
cd /opt/inotify-tools-3.14/
./configure
make -j 4 && make install
#3.监控 /abc 下,添加文件、移动文件等操作,并跟踪屏幕输出结果
inotifywait -mrq -e modify,create,move,delete /abc
#然后另开一个终端,进行 创建、删除、重命名 等操作,查看变化
touch 4.txt
touch 5.txt
rm -rf 4.txt
mv 5.txt 6.txt
结合shell脚本部署
#1.脚本
vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /abc/"
RSYNC_CMD="rsync -apzH --delete --password-file=/etc/server.pass /abc/ [email protected]::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ]; then
$RSYNC_CMD
fi
done
#2.加权
chmod +x /opt/inotify.sh
chmod +x /etc/rc.d/rc.local
echo "/opt/inotify.sh" >> /etc/rc.d/rc.local
#3.测试
cd /abc
touch 1.txt
#再回到rsync看,发现已经同步过去
总结
-
rsync 是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,并保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。
版权声明
本文为[C和弦~]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Ab960311/article/details/121368178
边栏推荐
- 全局变量能否放在头文件中定义
- Logging模块
- 微信小程序与低功耗蓝牙通信-接受硬件端发送来的数据(四)
- jsp学习3
- PySide2
- POI operation word template replaces data and exports word
- Detailed tutorial on the use of setinterval timing function of wechat applet
- Recyclerview advanced use (I) - simple implementation of sideslip deletion
- 使用Postman进行Mock测试
- 查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
猜你喜欢
How QT designer adds resource files
基于ibeacons签到系统
RobotFramework 之 用例标签机制
Mock测试
Chapter I review of e-commerce spike products
Subscription number development of wechat applet (message push)
Detailed tutorial on the use of setinterval timing function of wechat applet
RobotFramework 之 文件上传和下载
云迁移的六大场景
基于CM管理的CDH6.3.2集群集成Atlas2.1.0
随机推荐
leetcode--977. Squares of a Sorted Array
查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
Mock测试
Nifi 快速安装及文件同步操作
jacob打印word
星界边境Starbound创意工坊订阅的mod的存放路径
JSP学习2
基于ibeacons签到系统
ActiveMq基础知识
poi操作word模板替换数据并且导出word
可否把模板的头文件和源文件分开编译
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
微信小程序基于udp协议与esp8266进行通信
POI operation word template replaces data and exports word
星界边境文本自动翻译机(高级版)使用说明
pthread_ Why does self() repeat
容灾有疑问?点这里
As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture
Detailed tutorial on the use of setinterval timing function of wechat applet
金融行业云迁移实践 平安金融云整合HyperMotion云迁移解决方案,为金融行业客户提供迁移服务