XFS文件系统的备份和恢复

XFS文件系统作为Centos7默认的文件系统,与以往的ext文件系统相比有以下特点:

  • 专为大数据而生,单个文件系统最大可支持到8EB(1TB=1024GB,1PB=1024TB,1EB=1024PB)
  • 单个文件可以支持到16TB
  • 在备份前不需要卸载

XFS提供了xfsdump和xfsrestore工具协助备份文件系统中的数据,XFS 的备份和恢复的过程是可以被中断然后继续的,无须冻结文件系统。xfsdump 甚至提供了高性能的多线程备份操做——它把一次 dump 拆分成多个数据流,每个数据流可以被发往不同的目的地。
用xfsdump备份之前先了解一下以下两种备份级别:
0:完全备份(缺省)
1~9:增量备份
扩展:

名称说明
完全备份即每次都把指定的文件备份一遍,不管文件有没有发生变化
增量备份只针对之前做过的备份之后有变化的文件进行备份
差异备份将上一次完整备份后有变化的文件进行备份

在这里插入图片描述
环境准备:

在VMwareWorkstation中为Centos7.3添加一块虚拟磁盘。
先对磁盘进行创建分区:

[root@localhost ~]# fdisk /dev/sdb 
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xe37c8df1.

Command (m for help): n       #新建一个分区
Partition type:
p   primary (0 primary, 0 extended, 4 free)
e   extended
Select (default p): p       #创建一个主分区
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 	41943039): +5G      #指定分区大小       
Partition 1 of type Linux and of size 5 GiB is set

Command (m for help): p     #打印分区表

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xe37c8df1

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    10487807     5242880   83  Linux

Command (m for help): w     #保存
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 

查看一下

[root@localhost ~]# ls /dev/sdb*

/dev/sdb  /dev/sdb1    #成功创建分区

接下来格式化分区并挂载:

[root@localhost ~]# mkfs.xfs /dev/sdb1 
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=327680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data  =                          bsize=4096   blocks=1310720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=2560, version=2
        =                        sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# mkdir /xfstest
[root@localhost ~]#mount /dev/sdb1 /xfstest

准备备份测试文件:

[root@localhost ~]# cd /xfstest/
[root@localhost xfstest]# cp /etc/passwd
passwd   passwd-  
[root@localhost xfstest]# cp /etc/passwd ./
[root@localhost xfstest]# ls
passwd
[root@localhost xfstest]# ll
total 4
-rw-r--r--. 1 root root 846 Feb 21 01:07 passwd
[root@localhost xfstest]# mkdir test
[root@localhost xfstest]# touch test/a.tst
[root@localhost xfstest]# tree /xfstest/
/xfstest/
├── passwd
└── test
	└── a.tst

1 directory, 2 files
[root@localhost xfstest]# 

用tree命令我们可以看到目录下树形结构关系,接下来开始备份:

1.使用xfsdump备份整个分区:

使用方法:xfsdump -f 【备份后的路径】 【要备份的文件或者分区】

注意:要备份的文件路径不能写成/dev/sdb1/ (后面不能有/)

[root@localhost xfstest]# xfsdump -f /opt/dump_xfs /dev/sdb1
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control

 ============================= dump label dialog 	==============================

please enter label for this dump session (timeout in 300 sec)
 -> dump_sdb1   #指定一个备份会话标签
session label entered: "dump_sdb1"

 --------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of localhost.localdomain:/xfstest
xfsdump: dump date: Fri Feb 21 01:18:39 2020
xfsdump: session id: ec0b8c2f-35c7-4f38-8d26-f607098ef674
xfsdump: session label: "dump_sdb1"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 25856 bytes
xfsdump: /var/lib/xfsdump/inventory created

============================= media label dialog 	=============================

please enter label for media in drive 0 (timeout in 300 sec)
 -> sdb1    #指定设备标签,对要备份的文件做一个描述
media label entered: "sdb1"

 --------------------------------- end dialog ---------------------------------

xfsdump: creating dump session media file 0 (media 0, file 0)
xfsdump: dumping ino map
xfsdump: dumping directories
xfsdump: dumping non-directory files
xfsdump: ending media file
xfsdump: media file size 24016 bytes
xfsdump: dump size (non-dir files) : 1056 bytes
xfsdump: dump complete: 45 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /opt/dump_xfs OK (success)
xfsdump: Dump Status: SUCCESS     #操作成功
[root@localhost xfstest]# 

为了省去以上备份的交互式操作我们还可以使用以下方式进行操作:

[root@localhost xfstest]# xfsdump -f /opt/dump_xfs /dev/sdb1 -L 	/dump_sdb1 -M mda1

-L:xfsdump 纪录每次备份的 session 标头,也就是我们指定的会话标签
-M:xfsdump 可以纪录储存媒体的标头,这里可以填写此媒体的简易说明

