三、基础篇:xfs 文件系统的备份和恢复;xfsdump和xfsrestore命令详解

XFS 提供了 xfsdump 和 xfsrestore 工具协助备份 XFS 文件系统中的数据。xfsdump 按 inode 顺序备份一个 XFS 文件系统。

centos7 开始选择 xfs 格式作为默认文件系统,而且不再使用以前的 ext,仍然支持 ext4,xfs 专为大数据产生,每个单个文件系统最大可以支持 8eb,单个文件可以支持 16tb;不仅数据量大,而且扩展性高。还可以通过 xfsdump,xfsrestore 来备份和恢复。 与传统的 UNIX 文件系统不同,XFS 不需要在备份前被卸载;对使用中的 XFS 文件系统做备份就可以保证镜像的一致性。XFS 的备份和恢复的过程是可以被中断然后继续的,无需冻结文件系统。xfsdump 甚至提供了高性能的多线程备份操作,它把一次 dump 拆分成多个数据流,每个数据流可以被发往不同的目的地。

xfsdump 的备份级别有以下两种,默认为 0(即完全备份)

0 级别代表: 完全备份

1 到 9 级别代表: 增量备份

扩展:

完全备份:不管目录下的文件有没有变化每次都把指定的备份目彔完整的复制一遍,; 如:cp

增量备份:每次将之前(第一次、第二次、直到前一次)做过备份之后有变化的文件进行备份;

差异备份:每次都将第一次完整备份以来有变化的文件进行备份。如: rsync 备份

xfsdump 详解

对完整的文件系统进行备份,除了可以进行完整备份,还可以进行累积性备份。就是第一次使用xfsdump,是完整备份,定义为level 0,而第二次备份,则不会完整备份,而是和第一次的备份(level 0)进行比较,仅备份有差异的文件(level 1)。各个level的文件,一本存放在/var/lib/xfsdump/inventory中。

xfsdump有5个限制:
1. 必须用root权限
2. 只能备份已挂载的文件系统
3. 只能备份XFS文件系统
4. 只能用xfsrestore解析
5. 透过文件系统的UUID来分辨备份档,因此不能备份相同UUID的文件系统

语法:
xfsdump [-L S_lable] [-M M_lable] [-l #] [-f 备份档] 待备份资料
xfsdump -I

参数:
-L:xfsdump会记录每次备份的session标头,这里可以填写针对此文件系统的简易说明;
-M:xfsdump可以记录存储媒体的标头(储存设备的标签),这里可以填写此媒体的简易说明。
-l:是L的小写,就是指定等级,有0~9共10个等级,默认为0,即完整备份。
-f:有点类似tar,后面接产生的文件,也可以接例如/dev/st0设备文件名或其他一般文件文件名;
-I:大写的“i”,从/var/lib/xfsdump/inventory 列出目前备份的信息状态

例如: 

xfsdump -f /opt/dump_sdb1_v2  /dev/sdb1  -L dump_sdb1_v2 -M sdb1_is_bigdata

xfsrestore详解

xfs文件系统的备份与还原——xfsrestore命令:

选项和参数:
-I:大写的“i”跟xfsdump相同的输出!可查询备份数据,包括Label名称和备份时间等;
-f:后面接的就是备份文件!企业中很有可能会接/dev/st0等磁带机!
-L:可用“-I”查询到的数据。在这个选项后输入;
-s:需要接某特定目录,也能复原某一文件或目录;
-r:如果是用文件来存储备份数据,那这个就不需要使用。如果是一个磁盘内有多个文件,需要使用它来达成累积复原;
-i:进入互动模式(一般不需要)

例如:

xfsrestore -f /opt/dump_sdb1_v2  /var/test/

实验:

步骤:新建硬盘 并分区,挂载好分区,执行完全备份,level 1备份,level2备份,恢复完全备份,恢复level1备份,恢复level 2备份。

如果系统没有xfsdump,可以执行yum install xfsdump  安装。

#查看新增硬盘 - sdb

[root@localhost ~]# ls -l /dev/sd*
brw-rw----. 1 root disk 8,  0 Jan 13 14:48 /dev/sda
brw-rw----. 1 root disk 8,  1 Jan 13 14:48 /dev/sda1
brw-rw----. 1 root disk 8,  2 Jan 13 14:48 /dev/sda2
brw-rw----. 1 root disk 8, 16 Jan 13 14:48 /dev/sdb


#格式化并分区得到 sdb1


[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 0x2b09fdf8.

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partitions system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

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): +1G
Partition 1 of type Linux and of size 1 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: 0x2b09fdf8

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# ls -l /dev/sd* 
brw-rw----. 1 root disk 8,  0 Jan 13 14:48 /dev/sda
brw-rw----. 1 root disk 8,  1 Jan 13 14:48 /dev/sda1
brw-rw----. 1 root disk 8,  2 Jan 13 14:48 /dev/sda2
brw-rw----. 1 root disk 8, 16 Jan 13 14:52 /dev/sdb
brw-rw----. 1 root disk 8, 17 Jan 13 14:52 /dev/sdb1

#挂载sdb1到 /test 下

[root@localhost ~]# yum install xfsprogs

[root@localhost ~]# mkfs.xfs /dev/sdb1 
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=65536 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=262144, 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 ~]# mount /dev/sdb1 /test

