NFS + inotify + rsync for data and real-time remote mount an incremental backup

NFS Network File System

  • Function: Users can access their own like the local file system using the same file on a remote system network
  • How it works: the user process -> RPC service (portman) -> tcp / ip protocol stack -> remote host RPC service after -> Remote NFS Services -> remote hosts a local disk data -> NFS Services -> tcp / ip protocol stack -> local data
  • Advantages: easy to implement the preceding proxy server when processing the shunt can not find the data sission and data search and other issues, while even played any web server downtime will not affect access to data
  • Disadvantages: a single point of failure all data on the same physical host, once the physical host down the entire project will lead to the loss of stored data inaccessible Solution: Based on inotify + rsync do incremental backups of data in real time to deal with this problem

    NFS build (based startup file / etc / fatab or automatically mount autofs)

  • 包:nfs-utils,rpcbind,tcp_wrappers
  • linux kernel needs support nfs.ko
  • Log: / var / lib / nfs /
  • Configuration file: / etc / exports, / etc / exports.d / * exports.
  • Service open: systemctl start nfs-server

    file format:

  • Share files Host: 192.168.47.117


host:主机1(opt1,opt2) 主机2(opt1,opt2)

单个主机:    # ipv4,ipv6,FQDN
IP networks:# 两种掩码格式均支持 172.18.0.0/255.255.0.0 172.18.0.0/16
wildcards:  # 主机名通配,例如*.xiehegang.online,IP不可以
netgroups:  # NIS域的主机组,@group_name 
anonymous:  # 表示使用*通配所有客户端

#-----------------------------------------------------------------------------------------------------------------------------
option:(ro,sync,root_squash,no_all_squash)


ro,rw             # 只读和读写
async             # 异步,数据变化后不立即写磁盘,性能高
sync              # (1.0.0后为默认)同步,数据在请求时立即写入共享存储磁盘
root_squash       # (默认)远程root映射为nfsnobody,UID为65534,CentOS8 为nobody,早期版本是4294967294(nfsnobody)
no_root_squash    # 远程root映射成root用户
all_squash        # 所有远程用户(包括root)都变成nfsnobody,CentOS8 为nobody
no_all_squash     # (默认)保留共享文件的UID和GID
anonuid和anongid  # 指明匿名用户映射为特定用户UID和组GID,而非nfsnobody,可配合all_squash使用

#-----------------------------------------------------------------------------------------------------------------------------

# 例
/data/app1/  192.168.47.106(ro,sync,no_all_squash)
/data/app2   192.168.47.106(rw,no_rootsquash,all_squash) 
  • Mount host 192.168.47.106

# 查看网络共享情况
showmount -e 192.168.47.117

# 创建挂载点并挂载
mkdir /data/webdata/ -pv;
mount 192.168.47.117:/data/app1  /data/webdata 

# 完成文件的挂载,创建文件,在另一个主机上可以看到, 但是App2挂载点就无法创建文件,因为47.117的配置文件设置了ro
#------------------------------------------------------------------------------------------------------------------------------
# 在/etc/fatab 下实现自动挂载,防止断电导致挂载丢失,**追加**以下内容

192.168.47.117:/data/app1         /data/webdata             nfs     _netdev        0 0

#------------------------------------------------------------------------------------------------------------------------------
# 如果有需求也可像访问ls/msic/cd 访问光盘一样设置实时挂载,即:访问即挂载(aotofs)
# 安装autofs
yum install -y autofs

# 编辑配置文件 vi /etc/auto.misc 追加内容
nfs             -fstype=nfs             192.168.47.117:/data/app1

# 开启autofs服务
systemctl start autofs

# 访问目录:
cd /msic/app1 

Based on inotify + rsync achieve incremental backup

  • NFS backup Host: 192.168.47.129
  • linux kernel version: more than 2.6.13

# 修改内核参数:
vim /etc/sysctl.conf
fs.inotify.max_queued_events=66666
fs.inotify.max_user_watches=100000

# 重新应用 
sysctl -p

# 查看修改情况
cat /proc/sys/fs/inotify/*

inotify-tools Tool

  • inotify-tools installation tools: yum install inotify-tools (yum -y install epel-release epel source is not installed)
  • inotifywait command options:

-m, --monitor       # 始终保持事件监听
-d, --daemon        # 以守护进程方式执行,和-m相似,配合-o使用
-r, --recursive     # 递归监控目录数据信息变化
-q, --quiet         # 输出少量事件信息
--exclude           # 指定排除文件或目录,使用扩展的正则表达式匹配的模式实现
--excludei          # 和exclude相似,不区分大小写
-o, --outfile       # 打印事件到文件中,相当于标准正确输出,注意:使用绝对路径
-s, --syslogOutput  # 发送错误到syslog相当于标准错误输出
--timefmt           # 指定时间输出格式
--format            # 指定的输出格式;即实际监控输出内容
-e                  # 指定监听指定的事件,如果省略,表示所有事件都进行监听

# --timefmt 格式 --------------------------------------------------------------------------------------------------------------------

%Y # 年份信息,包含世纪信息
%y # 年份信息,不包括世纪信息
%m # 显示月份,范围 01-12
%d # 每月的第几天,范围是 01-31
%H # 小时信息,使用 24小时制,范围 00-23
%M # 分钟,范围 00-59

例:--timefmt "%Y-%m-%d %H:%M"

# --format 格式 --------------------------------------------------------------------------------------------------------------------
%T  # 输出时间格式中定义的时间格式信息,通过 --timefmt option 语法格式指定时间信息
%w  # 事件出现时,监控文件或目录的名称信息
%f  # 事件出现时,将显示监控目录下触发事件的文件或目录信息,否则为空
%e  # 显示发生的事件信息,不同的事件默认用逗号分隔
%Xe # 显示发生的事件信息,不同的事件指定用X进行分隔

例:--format "%T %w%f event: %;e"
# -e 格式 --------------------------------------------------------------------------------------------------------------------
create          # 文件或目录创建
delete          # 文件或目录被删除
modify          # 文件或目录内容被写入
attrib          # 文件或目录属性改变
close_write     # 文件或目录关闭,在写入模式打开之后关闭的
close_nowrite   # 文件或目录关闭,在只读模式打开之后关闭的
close           # 文件或目录关闭,不管读或是写模式
open            # 文件或目录被打开
moved_to        # 文件或目录被移动到监控的目录中
moved_from      # 文件或目录从监控的目录中被移动
move            # 文件或目录不管移动到或是移出监控目录都触发事件
access          # 文件或目录内容被读取
delete_self     # 文件或目录被删除,目录本身被删除
unmount         # 取消挂载

例: -e create,delete,moved_to,close_write, attrib

inotify complete usage examples

# 监控一次性事件
inotifywait /data
# 持续前台监控
inotifywait -mrq /data
# 持续后台监控,并记录日志
inotifywait -o /root/inotify.log -drq /data --timefmt "%Y-%m-%d %H:%M" --format
"%T %w%f event: %e"
# 持续前台监控特定事件
inotifywait -mrq /data --timefmt "%F %H:%M" --format "%T %w%f event: %;e" -e
create,delete,moved_to,close_write,attrib

rsync achieve incremental backup

  • 包 rsync
  • Service file: /usr/lib/systemd/system/rsyncd.service
  • Profile: /etc/rsyncd.conf
  • Port: 873 / tcp
  • Format: rsync [OPTION ...] SRC ... [DEST]

rsync有三种工作方式:
1. 本地文件系统上实现同步。命令行语法格式为上述"Local"段的格式。
2. 本地主机使用远程shell和远程主机通信。命令行语法格式为上述"Access via remote shell"段的格
式。
3. 本地主机通过网络套接字连接远程主机上的rsync daemon。命令行语法格式为上述"Access via
rsync daemon"段的格式。

The present embodiment using a separate synchronization run rsync

  • Independent service run rsync (192.168.47.117)

# 安装
 yum -y install rsync

#创建rsync服务器的配置文件
vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
reverse lookup = no
hosts allow = 192.168.47.0/24
[backup]
path = /backup/
comment = backup
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pass

# 服务器端准备目录

cd /; mkdir /backup

# 服务器端生成验证文件 
echo "rsyncuser:justin" > /etc/rsync.pass
chmod 600 /etc/rsync.pass

#启动服务
/usr/bin/rsync --daemon ,rsync --daemon
  • The backup server 192.168.47.129

#客户端配置密码文件
echo "justin" > /etc/rsync.pass
chmod 600 /etc/rsync.pass 

#客户端测试同步数据
rsync -avz --delete --password-file=/etc/rsync.pass /data/app1   [email protected]::/backup

Create a script file, pull real-time, based on inotify + rsync


vim  /etc/profile.d/bf.sh 
#!/bin/bash
SRC='/data/app'
DEST='[email protected]::backup'
inotifywait -mrq --timefmt '%Y-%m-%d %H:%M' --format '%T %w %f' -e
create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR
FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo
"At ${TIME} on ${DATE}, file $FILEPATH was backuped up via rsync" >>
/var/log/changelist.log
done

. /etc/profile.d/bf.sh 

final effect

Server / data / file webdata of the actual position of 47.106 to 47.117 / data / App1,
/ Backup file server data backup 47.117 47.129 / data / App1 to a local folder

Of course, this is my experiment, there may be errors in the process and, if found Comments welcome, thank you ~

Guess you like

Origin www.cnblogs.com/Justin0717/p/12074755.html