rsync+inotify

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,
支持本地复制,或者与其他SSH、rsync主机同步。 与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。

Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。

环境:

服务器类型ip地址应用操作系统
源服务器192.168.35.142rsync inotify-tools脚本  rockylinux9
目标服务器192.168.35.143rsync  rockylinux9

需求:把源服务器上/root/etc目录实时同步到目标服务器的/tmp下

操作:

//在服务器,进行时间同步
[root@server ~]# systemctl restart chronyd
[root@server ~]# systemctl enable chronyd
[root@server ~]# hwclock -w
//在客户端,进行时间同步
[root@client ~]# systemctl restart chronyd
[root@client ~]# systemctl enable chronyd
[root@client ~]# hwclock -w

//在服务器,安装rsync-inotify
[root@server ~]# yum -y install rsync tar
[root@server ~]# yum -y install inotify-tools make gcc gcc-c++
//在客户端 安装rsync
[root@client ~]# yum -y install rsync

//在客户端编辑配置文件,创建用户认证文件,设置文件权限,启动rsync服务并设置开机自启动
[root@client ~]# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log    // 日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid     // pid文件的存放位置
lock file = /var/run/rsync.lock   // 支持max connections参数的锁文件
secrets file = /etc/rsync.pass    // 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件

[etc_from_client]     // 自定义同步名称
path = /tmp/          // rsync服务端数据存放路径,客户端的数据将同步至此目录
comment = sync etc from client
uid = root        // 设置rsync运行权限为root
gid = root        // 设置rsync运行权限为root
port = 873        // 默认端口
ignore errors     // 表示出现错误忽略错误
use chroot = no       // 默认为true,修改为no,增加对目录文件软连接的备份
read only = no    // 设置rsync服务端为读写权限
list = no     # 不显示rsync服务端资源列表
max connections = 200     // 最大连接数
timeout = 600     // 设置超时时间
auth users = admin        // 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.100.10   // 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.1.1      // 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
[root@client ~]# vim /etc/rsync.pass
admin:redhat
[root@client ~]# chmod 600 /etc/rsync*
[root@client ~]# ll /etc/rsync*
-rw------- 1 root root 812 Jul 24 08:02 /etc/rsyncd.conf
-rw------- 1 root root  13 Jul 24 08:03 /etc/rsync.pass
[root@client ~]# rsync --daemon
[root@client ~]# vim /etc/rc.d/rc.local 
rsync --daemon --config=/etc/rsyncd.conf
[root@client ~]# netstat -tulnp | grep 873 //查看873端口进行验证

//在服务器,创建认证密码文件,设置文件权限,在源服务器上创建测试目录进行测试
[root@server ~]# vim /etc/rsync.pass //密码与服务端同步
redhat
[root@server ~]#  chmod 600 /etc/rsync.pass
[root@server ~]# mkdir etc
[root@server ~]# cd etc/
[root@server etc]# mkdir test
[root@server etc]# cd test/
[root@server test]# pwd
/root/etc/test 
[root@server ~]# rsync -avH --port 873 --progress  --delete /root/etc/ admin@192.168.35.143::etc_from_client --password-file=/etc/rsync.pass 
// 略
//在客户端验证
[root@client ~]# cd /tmp/
[root@client tmp]# ls
test

//在服务器写同步脚本
[root@server ~]# mkdir /lsy
[root@server ~]# cd /lsy/
[root@server lsy]# pwd
/lsy
[root@server lsy]# vim inotify.sh
host=192.168.100.20     // 目标服务器的ip(备份服务器)
src=/root/etc        // 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=etc_from_client     // 自定义的模块名,需要与目标服务器上定义的同步名称一致
password=/etc/rsync.pass        // 执行数据同步的密码文件
user=admin          // 执行数据同步的用户名
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files;do
    rsync -avzP --delete  --timeout=100 --password-file=${password} $src $user@$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
//inotifywait详解
//-m:持续监听
//-r:使用递归形式监视目录
//-q:减少冗余信息,只打印出需要的信息
//-e:指定要监视的事件,多个时间使用逗号隔开
//–timefmt:时间格式
//–format:监听到的文件变化的信息
//%w:表示发生事件的目录 
//%f:表示发生事件的文件 
//%e:表示发生的事件 
//%Xe:事件以“X”分隔 
//%T:使用由–timefmt定义的时间格式
//access:访问,读取文件 
//modify:修改,文件内容被修改
//attrib:属性,文件元数据被修改 
//move:移动,对文件进行移动操作 
//create:创建,生成新文件 
//open:打开,对文件进行打开操作 
//close:关闭,对文件进行关闭操作 
//delete:删除,文件被删除
[root@server lsy]# chmod +x inotify.sh 
[root@server lsy]# cd
[root@server ~]# nohup bash /lsy/inotify.sh &
[1] 10655
[root@server ~]# nohup: ignoring input and appending output to 'nohup.out'

[root@server ~]# ls
anaconda-ks.cfg  etc  nohup.out

//测试
//服务器
[root@server ~]# cd /root/etc/
[root@server etc]# touch file13434
[root@server etc]# ls
file13434  test
[root@server etc]# cd
[root@server ~]# tail /tmp/rsync.log //查看日志文件
20240724 08:21 /root/etc/file13434CREATE was rsynced
20240724 08:21 /root/etc/file13434ATTRIB was rsynced
//客户端
[root@client ~]# cd /tmp/
[root@client tmp]# ls
test
[root@client tmp]# ls
etc  test
[root@client tmp]# cd etc/
[root@client etc]# ls
file13434  test




总结 :

随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,
首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。
而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,
虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,
无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值