#创建目录和文件

[root@localhost ~]# cd /test/
[root@localhost test]# mkdir data1 data2
[root@localhost test]# echo 'hello' > 1.txt
[root@localhost test]# echo 'hello1' > data1/21.txt
[root@localhost test]# tree /test
/test
├── 1.txt
├── data1
│   └── 21.txt
└── data2

2 directories, 2 files

#完全备份

[root@localhost test]# xfsdump -f /opt/test-full /test -L test-full -M sdb1
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsdump: level 0 dump of localhost.localdomain:/test
xfsdump: dump date: Wed Jan 13 15:25:17 2021
xfsdump: session id: c9c27551-99a7-4648-8c61-ac13ba201a6f
xfsdump: session label: "test-full"
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: 30272 bytes
xfsdump: /var/lib/xfsdump/inventory created
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 24688 bytes
xfsdump: dump size (non-dir files) : 1088 bytes
xfsdump: dump complete: 0 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /opt/test-full OK (success)
xfsdump: Dump Status: SUCCESS


#新增一些文件,并增量备份(level 1)

[root@localhost test]# touch /test/23.txt /test/12.txt /test/data2/9.txt 
[root@localhost test]# tree /test
/test
├── 12.txt
├── 1.txt
├── 23.txt
├── data1
│   └── 21.txt
└── data2
    └── 9.txt

2 directories, 5 files
[root@localhost test]# 

[root@localhost test]# xfsdump -l 1 -f /opt/test-back1 /test -L test-bak1 -M sdb1 
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsdump: level 1 incremental dump of localhost.localdomain:/test based on level 0 dump begun Wed Jan 13 15:25:17 2021
xfsdump: dump date: Wed Jan 13 15:30:00 2021
xfsdump: session id: 51b782f9-7f7e-4119-b46e-7d0b4e4f7fb6
xfsdump: session label: "test-bak1"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: pruning unneeded subtrees
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 22080 bytes
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 23664 bytes
xfsdump: dump size (non-dir files) : 0 bytes
xfsdump: dump complete: 0 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /opt/test-back1 OK (success)
xfsdump: Dump Status: SUCCESS

#再新增一些文件,并增量备份(level 2)

[root@localhost test]# mkdir data3
[root@localhost test]# touch data3/t.txt
[root@localhost test]# tree /test
/test
├── 12.txt
├── 1.txt
├── 23.txt
├── data1
│   └── 21.txt
├── data2
│   └── 9.txt
└── data3
    └── t.txt

3 directories, 6 files

