当前位置:网站首页>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!
边栏推荐
- 机器学习让文字识别更简单:Kotlin+MVVM+华为ML Kit
- 与张小姐的春夏秋冬(2)
- Build msys2 environment with win10
- 超简单集成HMS ML Kit 实现parental control
- WIN10 编译ffmpeg(包含ffplay)
- DAY6:利用 PHP 编写登陆页面
- Differences between href and SRC
- 与开源项目同步开发& CodeReview & Pull Request & fork怎么拉取原始仓库
- Crypto giants all in metauniverse, and platofarm may break through
- Thinkphp6管道模式Pipeline使用
猜你喜欢
Fantom (FTM) 在 FOMC会议之前飙升 45%
机器学习让文字识别更简单:Kotlin+MVVM+华为ML Kit
Plato farm is expected to further expand its ecosystem through elephant swap
The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
Breaking through the hardware bottleneck (I): the development of Intel Architecture and bottleneck mining
Read and understand move2earn project - move
Sliding switch of tab of uniapp component
sql-server 数据表的简单操作
Gluster cluster management analysis
剑指核心-TaoCloud全闪SDS助力构建高性能云服务
随机推荐
Reporting Service 2016 自定义身份验证
7 月 28 日 ENS/USD 价值预测:ENS 吸引巨额利润
与张小姐的春夏秋冬(4)
Reporting Services- Web Service
Laravel服务容器(继承与事件)
Record the SQL injection vulnerability of XX company
Power BI Report Server 自定义身份验证
PHP如何生成二维码?
MySQL decompressed version windows installation
超简单集成HMS ML Kit 实现parental control
Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
Win10 搭建MSYS2环境
“山东大学移动互联网开发技术教学网站建设”项目实训日志二
Sports health is deeply rooted in the hearts of the people, and move protocol leads quality life
DeFi 2.0的LaaS协议,重振DeFi赛道发展的关键
Bare metal cloud FASS high performance elastic block storage solution
Changed crying, and finally solved cannot read properties of undefined (reading 'parsecomponent')
Under the bear market of encrypted assets, platofarm's strategy can still obtain stable income
C# 判断用户是手机访问还是电脑访问
WIN10 编译ffmpeg(包含ffplay)