温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

rsync+inotify实现数据实时同步备份

发布时间:2020-06-06 05:22:07 来源:网络 阅读:1110 作者:翘楚秦歌 栏目:建站服务器

在实际生产环境当中,我们总会遇见需要把一些重要数据进行备份,且随着应用系统规模的增大,对数据的安全性、可靠性、时效性要求还是比较高的,

因此我自己有在用rsync+inotify来实现数据实时同步备份,下面记录下操作步骤,以防日后自己忘记。

实验背景:

操作系统          IP         机器名        角色

CentOS 7.2       172.16.22.1     nginx01        数据源(服务器端)

CentOS 7.2       172.16.22.7     nginx02        备份地(客户端)


一、rsync的安装

在服务器端与客户端都安装rsync

[root@nginx01 ]# yum install -y rsync
[root@nginx02 ]# yum install -y rsync


分别启动服务器端和客户端的rsync守护进程

[root@nginx01 ]# /usr/bin/rsync --daemon
[root@nginx02 ]# /usr/bin/rsync --daemon


二、安装inotify

因为inotify是一种强大的、细粒度的、异步文件系统事件监控机制,从Linux内核2.6.13版本起,加入了对inotify的支持。

inotify可以监控文件系统的各种变化,当文件出现任何变动时,就会触发rsync同步,恰好解决了数据实时同步的问题。

inotify只需要安装在服务器端即可。

[root@nginx01 ]# uname -r
3.10.0-327.el7.x86_64
[root@nginx01 ]# ll /proc/sys/fs/inotify
total 0
-rw-r--r--. 1 root root 0 Jun 12 13:43 max_queued_events
-rw-r--r--. 1 root root 0 Jun 12 13:43 max_user_instances
-rw-r--r--. 1 root root 0 Jun 12 13:43 max_user_watches

如果出现了上面的3项内容,说明系统默认支持inotify,那么我们就可以安装inotify-tools了。


我们可以到http://inotify-tools.sourceforge.net 下载相应版本的inotify-tools,我发现最新的也就是2010年出的inotify-tool-3.1.14

下载完毕后,进行安装

先解压

[root@nginx01 ]# tar -xf inotify-tools-3.14.tar.gz

进入解压后的安装包目录里

[root@nginx01 ]# cd inotify-tools-3.14


进行检查编译,并制定安装路径

[root@nginx01 inotify-tools-3.14 ]# ./configure --prefix=/data0/inotify


进行make编译安装

[root@nginx01 inotify-tools-3.14 ]# make && make install


安装完毕,查看是否生成了inotifywait、inotifywatch这两个指令

[root@nginx01 ]# ll /data0/inotify/bin/inotifywa*
-rwxr-xr-x. 1 root root 60892 Jun 12 13:45 /data0/inotify/bin/inotifywait
-rwxr-xr-x. 1 root root 55183 Jun 12 13:45 /data0/inotify/bin/inotifywatch

注:inotifywait --用于等待文件或者文件集上的一个特定时间,可以监控任何文件和目录设定,并且可递归监控整个目录树;inotifywatch--用于收集被监控的文件系统统计数据,包括每个inotify事件发生的次数等相关信息。关于它们的用法可以使用 /data0/inotify/bin/inotifywait --help、/data0/inotify/bin/inotifywatch --help的方式来了解


三、inotify的相关参数

inotify定义了一些接口参数,可以用来限制inotify消耗kernel memory的大小,所以我们要根据实际应用的需求,来调节其大小。

[root@nginx01 ]# ll /proc/sys/fs/inotify
total 0
-rw-r--r--. 1 root root 0 Jun 12 13:43 max_queued_events
-rw-r--r--. 1 root root 0 Jun 12 13:43 max_user_instances
-rw-r--r--. 1 root root 0 Jun 12 13:43 max_user_watches


/proc/sys/fs/inotify/max_queued_events --表示调用inotify_init时分配至inotify instance中可以排队的最大事件数,一旦超过这个值,事件就会被抛弃,但是会触发IN_Q_OVERFLOW事件

/proc/sys/fs/inotify/max_user_instances --表示每一个real user ID 可创建的inotify instances数量的最大上限

/proc/sys/fs/inotify/max_user_watches --表示每个inotify实例相关联的watches的上限,也就是每个inotify实例可监控的最大目录数量,如果你所需要监控的数量巨大,可以适当增大它。


