rsync + inotify 数据实时同步

rsync + inotify 数据实时同步

一、rsync简介

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,
支持本地复制,或者与其他SSH、rsync主机同步

二、rsync三种命令

Rsync的命令格式常用的有以下三种:(与ssh类似)
rsync [OPTION]… SRC DEST (本地服务器之间:复制)
rsync [OPTION]… SRC [USER@]HOST:DEST (上传)
rsync [OPTION]… [USER@]HOST:SRC DEST (下载)

1、 rsync [OPTION]… SRC DEST (本地服务器之间:复制)
服务器(源):
[root@server tmp]# touch a
[root@server tmp]# ls
a
[root@server tmp]# rsync -a a filea
[root@server tmp]# ls
a  filea
2、 rsync [OPTION]… SRC [USER@]HOST:DEST (上传)
服务器(源):
[root@server tmp]# rsync -avz a root@192.168.100.30:/tmp
The authenticity of host '192.168.100.30 (192.168.100.30)' can't be established.
ECDSA key fingerprint is SHA256:R7/1dpul7cu8SnefsN2wQw5hKDL+xekk0ffasLS6OGI.
ECDSA key fingerprint is MD5:81:88:a1:16:52:83:c0:d5:59:ad:2b:3a:d5:52:02:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.100.30' (ECDSA) to the list of known hosts.
root@192.168.100.30's password: 
sending incremental file list
a

sent 80 bytes  received 35 bytes  6.57 bytes/sec
total size is 0  speedup is 0.00
客户端(目标):
[root@stw3 tmp]# ls
a
3、 rsync [OPTION]… [USER@]HOST:SRC DEST (下载)
目标:
[root@stw3 tmp]# touch b
[root@stw3 tmp]# ls
a  b
源:
[root@server tmp]# rsync -avz root@192.168.100.30:/tmp/b .
root@192.168.100.30's password: 
receiving incremental file list
b

sent 43 bytes  received 80 bytes  7.45 bytes/sec
total size is 0  speedup is 0.00
[root@server tmp]# ls
a  b  filea

三、rsync+inotify

1、rsync的优点和缺点

优点:rsync与传统的cp、tar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,
通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,
对本地磁盘定期做数据镜像等。

缺点:首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千 万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。

其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。

2、inotify

inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。在前面有讲到,rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。

基于以上原因,rsync+inotify(数据实时同步)组合出现了!

四、配置

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

1、源服务器和目标服务器要进行时钟同步
源:
[root@server ~]# vim /etc/chrony.conf 
[root@server ~]# systemctl restart chronyd
[root@server ~]# systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
[root@server ~]# timedatectl 
      Local time: Wed 2025-08-20 11:15:45 CST
  Universal time: Wed 2025-08-20 03:15:45 UTC
        RTC time: Wed 2025-08-20 03:15:45
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
[root@server ~]# hwclock -w

在这里插入图片描述

目标:
[root@stw3 ~]# vim /etc/chrony.conf 
[root@stw3 ~]# systemctl restart chronyd
[root@stw3 ~]# systemctl enable chronyd
Created symlink from /etc/systemd/system/multi-user.target.wants/chronyd.service to /usr/lib/systemd/system/chronyd.service.
[root@stw3 ~]# hwclock -w
[root@stw3 ~]# chrony
chronyc  chronyd  
[root@stw3 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? 192.168.100.20                0   7     0     -     +0ns[   +0ns] +/-    0ns

在这里插入图片描述

2、关闭防火墙和selinux(源和目标主机都要关闭,操作一致)
[root@server ~]# systemctl stop firewalld.service 
[root@server ~]# systemctl disable firewalld.service 
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@server ~]# setenforce 0
[root@server ~]# vim /etc/selinux/config 
[root@server ~]# reboot

在这里插入图片描述

3、目标服务器:
(1)修改配置文件

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@stw3 ~]# vim /etc/rsyncd.conf

在这里插入图片描述

(2)创建用户认证文件
[root@stw3 ~]# vim /etc/rsync.pass
[root@stw3 ~]# cat /etc/rsync.pass 
admin:123456

在这里插入图片描述

(3)设置文件权限
[root@stw3 ~]# chmod 600 /etc/rsync*
[root@stw3 ~]# ll /etc/rsync*
-rw------- 1 root root 914 Aug 20 11:30 /etc/rsyncd.conf
-rw------- 1 root root  13 Aug 20 11:32 /etc/rsync.pass
(4)启动rsync服务并设置开机自启
[root@stw3 ~]# rsync --daemon
[root@stw3 ~]# vim /etc/rc.d/rc.local 
[root@stw3 ~]# netstat -tulnp | grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      9993/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      9993/rsync      

在这里插入图片描述

