当前位置:网站首页>rsync+inotyfy实现数据单项监控实时同步
rsync+inotyfy实现数据单项监控实时同步
2022-07-29 05:20:00 【JACK-JIE】
一,环境信息
1, 使用环境
CentOS Linux release 7.5.1804 (Core)
更新源:192.168.21.131
目标源:192.168.21.132
2, 检查Rsync是否安装(没有的话直接yum安装即可)
rpm -qa | grep rsync
2.1,我使用的rsync版本
rsync-3.1.2-6.el7_6.1.x86_64
2.2,rsync的常用参数
-v –verbose详细模式输出。
-a –archive归档模式,表示以递归方式传输文件,并保持所有文件属性不变。
-l 保留软链接
-R 保留相对路径
-H 保留硬链接
-p,-o,-g,-A 分别保留权限,属主,属组,acl等,但如果加了-a,这些就都包括了
-z, –compress对备份的文件在传输时进行压缩处理。
-D 等于–devices --specials 表示支持b,c,s,p类型的文件
–delete删除那些DST中存在而在SRC中没有的文件。
–progress显示转移过程中的进度
二,目标服务器的配置,Rsync服务端(192.168.21.132)
3, 定义Rsync配置文件
vim /etc/rsyncd.conf
uid = tongbu #守护进程用户
gid = tongbu #守护进程组
use chroot = no #不适用chroot
max connections = 100 #最大连接数,0为不限制
port = 873 #默认端口为873
timeout = 600 #指定IP的超时时间
pid file = /var/run/rsyncd.pid #Pid存放的位置
lock file = /var/run/rsyncd.lock #锁文件存放位置,默认为/var/run/rsync.lock
log file = /var/log/rsyncd.log #日志文件的存放位置
Log format = %t %a %m %f %b #自定义日志文件字段
syslog facility = local3 #定义日志级别,默认是daemon
#模块参数
[web1] #认证模块名,在client端需要指定
path = /home #需要做镜像的目录
ignore errors #忽略无关的IO错误
comment = backup web #模块的注释信息
read only = no #是否允许客户上载文件
list = no #不允许列文件
hosts allow = 192.168.21.0/255.255.255.0 #允许的主机网段
auth users = tongbu #认证的用户名,如果没有这行则表明是匿名,此用户与系统无关
secrets file = /etc/tongbu.pwd #用户及密码对比表
3.1,注意
报错内容:rsync: failed to set times on “xxxx”: Operation not permitted
这里uid及gid需要与同步的目录的用户及组保持一致,否则会报错
4, 创建认证文件
echo “tongbu:Admin123” >> /etc/tongbu.pwd
5, 设定权限
chmod 600 /etc/{tongbu.pwd,rsyncd.conf}
6, 建立motd文件(rsync的欢迎信息,随便填写)
echo “Welcome to use the rsync services!” >> /var/rsyncd.motd
7, 启动Rsync(以守护进程启动)
/usr/bin/rsync –daemon
8, 加入自启动
echo “/usr/bin/rsync --daemon” >> /etc/rc.local
三,更新服务器配置,Rsync客户端(192.168.21.131)
1, 安装inotify-tools
1.1, 注意
在安装inotify-tools前请先确认你的linux内核是否打到了2.6.13,并且在编译时开启了CONFIG_INOTIFY选项,也可以通过以下命令检测
1.2, 命令(出现以下三个MAX则为开启)
ls /proc/sys/fs/inotify
max_queued_events max_user_instances max_user_watches
1.3, 安装inotify-tools
tar -zxf inotify-tools-3.14.gz
cd inotify-tools-3.14
./confifure
Make && make install
1.4, inotifywait的几个参数介绍
-m 即–monitor,表示始终保持事件监听状态。
-r 即–recursive,表示递归查询目录。
-q 即–quiet,表示打印出监控事件。
-e 即–event,通过此参数可以指定要监控的事件,常见的事件有modify、delete、create、attrib等
–timefmt:指定时间的输出格式
–format:指定变化文件的详细信息
2, 编写实时监控脚本
vim /root/rsync.sh
#!/bin/bash
UNISON=ps -ef |grep -v grep|grep -c inotifywait
if [ ${UNISON} -lt 1 ];then
HOST1=192.168.21.132
SRC=/home/tongbu
DES1=web1
USER1=tongbu
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${SRC} |
while read file
do
rsync -vzrtopg --delete --progress ${SRC} ${USER1}@${HOST1}::${DES1} --password-file=/etc/tongbu.pwd &&
echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
echo "---------------------------------------------------------------------------"
done
fi
2.1,如果不需要实时监控的话只需要做一个定时任务即可
30 1 * * * /usr/bin/rsync -vzrtopg --delete --progress /home/tongbu/ [email protected]::web1 --password-file=/etc/tongbu.pwd >/dev/null 2>&1 &
3, 创建认证文件
echo “tongbu:Admin123” >> /etc/tongbu.pwd
4, 设定权限
chmod 600 /etc/tongbu.pwd
5, 检查启动
5.1,语法检查
/usr/bin/bash -n /root/rsync.sh
5.2,后台启动
chmod +x /root/rsync.sh
nohup /usr/bin/sh /root/rsync.sh &
6, 在脚本之前可以使用命令先进行测试
rsync -vzrtopg --delete --progress /home/tongbu/ [email protected]::web1 --password-file=/etc/tongbu.pwd
When you don’t know what to do, just settle down and study!
边栏推荐
- Qt设置背景图片方法
- Idea using JDBC to connect mysql database personal detailed tutorial
- 与张小姐的春夏秋冬(5)
- DAY6:利用 PHP 编写文件上传页面
- Breaking through the hardware bottleneck (I): the development of Intel Architecture and bottleneck mining
- Sqlmap是什么以及使用方法
- Starfish OS:以现实为纽带,打造元宇宙新范式
- 如何 Pr 一个开源composer项目
- The completely decentralized programming mode does not need servers or IP, just like a aimless network extending everywhere
- php写一个购买全网最低价的纸尿裤
猜你喜欢
Changed crying, and finally solved cannot read properties of undefined (reading 'parsecomponent')
Strategic cooperation with many institutions shows the strength of the leading company of platofarm yuancosmos
如何在加密市场熊市中生存?
“山东大学移动互联网开发技术教学网站建设”项目实训日志二
突破硬件瓶颈(一):Intel体系架构的发展与瓶颈挖掘
农村品牌建设给年轻人的一些机会
华为2020校招笔试编程题 看这篇就够了(下)
Laravel service container (inheritance and events)
『全闪实测』数据库加速解决方案
量化开发必掌握的30个知识点【什么是分笔逐笔数据】?
随机推荐
Countdown of the uniapp component (such as the countdown to reading the agreement and the countdown to completing learning)
一文读懂Move2Earn项目——MOVE
Elastic box flex
Related knowledge of elastic box
SQL修复重复数据
如何 Pr 一个开源composer项目
QT layout management -- Part stretch principle and sizepolicy
完全去中心化的编程模式,不需要服务器,也不需要ip,就像一张漫无目的的网络、四处延伸
Get the number of daffodils
性能对比|FASS iSCSI vs NVMe/TCP
Laravel服务容器(继承与事件)
中海油集团,桌面云&网盘存储系统应用案例
Differences between href and SRC
H5 semantic label
第五空间智能安全⼤赛真题----------PNG图⽚转换器
JDBC连接数据库详细步骤
突破硬件瓶颈(一):Intel体系架构的发展与瓶颈挖掘
DAY6:利用 PHP 编写文件上传页面
MOVE PROTOCOL全球健康宣言,将健康运动进行到底
July 28 ens/usd Value Forecast: ENS attracts huge profits