惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
H
Hacker News: Front Page
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
AWS News Blog
AWS News Blog
Spread Privacy
Spread Privacy
S
Schneier on Security
The Hacker News
The Hacker News
Cyberwarzone
Cyberwarzone
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Tailwind CSS Blog
S
Secure Thoughts
N
Netflix TechBlog - Medium
T
The Exploit Database - CXSecurity.com
I
Intezer
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Help Net Security
Help Net Security
K
Kaspersky official blog
Google Online Security Blog
Google Online Security Blog
L
LangChain Blog
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
Hacker News: Ask HN
Hacker News: Ask HN
www.infosecurity-magazine.com
www.infosecurity-magazine.com
有赞技术团队
有赞技术团队
P
Privacy International News Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recent Announcements
Recent Announcements
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
Recorded Future
Recorded Future
The Last Watchdog
The Last Watchdog
G
Google Developers Blog
T
Threatpost
小众软件
小众软件
S
Securelist
Recent Commits to openclaw:main
Recent Commits to openclaw:main
O
OpenAI News

博客园 - 光何

奴隶主思维 linux挂载windows共享命令 吃苦、努力、竞争 活的放松点 女性慕强慕的是什么 社会学研究之鼓励生育 mysql双主机热备 中科方德安装jdk、redis、mysql、nginx 如何远离精神内耗,远离抑郁 支持性神经递质 为何大多数人流于世俗,而清醒者往往导向虚无 InstallShield安装包制作 windows平台常用安装包制作 桌面UI开发框架 五款Java后端快速开发平台 maven依赖范围及依赖传递 Feign根据动态ip调用不同主机的微服务 硬盘安装RockyLinux9 女生靠追还是靠吸引
Linux文件实时同步配置
光何 · 2023-08-14 · via 博客园 - 光何

  今天接到需求说要实现mysql与指定目录的文件实时双向热备,mysql的上篇已经解决,linux文件的在这篇展开讨论。

  Linux主机之间文件实时自动同步备份,使用 rsync+inotify 组合的方式来实现,避免由于硬件或者软件导致的 Linux 系统死机或损坏造成的损失。

一、rsync+inotify 简介

1、rsync简介

  rsync(remote synchronize)是 Liunx/Unix 下的一个远程数据同步工具,它可通过 LAN/WAN 快速同步多台主机间的文件和目录。

  Linux 之间同步文件一般有两种方式,分别是 rsync 与 scp 。scp 相当于复制,粘贴,文件不存在则新建,若存在则覆盖,而 rsync 则是比较两边文件是否相同,不相同才进行更新。所以 rsync 和 scp 在文件夹存在的情况下差异很大,因为 scp 是复制和覆盖,从执行性能来说 rsync 更胜一筹。而且 rsync 能将文件夹、文件的权限等信息也保存下来。

  但是 rsync 也有一定的缺点,在同步数据时,需要扫描所有文件后进行比对,如果文件数量相当大时,扫描文件就非常耗费时间和性能。其次,rsync 不能够实时监测、同步数据,这就可能导致一些时间段数据不一致。解决这个问题的方法就是实时同步,所以需要使用 rsync+inotify 组合。

2、inotify简介

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

先看一看系统中是否已安装rsync,现在一般系统都是预装的

rsync --version

再检查系统内核是否支持 inotify:

ll /proc/sys/fs/inotify

出现以下三个文件表示系统默认支持 inotify,如下所示。

二、整体架构

这里我使用两个 Linux 服务器节点来做演示,实现两个节点间文件的实时同步,node1 为源服务器节点,就是需要同步数据的节点,部署 rsync+inotify ,node2 为同步节点,也就是接收同步数据的节点,只需要部署 rsync,如下所示。

三、同步节点部署(rsync)

同步节点,也就是node2 172.19.42.200

配置rsync配置文件

vim /etc/rsyncd.conf

 1 uid = nobody
 2 gid = nobody
 3 use chroot = yes
 4 max connections = 10
 5 strict mode=yes
 6 pid file = /var/run/rsyncd.pid
 7 lock file=/var/run/rsync.lock
 8 log file=/var/log/rsyncd.log
 9 [backup]
10 path = /opt/ffs/files/
11 comment = ffs file
12 ignore errrors
13 read only=no
14 write only=no
15 hosts allow=172.19.42.100
16 hosts deny=*
17 list=false
18 uid=root
19 gid=root
20 auth users=ffs
21 secrets file=/etc/rsync.password