4、源服务器:
(1)配置网络源和epel-release
[root@server ~]# cd /etc/yum.repos.d/
[root@server yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo
[root@server yum.repos.d]# rm -rf *
[root@server yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2025-08-20 11:43:00--  https://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 117.92.155.17, 150.139.241.204, 111.77.199.29
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|117.92.155.17|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’

100%[===================================================================>] 2,523       --.-K/s   in 0s      

2025-08-20 11:43:00 (10.5 MB/s) -/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]

[root@server yum.repos.d]# yum -y install epel-release
(2)安装rsync服务端软件,只需要安装,不要启动,不需要配置(可忽略,rsync默认已经安装)
[root@server ~]# yum -y install rsync
(3)创建认证密码文件
[root@server ~]# vim /etc/rsync.pass
[root@server ~]# cat /etc/rsync.pass 
123456

在这里插入图片描述

(4)设置文件权限,只设置文件所有者具有读取、写入权限即可
[root@server ~]# chmod 600 /etc/rsync.pass 
(5)在源服务器上创建测试目录,然后在源服务器运行以下命令
[root@server ~]# mkdir /root/etc
[root@server ~]# cd /root/etc
[root@server etc]# ls
[root@server etc]# mkdir test
[root@server etc]# cd test
[root@server test]# pwd
/root/etc/test
[root@server test]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.100.30::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
deleting vmware-root_8623-1990534217/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-rtkit-daemon.service-V88iF7/tmp/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-rtkit-daemon.service-V88iF7/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-cups.service-I0Q073/tmp/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-cups.service-I0Q073/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-colord.service-9TD9Zd/tmp/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-colord.service-9TD9Zd/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-chronyd.service-fc3OEy/tmp/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-chronyd.service-fc3OEy/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-bolt.service-ZCd3B8/tmp/
deleting systemd-private-587fbc66687344f9bed799a3b4ad760a-bolt.service-ZCd3B8/
deleting .font-unix/
deleting .esd-0/
deleting .XIM-unix/
deleting .X11-unix/X0
deleting .X11-unix/
deleting .Test-unix/
deleting .ICE-unix/9983
deleting .ICE-unix/9630
deleting .ICE-unix/9469
deleting .ICE-unix/9446
deleting .ICE-unix/
deleting .X0-lock
./
test/

sent 77 bytes  received 1,018 bytes  104.29 bytes/sec
total size is 0  speedup is 0.00
(6)在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
[root@stw3 ~]# cd /tmp
[root@stw3 tmp]# ls
systemd-private-587fbc66687344f9bed799a3b4ad760a-bolt.service-ZCd3B8
systemd-private-587fbc66687344f9bed799a3b4ad760a-chronyd.service-fc3OEy
systemd-private-587fbc66687344f9bed799a3b4ad760a-colord.service-9TD9Zd
systemd-private-587fbc66687344f9bed799a3b4ad760a-cups.service-I0Q073
systemd-private-587fbc66687344f9bed799a3b4ad760a-rtkit-daemon.service-V88iF7
vmware-root_8623-1990534217
[root@stw3 tmp]# ls
test
测试:源服务器中创建文件,可以传输到目标服务器中
源:
[root@server test]# ls
[root@server test]# 
[root@server test]# touch file1 file2
[root@server test]# ls
file1  file2
[root@server test]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.100.30::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
test/
test/file1
              0 100%    0.00kB/s    0:00:00 (xfr#1, to-chk=1/4)
test/file2
              0 100%    0.00kB/s    0:00:00 (xfr#2, to-chk=0/4)

sent 195 bytes  received 66 bytes  24.86 bytes/sec
total size is 0  speedup is 0.00
目标:
[root@stw3 tmp]# cd test
[root@stw3 test]# ls
file1  file2

实现实时同步(rsync+inotify)

源:
(1)安装inotify-tools工具(make、gcc、gcc-c++、inotify-tools)
[root@server ~]# yum -y install make gcc gcc-c++ inotify-tools
(2)写同步脚本,让脚本自动去检测我们制定的目录下 文件发生的变化,然后再执行rsync的命令把它同步到我们的目标服务器端去
[root@server ~]# mkdir /tbjiaoben
[root@server ~]# cd /tbjiaoben
[root@server tbjiaoben]# ls
[root@server tbjiaoben]# 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∣whilereadfiles;dorsync−avzP−−delete−−timeout=100−−password−file=src | while read files;do rsync -avzP --delete --timeout=100 --password-file=srcwhilereadfiles;dorsyncavzPdeletetimeout=100passwordfile={password} $src user@user@user@host::desecho"des echo "desecho"{files} was rsynced" >>/tmp/rsync.log 2>&1
done

在这里插入图片描述

(3)启动脚本
[root@server ~]# nohup bash /tbjiaoben/inotify.sh &
[2] 11411
[root@server ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@server ~]# ps -ef | grep inotify
root      11401      1  0 15:48 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/etc
root      11402      1  0 15:48 pts/0    00:00:00 bash /tbjiaoben/inotify.sh
root      11411   9783  0 15:48 pts/0    00:00:00 bash /tbjiaoben/inotify.sh
root      11412  11411  0 15:48 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/etc
root      11413  11411  0 15:48 pts/0    00:00:00 bash /tbjiaoben/inotify.sh
root      11436   9783  0 15:50 pts/0    00:00:00 grep --color=auto inotify
(wd now: ~)
(4)在源服务器上生成一个新文件,查看inotify生成的日志

从日志上可以看到,我们生成了一个test文件,并且添加了内容到其里面

[root@server ~]# cd /root/etc
[root@server etc]# ls
test
[root@server etc]# touch file111
[root@server etc]# ls
file111  test
[root@server etc]# cd
[root@server ~]# tail /tmp/rsync.log 
20250820 15:52 /root/etc/file111CREATE was rsynced
20250820 15:52 /root/etc/file111CREATE was rsynced
20250820 15:52 /root/etc/file111ATTRIB was rsynced
20250820 15:52 /root/etc/file111ATTRIB was rsynced
(5)目标服务器验证
[root@stw3 ~]# cd /tmp
[root@stw3 tmp]# ls
etc  test
[root@stw3 tmp]# cd etc
[root@stw3 etc]# ls
file111  test
5、设置脚本开机自启
(1)把nohup /bin/bash /tbjiaoben/inotify.sh &写到配置文件(/etc/rc.d/rc.local)中
[root@server ~]# vim /etc/rc.d/rc.local 

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值