[root@localhost test]# xfsdump -l 2 -f /opt/test-back2 /test -L test-bak2 -M sdb1    
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsdump: level 2 incremental dump of localhost.localdomain:/test based on level 1 dump begun Wed Jan 13 15:30:00 2021
xfsdump: dump date: Wed Jan 13 15:34:42 2021
xfsdump: session id: 6984e19c-e07b-42e1-9d30-09aa8e269c71
xfsdump: session label: "test-bak2"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: pruning unneeded subtrees
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 21440 bytes
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 22440 bytes
xfsdump: dump size (non-dir files) : 0 bytes
xfsdump: dump complete: 0 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /opt/test-back2 OK (success)
xfsdump: Dump Status: SUCCESS

#清空数据
[root@localhost test]#  rm -rf /test/* 
[root@localhost test]# tree /test
/test

0 directories, 0 files

# 恢复数据 
# 第一步:先恢复完全备份 

[root@localhost test]# xfsrestore -f /opt/test-full /test/ 
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: /test
xfsrestore: volume: /dev/sdb1
xfsrestore: session time: Wed Jan 13 15:25:17 2021
xfsrestore: level: 0
xfsrestore: session label: "test-full"
xfsrestore: media label: "sdb1"
xfsrestore: file system id: 52253f2c-e6dc-4fca-9a66-3676166e9a73
xfsrestore: session id: c9c27551-99a7-4648-8c61-ac13ba201a6f
xfsrestore: media id: 8e448654-f00a-4a37-92ca-d1d726617357
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 3 directories and 4 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /opt/test-full OK (success)
xfsrestore: Restore Status: SUCCESS
[root@localhost test]# tree /test
/test
├── 1.txt
├── data1
│   └── 21.txt
└── data2

2 directories, 2 files

#第二步:恢复level 1 的数据,如果同一个级别有多个备份文件,则恢复最新一个。

[root@localhost test]#  xfsrestore -f /opt/test-back1 /test/
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: /test
xfsrestore: volume: /dev/sdb1
xfsrestore: session time: Wed Jan 13 15:30:00 2021
xfsrestore: level: 1
xfsrestore: session label: "test-bak1"
xfsrestore: media label: "sdb1"
xfsrestore: file system id: 52253f2c-e6dc-4fca-9a66-3676166e9a73
xfsrestore: session id: 51b782f9-7f7e-4119-b46e-7d0b4e4f7fb6
xfsrestore: media id: f150b3bf-b52a-44ff-82f6-9ebcb0d0e397
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 2 directories and 6 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /opt/test-back1 OK (success)
xfsrestore: Restore Status: SUCCESS
[root@localhost test]# tree /test
/test
├── 12.txt
├── 1.txt
├── 23.txt
├── data1
│   └── 21.txt
└── data2
    └── 9.txt

2 directories, 5 files


#第三步:恢复level 2 的数据,如果同一个级别有多个备份文件,则恢复最新一个。

[root@localhost test]#  xfsrestore -f /opt/test-back2 /test/
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: /test
xfsrestore: volume: /dev/sdb1
xfsrestore: session time: Wed Jan 13 15:34:42 2021
xfsrestore: level: 2
xfsrestore: session label: "test-bak2"
xfsrestore: media label: "sdb1"
xfsrestore: file system id: 52253f2c-e6dc-4fca-9a66-3676166e9a73
xfsrestore: session id: 6984e19c-e07b-42e1-9d30-09aa8e269c71
xfsrestore: media id: f2ec6b23-e781-4c7d-91cb-fdfa6693bc62
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 2 directories and 7 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /opt/test-back2 OK (success)
xfsrestore: Restore Status: SUCCESS
[root@localhost test]# tree /test
/test
├── 12.txt
├── 1.txt
├── 23.txt
├── data1
│   └── 21.txt
├── data2
│   └── 9.txt
└── data3
    └── t.txt

3 directories, 6 files

# 依次类推直至level 最高值的文件恢复完毕


猜你喜欢

转载自blog.csdn.net/glmichelin/article/details/112572121