avatar

4.NFS服务实时备份

NFS服务实时备份

第一章 NFS单点问题

1.为什么需要解决NFS单点问题

只有一个NFS服务器时,当出现故障时,数据没办法再实现共享,既不安全也不稳定

2. 解决思路

1).监控文件是否发生变化

2)如果发生变化,就通过rsync传输到备份服务器,并且数据保持完全一致

3)备份服务器也需要提供NFS服务,以便NFS服务器故障的时候可以顶替,继续提供NFS服务

4)假如发生了故障,客户端需要先卸载原有已经挂载的目录,然后重新挂载到备份服务器的NFS共享目录

3. 需要用到的软件

1).监控文件是否发生变化 inotify-tools

2).实时备份 rsync+inotify-tools / sersync / lsyncd

3).提供NFS服务 NFS+RPC

4. 环境准备

操作系统 服务器角色 IP地址
CentOS 7.4 x86_64 NFS服务端(nfs-server) 172.16.1.31
CentOS 7.4 x86_64 NFS备份服务器(backup-server) 172.16.1.41

第二章 Inotify简介

Inotify是一种强大的,细粒度的,异步的文件系统事件监视机制,Linux2.6.13起加入了inotify支持,通过inotify可以监控文件系统中添加,删除,修改,移动等各种事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools正是实施这样监控的软件,另外一个这样效果的软件是中国人周洋在金山公司开发的sersync,还有一个配置更简单的软件叫lsyncd

2.1 查看系统内核

查看当前系统内核是否支持inotify

1
2
[root@nfs ~]# uname -r
3.10.0-693.el7.x86_64

2.2 安装Inotify

1
2
[root@nfs ~]# yum search inotify  #查找inotify包名
[root@nfs ~]# yum -y install inotify-tools

2.3 关键参数说明:

1)在/proc/sys/fs/inotify目录下有三个文件,对inotify机制有一定限制

1
2
3
4
5
6
7
8
9
[root@nfs ~]# ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Jul 19 11:37 max_queued_events
-rw-r--r-- 1 root root 0 Jul 19 09:52 max_user_instances
-rw-r--r-- 1 root root 0 Jul 19 11:37 max_user_watches
########################################################
max_queued_events =====>设置inotify设置inotify实例事件(event)队列可容纳的事件数量
max_user_instances =====>设置每个用户可以运行的inotify或者inotifywatch命令的进程数
max_user_watches =====>设置inotifywait或者inotifywatch命令可以监视的文件数量(单进程)

2.4 inotifywait详细参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
[root@nfs ~]# inotifywait --help
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-h|--help Show this help text.
@<file> Exclude the specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
extended regular expression <pattern>.
--excludei <pattern>
Like --exclude but case insensitive.
###排除文件或目录,不区分大小写
-m|--monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-d|--daemon Same as --monitor, except run in the background
logging events to a file specified by --outfile.
Implies --syslog.
-r|--recursive Watch directories recursively.
###递归查询目录
--fromfile <file>
Read files to watch from <file> or `-' for stdin.
-o|--outfile <file>
Print events to <file> rather than stdout.
-s|--syslog Send errors to syslog rather than stderr.
-q|--quiet Print less (only print events).
###打印很少的信息,仅仅打印监控事件的信息,安静的
-qq Print nothing (not even events).
--format <fmt> Print using a specified printf-like format ###打印指定输出类似格式字符串
string; read the man page for more details.
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string. ###指定时间输出的格式
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for. ###通过次参数可以指定需要监控的事件

Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time.

Events:
access file or directory contents were read
###文件或目录被读取
modify file or directory contents were written
###文件或目录被修改
attrib file or directory attributes changed
###文件或目录属性被改变
close_write file or directory closed, after being opened in
writeable mode ###文件或目录封闭,无论读/写模式
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened ###文件目录被打开
moved_to file or directory moved to watched directory ###文件或目录被移动到另一个目录
moved_from file or directory moved from watched directory
move file or directory moved to or from watched directory
create file or directory created within watched directory ###文件或目录被创建在当前目录
delete file or directory deleted within watched directory ###文件或目录被删除
delete_self file or directory was deleted
unmount file system containing file or directory unmounted ###文件系统被卸载

2.5 参数说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
inotifywait参数说明
参数名称 参数说明
-m ,-monitor 始终保持事件监听状态
-r,-recursive 递归查询目录
-q,-quiet 只打印监控事件的信息
-exclude 排除文件或目录时,不区分大小写
-t,-timeout 超时时间
-timefmt 指定时间输出格式
-format 指定时间输出格式
-e,event 后面指定增,删,改等事件
inotifywait events 事件说明
access 读取文件或目录内容
modify 修改文件或目录内容
attrib 文件或目录的属性改变
close_write 修改真实文件内容
close_nowrite
close
open 文件或目录被打开
moved_to 文件或目录移动到
moved_from 文件或目录从...移动到
move 移动文件或目录移动到监视目录
create 在监视目录下创建文件或目录
delete 删除监视目录下的文件或目录
delete_self
umount 卸载文件系统

2.6 查看inotify默认参数

1
2
3
4
5
6
7
[root@nfs ~]# sysctl -a|grep max_queued_events
fs.inotify.max_queued_events = 327679
[root@nfs ~]# sysctl -a |egrep max_user_watches
fs.epoll.max_user_watches = 412098
fs.inotify.max_user_watches = 50000000
[root@nfs ~]# sysctl -a |egrep max_user_instances
fs.inotify.max_user_instances = 128

2.7 测试

测试inotify-tools监控文件发生变化

1
2
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create /backup
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write /backup

监控脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat inotify_push.sh 
#!/bin/bash

Path=/data
backup_Server=172.16.1.41
export RSYNC_PASSWORD=oldboy

/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | while read line
do
echo "${line}"
if [ -f $line ];then
rsync -azvP $line --delete rsync_backup@$backup_Server::data
else
cd $Path &&\
rsync -azvP ./ --delete rsync_backup@$backup_Server::data
fi
done

2.8 inotify优缺点

inotify优点
1)监控文件系统事件变化,通过同步工具实现实时数据同步
inotify缺点
1)并发如果大于200个文(10-100k),同步就会由延迟
2)我们前面的脚本,每次都是全部推送一次,但是确实是增量的,也可以只同步变化的文件
3)监控到事件后,调用rsync同步是单进程(加&并发)sersync多进程同步
既然有了inotify-tools,为什么还需要sersync呢?

sersync功能多:(inotify+rsync命令)

1)支持通过配置文件管理
2)真正的守护进程socket
3)可以对失败文件定时重传(定时任务功能)
4)第三方的HTTP接口(例如:更新cdn缓存)
5)默认多线程rsync同步

第三章sersync安装部署

操作系统 服务器角色 IP地址
CentOS 7.4 x86_64 NFS服务端(nfs-server) 172.16.1.31
CentOS 7.4 x86_64 NFS备份服务器(backup-server) 172.16.1.41

3.1 下载sersync

1
[root@nfs ~]# wget https://github.com/wsgzao/sersync/blob/master/sersync2.5.4_64bit_binary_stable_final.tar.gz

3.2 解压

1
2
3
4
5
[root@nfs /opt]# tree server/
server/
├── confxml.xml
├── confxml.xml.bak
└── sersync2

3.3修改配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<sersync>
24 <localpath watch="/data">
25 <remote ip="172.16.1.42" name="/data"/>
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
29 <rsync>
30 <commonParams params="-az"/>
31 <auth start="true" users="rsynd_backup" passwordfile="/etc/rsyncd.conf"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="true" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>
36 <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
37 <crontab start="false" schedule="600"><!--600mins-->
38 <crontabfilter start="false">

3.4 查看帮助说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@nfs /opt/server]#  ./sersync2 -h
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序

3.5 启动服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@nfs /opt/server]# ./sersync2 -n 20 -dro confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -n thread num is: 20
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: confxml.xml
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is rsynd_backup
passwordfile is /etc/rsyncd.conf
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 22 = 1(primary thread) + 1(fail retry thread) + 20(daemon sub threads)
Max threads numbers is: 42 = 22(Thread pool nums) + 20(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -az -R --delete ./ --timeout=100 rsynd_backup@172.16.1.42::/data --password-file=/etc/rsyncd.conf >/dev/null 2>&1
run the sersync:
watch path is: /data

3.6 测试数据同步传输

nfs服务器生成数据

1
2
[root@nfs ~]# cd /data
[root@nfs /data]# for i in {1..1000};do echo "${i}"; echo "${i}" > ${i}.txt;sleep 0.1;done

backup备份服务器查看

1
2
[root@backup ~]# cd /data
[root@backup /data]# while true ;do ls |wc -l;sleep 0.1;done

第四章 lsyncd 服务

官网地址:https://github.com/axkibe/lsyncd

Lysncd 实际上是lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过rsync去差异同步,达到实时的效果。我认为它最令人称道的特性是,完美解决了 inotify + rsync海量文件同步带来的文件频繁发送文件列表的问题 —— 通过时间延迟或累计触发事件次数实现。另外,它的配置方式很简单,lua本身就是一种配置语言,可读性非常强。lsyncd也有多种工作模式可以选择,本地目录cp,本地目录rsync,远程目录rsyncssh。实现简单高效的本地目录同步备份(网络存储挂载也当作本地目录),一个命令搞定。

4.1 安装lsyncd

操作系统 服务器角色 IP地址
CentOS 7.4 x86_64 NFS服务端(nfs-server) 172.16.1.31
CentOS 7.4 x86_64 NFS备份服务器(backup-server) 172.16.1.41

查看包名

1
2
3
[root@nfs ~]# yum provides lsyncd
lsyncd-2.2.2-1.el7.x86_64 : File change monitoring and synchronization daemon
Repo : epel

安装lsyncd服务

1
[root@nfs ~]# yum -y install lsyncd

4.2 创建配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
[root@nfs ~]# rpm -qc lsyncd
/etc/lsyncd.conf
[root@nfs /data]# cat /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
}
sync {
default.rsync,
source = "/data",
target = "rsync_backup@172.16.1.41::data",
delete= true,
exclude = { ".*" },
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.passwd",
_extra = {"--bwlimit=200"}
}
}
sync {
default.rsync,
source = "/backup",
target = "rsync_backup@172.16.1.41::backup",
delete= true,
exclude = { ".*" },
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
archive = true,
compress = true,
verbose = true,
password_file = "/etc/rsync.passwd",
_extra = {"--bwlimit=200"}
}
}

4.3 配置文件说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
settings

里面是全局设置,--开头表示注释,下面是几个常用选项说明:

logfile #定义日志文件
stausFile #定义状态文件
nodaemon=true #表示不启用守护模式,默认
statusInterval #将lsyncd的状态写入上面的statusFile的间隔,默认10秒
inotifyMode #指定inotify监控的事件,默认是CloseWrite,还可以是Modify或CloseWrite or Modify
maxProcesses #同步进程的最大个数。假如同时有20个文件需要同步,而maxProcesses = 8,则最大能看到有8个rysnc进程
maxDelays #累计到多少所监控的事件激活一次同步,即使后面的delay延迟时间还未到
sync

里面是定义同步参数,可以继续使用maxDelays来重写settings的全局变量。一般第一个参数指定lsyncd以什么模式运行:rsync、rsyncssh、direct三种模式:

default.rsync #本地目录间同步,使用rsync,也可以达到使用ssh形式的远程rsync效果,或daemon方式连接远程rsyncd进程;
default.direct #本地目录间同步,使用cp、rm等命令完成差异文件备份;
default.rsyncssh #同步到远程主机目录,rsync的ssh模式,需要使用key来认证

source 同步的源目录,使用绝对路径。

target 定义目的地址.对应不同的模式有几种写法:
/tmp/dest #本地目录同步,可用于direct和rsync模式
172.16.1.41:/data #同步到远程服务器目录,可用于rsync和rsyncssh模式,拼接的命令类似于/usr/bin/rsync -ltsd --delete --include-from=- --exclude=* SOURCE TARGET,剩下的就是rsync的内容了,比如指定username,免密码同步
172.16.1.41::data #同步到远程服务器目录,用于rsync模式
三种模式的示例会在后面给出。

init 这是一个优化选项,当init = false,只同步进程启动以后发生改动事件的文件,原有的目录即使有差异也不会同步。默认是true

delay 累计事件,等待rsync同步延时时间,默认15秒(最大累计到1000个不可合并的事件)。也就是15s内监控目录下发生的改动,会累积到一次rsync同步,避免过于频繁的同步。(可合并的意思是,15s内两次修改了同一文件,最后只同步最新的文件)

excludeFrom 排除选项,后面指定排除的列表文件,如excludeFrom = "/etc/lsyncd.exclude",如果是简单的排除,可以使用exclude = LIST。
这里的排除规则写法与原生rsync有点不同,更为简单:
监控路径里的任何部分匹配到一个文本,都会被排除,例如/bin/foo/bar可以匹配规则foo
如果规则以斜线/开头,则从头开始要匹配全部
如果规则以/结尾,则要匹配监控路径的末尾
?匹配任何字符,但不包括/
*匹配0或多个字符,但不包括/
**匹配0或多个字符,可以是/
delete 为了保持target与souce完全同步,Lsyncd默认会delete = true来允许同步删除。它除了false,还有startup、running值,请参考 Lsyncd 2.1.x ‖ Layer 4 Config ‖ Default Behavior。

4.4 启动服务

1
2
[root@nfs ~]# systemctl start lsyncd.service
[root@nfs ~]# systemctl status lsyncd.service

4.5 查看日志

1
2
[root@nfs /data]# tail -f /var/log/lsyncd/lsyncd.log 
[root@nfs /data]# tail -f /var/log/lsyncd/lsyncd.status

4.6 测试数据

1
[root@nfs /data]# for i in {1..1000};do echo "${i}"; echo "${i}" > ${i}.txt;sleep 0.1;done

backup备份服务器查看

1
2
[root@backup ~]# cd /data
[root@backup /data]# while true ;do ls |wc -l;sleep 0.1;done
文章作者: Wu Fei
文章链接: http://linuxwf.com/2020/04/13/4-NFS%E6%9C%8D%E5%8A%A1%E5%AE%9E%E6%97%B6%E5%A4%87%E4%BB%BD/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 WF's Blog
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论