恢复文件

使用方法:xfsrestore -f 【曾经备份过的文件名】 【恢复到哪里】

[root@localhost xfstest]# rm -rf ./*  #删除挂载目录下的文件
[root@localhost xfstest]# ls
[root@localhost xfstest]# xfsrestore -f /opt/dump_xfs /xfstest/    #恢复文件
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and 	control
xfsrestore: searching media for dump
xfsrestore: examining media file 0
xfsrestore: dump description: 
xfsrestore: hostname: localhost.localdomain
xfsrestore: mount point: /xfstest
xfsrestore: volume: /dev/sdb1
xfsrestore: session time: Fri Feb 21 01:18:39 2020
xfsrestore: level: 0
xfsrestore: session label: "dump_sdb1"
xfsrestore: media label: "sdb1"
xfsrestore: file system id: 6b43eb38-36fd-485c-a7d1-fd8a166e16f7
xfsrestore: session id: ec0b8c2f-35c7-4f38-8d26-f607098ef674
xfsrestore: media id: aea32d38-7419-43eb-bb33-c61a9bcfc17d
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 2 directories and 3 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 1 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /opt/dump_xfs OK (success)
xfsrestore: Restore Status: SUCCESS
[root@localhost xfstest]# ls
passwd  test		#成功恢复文件
[root@localhost xfstest]# 

注意事项:使用xfsdump时,请注意下面的几个限制:
1.xfsdump不支持没有挂载的文件系统备份,所以只能备份已挂载的;
2.xfsdump必须使用root权限才能操作;
3.xfsdump只适用于xfs文件系统;
4.xfsdump备份的数据只能让xfsrestore解析;
5.xfsdump是透过文件系统的UUID来分辨各个备份挡的,因此不能备份两个具有相同UUID的文件系统

2.增量备份文件:

先做一次完全备份

[root@localhost xfstest]# xfsdump -f /opt/full_back /xfstest -L backmsg -M bak 

增加一些内容,然后做第一次增量备份:

 [root@localhost xfstest]# touch a.txt
[root@localhost xfstest]# touch b.txt
[root@localhost xfstest]# touch /xfstest/
[root@localhost xfstest]# tree /xfstest/
/xfstest/
├── a.txt
├── b.txt
└── passwd

0 directories, 3 files
[root@localhost xfstest]# xfsdump -l 1 -f /opt/full_back1 /xfstest -L bsk -M nsgbak

再增加一些内容,然后做第二次增量备份:

[root@localhost xfstest]# tree /xfstest/
/xfstest/
├── a.txt
├── b.txt
├── passwd
└── test
	├── c.txt
	└── d.txt

1 directory, 5 files
[root@localhost xfstest]# xfsdump -l 2 -f /opt/full_back2 /xfstest -L bsk2 -M nsgbak2

.恢复数据:
先恢复第一次做备份的数据(完全备份):

[root@localhost xfstest]# rm -rf ./*
[root@localhost xfstest]# ls
[root@localhost xfstest]# xfsrestore -f /opt/full_back /xfstest 
[root@localhost xfstest]# tree /xfstest/
/xfstest/
└── passwd

0 directories, 1 file
[root@localhost xfstest]# 

再恢复第二次备份的数据(第1次增量备份):

[root@localhost xfstest]# xfsrestore -f /opt/full_back1 /xfstest
[root@localhost xfstest]# tree /xfstest/
/xfstest/
├── a.txt
├── b.txt
└── passwd

0 directories, 3 files

最后恢复第三次备份的数据(第2次增量备份):

[root@localhost xfstest]# xfsrestore -f /opt/full_back2 /xfstest
[root@localhost xfstest]# tree /xfstest/
/xfstest/
├── a.txt
├── b.txt
├── passwd
└── test
	├── c.txt
	└── d.txt

1 directory, 5 files
[root@localhost xfstest]# 

到此,数据已经按照我们先后创建的文件恢复成功了。
注意:
1.先恢复完全备份
2.(情况1)如果连续的两次增量备份指定的级别都是1,那么只需要恢复后一个增量备份的文件就可以了
3.(情况2)如果你第一次增量备份指定的是1级别,第二次指定的是2级别,那么恢复的时候必须先恢复级别1的备份文件,再恢复级别2的,否则恢复后造成颠倒

小结
增量备份的优点:没有重复的备份数据,因此备份的数据量不大,备份所需的时间很短
增量备份的缺点:数据恢复相对比较麻烦,它需要上一次全备份和所有增量备份的内容才能够完全恢复成功,并且它们必须沿着从完全备份到依次增量备份的时间顺序逐个反推恢复,因此可能会消耗更长的恢复时间。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值