其中需要用到一个密码文件, /etc/rsync.password,内容格式为:user:password

echo "ffs:ffs" > /etc/rsync.password

然后需要给密码文件600权限

chmod 600 /etc/rsync.password

启动 rsync 守护进程

/usr/bin/rsync --daemon

加入系统自启动文件

echo "/usr/bin/rsync --daemon" >> /etc/rc.local

chmod 755 /etc/rc.local

如果有防火墙,开放端口

firewall-cmd --add-port=873/tcp --permanent --zone=public
firewall-cmd --reload

查看 rsync 进程,如下

ps aux | grep rsync

四、源服务器节点部署(rsync+inotify)

源服务器节点,也就是 node1 172.19.42.100,需要部署 rsync 和 inotify

配置rsync配置文件

vim /etc/rsyncd.conf

 1 uid = nobody
 2 gid = nobody
 3 use chroot = yes
 4 max connections = 10
 5 strict mode=yes
 6 pid file = /var/run/rsyncd.pid
 7 lock file=/var/run/rsync.lock
 8 log file=/var/log/rsyncd.log
 9 [backup]
10 path = /opt/ffs/files/
11 comment = ffs file
12 ignore errrors
13 read only=no
14 write only=no
15 hosts allow=172.19.42.200
16 hosts deny=*
17 list=false
18 uid=root
19 gid=root
20 auth users=ffs
21 secrets file=/etc/rsync.password

源服务器节点中只需要配置认证密码文件,首先在 etc 文件夹下创建文件 rsync.password,只需要密码,不需要用户,密码需要和同步节点 node2 中的一致

echo "ffs" > /etc/rsync.password

然后需要给密码文件600权限

chmod 600 /etc/rsync.password

启动 rsync 守护进程

/usr/bin/rsync --daemon

加入系统自启动文件

echo "/usr/bin/rsync --daemon" >> /etc/rc.local

chmod 755 /etc/rc.local

如果有防火墙,开放端口

firewall-cmd --add-port=873/tcp --permanent --zone=public
firewall-cmd --reload

测试一下,先在源服务器节点的 /opt/ffs/files/ 目录下新建一个 test 文件夹

mkdir /opt/ffs/files/test

然后进行进行同步测试,其中一些参数要和同步节点配置文件中相对应,比如下面的认证模块名backup、用户名 ffs 等

rsync -avH --port 873 --delete /opt/ffs/files/ ffs@172.19.42.200::backup --password-file=/etc/rsync.password

可以看到 node1 中文件夹 test 已经发送,查看同步节点 node2 中,如下

 test 文件夹已经同步到 node2,所以我们的 rsync 配置成功,可以进行文件同步,接下来就是部署 inotify 实现实时同步,通过inotify监听文件或文件夹,如果有变动就进行同步。

这里我们用到了inotify-tools,我使用的中科方德系统预装了这个,如果你的系统没有这个需要先安装,我这里直接开始配置了

创建用于 rsync 同步的 shell 脚本,如果添加、修改、删除了文件或文件夹,inotify 可以监控到,然后通过 rsync 进行同步,这里我们就在需要进行监控的目录创建这个脚本

vim /opt/ffs/files/inotifyrsync.sh

1 #!/bin/bash
2 host1=172.19.42.200
3 src=/opt/ffs/files/
4 dst1=backup
5 user1=ffs
6 /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src | while read files
7 do        /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsync.password $src $user1@$host1::$dst1 > /dev/null 2>&1
8         echo "${files} was rsynced." >> /tmp/rsync.log 2>&1
9 done

其中 host 是 client 的 ip,src 是 server 端要实时监控的目录,des 是认证的模块名,需要与 client 一致,user 是建立密码文件里的认证用户。

然后给这个脚本赋予权限

chmod 755 /opt/ffs/files/inotifyrsync.sh

后台运行这个脚本

/opt/ffs/files/inotifyrsync.sh &

有需要可以将脚本加入系统自启动文件中

echo "/opt/ffs/files/inotifyrsync.sh &" >> /etc/rc.local

测试,在 node1 节点中添加删除修改文件或文件夹,看 node2 中是否会自动同步,首先在 node1 中创建一个文件夹 test1、test2,文件test1.txt、text2.txt

 在 node2 中都会进行实时的同步备份,如下所示(文件和文件夹都创建成了一个文件[笑哭],小问题,不影响测试)

 经验证,修改文件和删除文件也能同步,实时自动同步备份功能成功实现