rsync+inotify实现数据的实时备份

简介:

 rsync+intofy实现数据的实时备份

一、环境
1、主机信息:
 server node0  192.168.32.30  /var/www/html
 client node1  192.168.32.31  /var/www/html
2、实验要求:
    client node1 web目录下文件或目录发生任何修改、新建、删除等操作,都实时同步备份到server node0上
 
二、rsync服务器端配置(node0)
1、主配置文件(/etc/rsyncd.conf)
[root@node0 ~]# vim /etc/rsyncd.conf 
gid = nobody
use chroot = no
max connections = 10             # 最大连接数为4
strict modes = yes
pid file = /var/run/rsyncd.pid   #日志文件
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
 
[webdata]                    #认证模块,在client端需要指定
path = /var/www/html         #需要备份或接受备份的目录
comment = web server file    
ignore errors                 #忽然一些无关的IO错误
read only = no
write only = no
hosts allow = *
hosts deny = 192.168.32.33
list = false               
uid = root 
gid = root
auth users = backup                #认证用户,不是系统用户,如没有则匿名 
secrets file = /etc/rsync_srv.pass #认证密码文件,权限必须为600
 
2、认证密码文件(文件名rsyncd.conf中定义)
[root@node0 ~]# vim /etc/rsync_srv.pass 
backup:backupweb    
#一行一个用户,可多行多用户               
 
3、将rsync以守护进程形式启动
[root@node0 ~]# vim /etc/xinetd.d/rsync 
service rsync
{
        disable = no              #no启动,yes禁止
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
#如不用xinetd进程启动rsync服务可直接运行命令:rsync --daemon   
 
[root@node0 ~]# service xinetd restart    #启动rsync服务
 
三、rsync客户端node1上配置
1、认证密码文件
[root@node1 ~]# vim /etc/rsync_srv.pass 
backupweb
#密码文件名随意,权限600,密码通server node0上定义
 
2、同步node1到node0上测试
[root@node1 ~]# rsync -vzrtopg --delete --progress --password-file=/etc/rsync_srv.pass /var/www/html/ backup@192.168.32.30::webdata
building file list ... 
10 files to consider
./
index5
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/10)
index6
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=2/10)
index7
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=1/10)
index8
           0 100%    0.00kB/s    0:00:00 (xfer#4, to-check=0/10)
 
sent 334 bytes  received 110 bytes  888.00 bytes/sec
total size is 30  speedup is 0.07
#同步node1上数据到node0上,如果想同步node0上数据到node1上将源或目标调换一下位置即可:
 
3、同步node0到node1上测试
 
[root@node1 ~]# rm -rf /var/www/html/*
[root@node1 ~]# rsync -vzrtopg --delete --progress --password-file=/etc/rsync_srv.pass backup@192.168.32.30::webdata /var/www/html/
receiving file list ... 
5 files to consider
./                  
index5
           0 100%    0.00kB/s    0:00:00 (xfer#6, to-check=3/10)
index6
           0 100%    0.00kB/s    0:00:00 (xfer#7, to-check=2/10)
index7
           0 100%    0.00kB/s    0:00:00 (xfer#8, to-check=1/10)
index8
           0 100%    0.00kB/s    0:00:00 (xfer#9, to-check=0/10)
sent 308 bytes  received 623 bytes  1862.00 bytes/sec
total size is 30  speedup is 0.03
[root@node1 ~]# ls /var/www/html
index5  index6  index7  index8  index.html
                             
四、安装inotify工具inotify-tool(node1上)
1、确认系统内核支持inotify
[root@node1 ~]# ll /proc/sys/fs/inotify/
总计 0
-rw-r--r-- 1 root root 0 02-07 16:14 max_queued_events  #分配到每个实例中排队的event数量
-rw-r--r-- 1 root root 0 02-07 16:14 max_user_instances #每个real user可创建的inotify实例最大数量
-rw-r--r-- 1 root root 0 02-07 16:14 max_user_watches   #每个inotify实例支持的最大目录数量
#inotify只对其所在系统平台进行监控,所用要将node1的数据实时传送到node0上,就必须安装在node1上
 
[root@node1 ~]#echo 30000000 > /proc/sys/fs/inotify/max_user_watches  #增加实例支持的最大目录数量
 
2、安装inotify-tools工具
[root@node1 ~]# tar -zxvf inotify-tools-3.14.tar.gz 
[root@node1 ~]# cd inotify-tools-3.14
[root@node1 inotify-tools-3.14]# 
[root@node1 inotify-tools-3.14]# ./configure 
[root@node1 inotify-tools-3.14]# make&&make install
[root@node1 inotify-tools-3.14]# ll /usr/
[root@node1 inotify-tools-3.14]# ls -ls /usr/local/bin/inotify*
40 -rwxr-xr-x 1 root root 37260 02-07 11:20 /usr/local/bin/inotifywait
36 -rwxr-xr-x 1 root root 35434 02-07 11:20 /usr/local/bin/inotifywatch
 
五、利用inotifywait监控等待事件,配合shell脚本实现rsync的实时同步
1、编辑实时同步脚本
[root@node1 ~]# vim /etc/init.d/rsy_inotify.sh 
#!/bin/bash
host=192.168.32.30
src=/var/www/html/
des=webdata
user=backup
case $1 in
start)
   inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attri
b $src \
| while read files  
 do
  rsync -vzrtopg --delete --progress --password-file=/etc/rsync_srv.pass $src $user@$host::$des >
> /tmp/rsyncd.log 2>&1
  echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
 done &
;;
stop)
 kill -9 `ps -ef | grep inotifywait |awk '{print $2}' |sed -n '1p'`
 echo "the inotifywait service have been stoped" >>/tmp/rsync.log
;;
esac
 
2、启动测试
[root@node1 ~]# /etc/init.d/rsy_inotify.sh start
[root@node0 ~]# ls /var/www/html/    #查看node0上内容
index1.html  index2.html  index3.html  index4.html  index5  index6  index7  index8  index.html
[root@node1 ~]# ls /var/www/html/    #查看node1上内容
index1.html  index2.html  index3.html  index4.html  index5  index6  index7  index8  index.html
[root@node1 ~]# rm -rf /var/www/html/*
[root@node0 ~]# ls /var/www/html/    #结果:node0上同步删除了
07/02/12 16:27 /var/www/html/index1.htmlDELETE was rsynced
07/02/12 16:27 /var/www/html/index2.htmlDELETE was rsynced
07/02/12 16:27 /var/www/html/index3.htmlDELETE was rsynced
07/02/12 16:27 /var/www/html/index4.htmlDELETE was rsynced
07/02/12 16:27 /var/www/html/index5DELETE was rsynced
07/02/12 16:27 /var/www/html/index6DELETE was rsynced
07/02/12 16:27 /var/www/html/index7DELETE was rsynced
07/02/12 16:27 /var/www/html/index8DELETE was rsynced
07/02/12 16:27 /var/www/html/index.htmlDELETE was rsynced
#日志显示文件删除备inotifywait监控到,并同步了。

本文转自netsword 51CTO博客,原文链接:http://blog.51cto.com/netsword/774225
相关文章
|
4天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1106 0
|
3天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
511 10
|
13天前
|
人工智能 运维 安全
|
12天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
4天前
|
弹性计算 Kubernetes jenkins
如何在 ECS/EKS 集群中有效使用 Jenkins
本文探讨了如何将 Jenkins 与 AWS ECS 和 EKS 集群集成,以构建高效、灵活且具备自动扩缩容能力的 CI/CD 流水线,提升软件交付效率并优化资源成本。
301 0
|
11天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
12天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
803 23
|
4天前
|
缓存 供应链 监控
VVIC seller_search 排行榜搜索接口深度分析及 Python 实现
VVIC搜款网seller_search接口提供服装批发市场的商品及商家排行榜数据,涵盖热销榜、销量排名、类目趋势等,支持多维度筛选与数据分析,助力选品决策、竞品分析与市场预测,为服装供应链提供有力数据支撑。
|
4天前
|
缓存 监控 API
Amazon item_review 商品评论接口深度分析及 Python 实现
亚马逊商品评论接口(item_review)可获取用户评分、评论内容及时间等数据,支持多维度筛选与分页调用,结合Python实现情感分析、关键词提取与可视化,助力竞品分析、产品优化与市场决策。