我就随便增大了我的上述设定值

[root@nginx01 ]# echo 32768 > /proc/sys/fs/inotify/max_queued_events
[root@nginx01 ]# echo 1024 > /proc/sys/fs/inotify/max_user_instances
[root@nginx01 ]# echo 90000000 > /proc/sys/fs/inotify/max_user_watches

四、配置双机ssh信任

使用rsync同步有两种方式可以实现:

一:使用rsync用户、密码

二:使用机器上的用户,无需输密码即可实现

我为了贪图方便选择后者


在数据源、备份机上创建RSA密钥

以下操作在两台机器上都要执行,下列提供其中一台的为例

1.使用root用户登入机器

2.在root用户的主目录下创建.ssh目录,并设置正确的权限

[root@nginx01 ]# mkdir ~/.ssh
[root@nginx01 ]# chmod 700 ~/.ssh

3.使用ssh-keygen命令生成第2版的SSH协议的RSA密钥

[root@nginx01 ]# ssh-keygen -t rsa

接下来的动作就是根据提示保存私钥(key)和公钥(public key)的位置时,使用默认值。如果需要私钥密码(passphrase), 则输入一个私钥密码(如果使用私钥密码,在利用ssh执行远程命令是需要输入私钥密码)

我是为了方便,直接回车就好!


添加密钥到授权的密钥文件中

1.使用root用户登入机器

2.在数据源机器上执行下列操作

[root@nginx01 ]# cd ~/.ssh
[root@nginx01 ./ssh]# ssh 172.16.22.7 cat /root/.ssh/id_rsa.pub >> authorized_keys
[root@nginx01 ./ssh]# ssh 172.16.22.1 cat /root/.ssh/id_rsa.pub >> authorized_keys
[root@nginx01 ./ssh]# scp authorized_keys 172.16.22.7:/root/.ssh/
[root@nginx01 ./ssh]# chmod 600 /root/.ssh/authorized_keys

3.在备份机上执行

[root@nginx02 ]# chmod 600 /root/.ssh/authorized_keys

4.分别在两台机器上进行测试

[root@nginx01 ]# ssh 172.16.22.1 date
Tue Jun 13 20:39:56 CST 2017
[root@nginx01 ]# ssh 172.16.22.7 date
Tue Jun 13 20:39:57 CST 2017
[root@nginx02 ]# ssh 172.16.22.1 date
Tue Jun 13 20:39:57 CST 2017
[root@nginx02 ]# ssh 172.16.22.7 date
Tue Jun 13 20:39:58 CST 2017

在第一次执行的时候,可能会要输入密码信息,再次执行的时候,你会发现不需要输入密码信息就能够显示系统日期,这就说明ssh互相信任配置成功!


五、实现实时同步

因为inotifywait是一个监控等待事件,所以我们可以使用shell脚本来配合使用它。

常用的inotifywait参数:

 -m --monitor 表示始终保持事件的监听状态

 -r --recursive 表示递归查询目录

 -q --quit 表示打印出监控事件

 -e --event  通过此参数可以指定要监控的事件,常见的有modify、delete、create、attrib等等

 

下面是我的一个实时同步脚本,仅供参考:

[root@nginx01 ]# vim  /data0/webscripts/inotify.sh
#!/bin/bash
SRC=/data0/nginx/res/home/
DST=root@172.16.22.7:/data0/nginx/res/home/
/data0/inotify/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read  a b c 
        do 
           /usr/bin/rsync  -ahqzt --delete $SRC $DST
done


让同步脚本具有可执行权限

[root@nginx01 ]# chmod +x /data0/webscripts/inotify.sh

在后台执行实时备份的脚本操作

[root@nginx01 ]# sh /data0/webscripts/inotify.sh &

在备份的同时,你可以去到你的备份机上备份目录里查看它是不是一直在变大,直到和数据源目录一样大呢

如果你的备份目录比较大,从数字上可能不是那么好辨认的话,建议使用查看目录文件数量的方式来比对确认


如在数据源目录查看后,在去备份目录看是否一致。

[root@nginx01 ]# find /data0/nginx/res/home -type f | wc -l
30777
[root@nginx02 ]# find /data0/nginx/res/home -type f | wc -l
30777

到这里配置完成,测试成功

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI