结合nfs服务器很实用的rsync+inotify实时同步

简述

在服务器中,通常会结合计划任务shell脚本来执行本地备份,而rsync可用于异地备份,它同步的是目录和文件,结合inotify,根据目录或文件的变动及时同步。

言归正传,看一下rsync的概述

rsync( Remote Sync,远程同步)是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,且采用优化的同步算法,传输前执行压缩,因此非常适用于异地备份,镜像服务器等应用。

rsync的官方网址是:https://rsync.samba.org/
目前最新的版本是3.1.3(还是18年更新的),不过真的很实用,由 Wayne Davison进行维护,作为一种最常用的文件备份工具,rsync往往是 Linux和UNIX系统默认安装的基本组件之一
在这里插入图片描述
可以使用rpm -q rsync查看版本

[root@bogon ~]# rpm -q rsync
rsync-3.0.9-17.el7.x86_64

不想要它的,也可以自己yum下载,也超级简单,我的是阿里云的Centos源和epel源,直接 yum -y install rsync

[root@bogon ~]# yum -y install rsync
[root@bogon ~]# rpm -q rsync
rsync-3.1.2-6.el7_6.1.x86_64
[root@bogon ~]# 

在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步源负责提供文档的原始位置,发起端应对该位置具有读取权限

在这里插入图片描述
rsync作为同步源时以守护进程运行,为其他客户机提供备份源,它的配置文件在/etc/rsyncd.conf,在做同步目录的时候,可以通过修改配置文件,也可以直接通过命令实现,我们主要说第二种简单的方式
配置文件例子:

 uid = nobody
 gid = nobody
 use chroot = yes	//禁锢在源目录
 max connections = 4	//允许同时连接数:4
 address= 192.168.1.103	//监听地址:192.168.1.103
 port 873	//监听端口号:873
 pid file = /var/run/rsyncd.pid	//存放进程ID的文件位置
 log file = /var/log/rsyncd.log	//日志文件存放位置
 hosts allow = 192.168.1.0/24	//允许访问的客户机地址
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress  = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[wwwroot]	//共享模块名称

path = /var/www/html	//源目录的实际路径
read only = yes                         //是否为只读
dont compress  = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  //同步时不再压缩的文件类型
auth users = backuper                  //授权账户
secrets file = /etc/rsyncd_users.db	//存放账户信息的数据文件

执行rsync --daemon命令就可以启动 rsync服务,以独立监听服务的方式运行,若要关闭 rsync服务,可以采取kill进程的方式,如 kill $(cat/var/run/ rsyncd.pid)

rsync的命令

备份的基本格式为“ rsync[选项]原始位置 目标位置”,其中常用的一些命令选项如下所示,具体应根据实际需求选择(如-avz)

-r :递归模式,包含目录及子目录中的所有文件。
-l :对于符号链接文件仍然复制为符号链接文件。
-v :显示同步过程的详细( verbose)信息。
-a :归档模式,保留文件的权限、属性等信息,等同于组合选项 “-rlptgoD”
-z :在传输文件时进行压缩( compress)。
-p :保留文件的权限标记。
-t :保留文件的时间标记。
-g :保留文件的属组标记(仅超级用户使用)。
-o :保留文件的属主标记(仅超级用户使用)。
-H :保留硬连接文件。
-A :保留ACL属性信息。
-D :保留设备文件及其他特殊文件。
--delete:删除目标位置有而原始位置没有的文件。
--checksum:根据校验和(而不是文件大小、修改时间)来决定是否跳过文件。

配置源的表示方法

rsync -avz --delete 用户名@主机地址:共享目录 本地目录

现在模拟一下远程同步实验

建立发布的共享目录和文件

[root@bogon ~]# mkdir /rsync
[root@bogon ~]# touch /rsync/file{1..10}
[root@bogon ~]# ll /rsync/
total 0
-rw-r--r--. 1 root root 0 Apr 28 12:12 file1
-rw-r--r--. 1 root root 0 Apr 28 12:12 file10
-rw-r--r--. 1 root root 0 Apr 28 12:12 file2
-rw-r--r--. 1 root root 0 Apr 28 12:12 file3
-rw-r--r--. 1 root root 0 Apr 28 12:12 file4
-rw-r--r--. 1 root root 0 Apr 28 12:12 file5
-rw-r--r--. 1 root root 0 Apr 28 12:12 file6
-rw-r--r--. 1 root root 0 Apr 28 12:12 file7
-rw-r--r--. 1 root root 0 Apr 28 12:12 file8
-rw-r--r--. 1 root root 0 Apr 28 12:12 file9
[root@bogon ~]# 

客户端同步操作
要注意:如果写的是192.168.1.103:/rsync 表示的是整个目录同步过去
换成192.168.1.103:/rsync/ 多加一个/,就会表示成这个目录里的所有东西

[root@slave ~]# rsync -avz --delete [email protected]:/rsync /var/www/html/
The authenticity of host '192.168.1.103 (192.168.1.103)' can't be established.
ECDSA key fingerprint is 4f:58:1b:4f:30:a5:40:89:34:b3:b7:ab:72:40:df:4d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.103' (ECDSA) to the list of known hosts.
[email protected]'s password:                  
receiving incremental file list
rsync/
rsync/file1
rsync/file10
rsync/file2
rsync/file3
rsync/file4
rsync/file5
rsync/file6
rsync/file7
rsync/file8
rsync/file9

sent 218 bytes  received 548 bytes  139.27 bytes/sec
total size is 0  speedup is 0.00
[root@slave ~]# 
[root@slave ~]# ll /var/www/html/
总用量 0
drwxr-xr-x. 2 root root 137 4月  28 2020 rsync
[root@slave html]# cd rsync/
[root@slave rsync]# ll
总用量 0
-rw-r--r--. 1 root root 0 4月  28 2020 file1
-rw-r--r--. 1 root root 0 4月  28 2020 file10
-rw-r--r--. 1 root root 0 4月  28 2020 file2
-rw-r--r--. 1 root root 0 4月  28 2020 file3
-rw-r--r--. 1 root root 0 4月  28 2020 file4
-rw-r--r--. 1 root root 0 4月  28 2020 file5
-rw-r--r--. 1 root root 0 4月  28 2020 file6
-rw-r--r--. 1 root root 0 4月  28 2020 file7
-rw-r--r--. 1 root root 0 4月  28 2020 file8
-rw-r--r--. 1 root root 0 4月  28 2020 file9
[root@bogon rsync]# 

由于rsync基于底层ssh实现,所以在传输时可以提前生成密钥对,实现免密传输

在这里插入图片描述

[root@slave html]# rsync -avz --delete [email protected]:/rsync2/ /var/www/html/ 
receiving incremental file list
deleting rsync2/linux5
deleting rsync2/linux4
deleting rsync2/linux3
deleting rsync2/linux2
deleting rsync2/linux1
deleting rsync2/
deleting rsync/file9
deleting rsync/file8
deleting rsync/file7
deleting rsync/file6
deleting rsync/file5
deleting rsync/file4
deleting rsync/file3
deleting rsync/file2
deleting rsync/file10
deleting rsync/file1
deleting rsync/
./
linux1
linux2
linux3
linux4
linux5

sent 122 bytes  received 300 bytes  844.00 bytes/sec
total size is 0  speedup is 0.00
[root@slave html]# cd /var/www/html/
[root@slave html]# ls
linux1  linux2  linux3  linux4  linux5

配置 inotify+ rsync实时同步

将 inotify机制与 rsync工具相结合,可以实现触发式备份(实时同步)——只要原始位置的文档发生变化,则立即启动增量备份操作,否则处于静默等待状态

在这里插入图片描述
使用 inotify机制还需要安装 inotify-tools(yum安装为例),以便提供 inotifywait、inotifywatch辅助工具程序,用来监控、汇总改动情况
yum -y install inotify-tools

以监控网站目录/var/www/html为例,可以先执行 inotifywait'命令,然后在另一个终端向/var/www/html目录下添加文件、移动文件,跟踪屏幕输出结果

在这里插入图片描述
删除后,立马会检测到

[root@bogon ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html   #监控变动情况
/var/www/html/ DELETE haha5

参数解释:

-e:指定要监控那些事件
-m:持续监控状态
-r:递归目录
-q:简洁输出信息
这样做虽然是监控到了,但是还需要手动做好计划任务才能定时更新数据,不能追踪实时进行同步数据,所以才会引用到inotify触发式同步。

inotifywait可监控 modify(修改)、create(创建)、move(移动)、delete(删除) , attrib(属性更改)等各种事件,一有变动立即输出结果,而 inotifywatch可用来收集文件系统变动情况,并在运行结束后输出汇总的变化情况。

编写触发式同步脚本

#!/bin/bash
#2020年4月27日15:28:39
inotifywait -mrq -e create,delete,modify,move /var/www/html/ | while read aaa  #将检测到的结果赋值给变量aaa,当成功后开始循环do...done内容
do
        rsync -avz --delete /var/www/html [email protected]:/rsync3
done

赋权:chmod +x rsync.sh

[root@slave ~]# sh rsync.sh &   #这里&表示放到后台执行
[1] 50129
[root@slave ~]# sending incremental file list
deleting html/file5
deleting html/file4
html/

在这期间在另一个slave终端删除操作

[root@slave ~]# cd /var/www/html/
[root@slave html]# ls
file1  file2  file3  file4  file5  haha1  haha2  haha3  haha4
[root@slave html]# rm -rf file5 file4
[root@slave html]#

在服务端查看是否同步过去
在这里插入图片描述
下面内容为后期添加的,测试了一下rsync的增量备份功能,前几天,听朋友说rsync+inotify是把整个目录同步 下来,若只修改了一小部分,整个目录大的话时会同步很慢,我觉得这不是可以增量的吗?

[root@rsync html]# vim 5.txt 
[root@rsync html]# rsync -avz --checksum /var/www/html/ [email protected]:/nfs
[email protected]'s password: 
sending incremental file list
./
5.txt
sent 152 bytes  received 41 bytes  55.14 bytes/sec
total size is 18  speedup is 0.09

########################################################
[root@rsync html]# vim 5.txt 
[root@rsync html]# rsync -avz --checksum /var/www/html/ [email protected]:/nfs
[email protected]'s password: 
sending incremental file list
./
5.txt
sent 157 bytes  received 47 bytes  37.09 bytes/sec
total size is 34  speedup is 0.17

#########################################################
[root@rsync html]# vim 5.txt 
[root@rsync html]# rsync -avz --delete /var/www/html/ [email protected]:/nfs
[email protected]'s password: 
sending incremental file list
./
5.txt
sent 133 bytes  received 47 bytes  51.43 bytes/sec
total size is 44  speedup is 0.24

123.txt没有动过,时间还是原来的时间,只有5.txt改变了,
确实是增量备份
故意和他打赌,有一顿饭有着落了,哈哈
在这里插入图片描述

你们学废了吗???

最后
在这里插入图片描述

原创文章 58 获赞 20 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43815140/article/details/105788030
今日推荐