# XFS 文件系统的备份与还原

使用 tar 通常是针对目录树系统来进行备份的工作,如果是针对整个文件系统来进行备份与还原。在 CentOS 7 中可以使用 xfsdump 与 xfsrestore 指令来进行

# XFS 文件系统备份 xfsdump

支持的功能:

  • 全量备份:完整备份文件系统(full backup)

  • 增量备份:Incremental backup

    第一次全量备份之后,就可以选择增量备份了

    增量备份的差异文件存放在 /var/lib/xfsdump/inventory 中

xfsdump 有如下限制:

  • 不支持没有挂载的文件系统备份,所以只能备份已挂载的
  • 必须使用 root 的权限才能操作,涉及文件系统的关系
  • 只能备份 XFS 文件系统
  • 备份下来的数据(文件或存储媒体)只能让 xfsrestore 解析
  • 是通过文件系统的 UUID 来分辨各个备份档的,因此不能备份两个具有相同 UUID 的文件系统

简单操作时的语法如下(完整的选项非常复杂)

xfsdump [-L S_label][-M M_label][-l #][-f 备份档] 带备份资料
xfsdump -I
1
2

选项与参数:

  • L:xfsdump 会记录每次备份的 session 标头,这里可以填写针对此文件系统的建议说明
  • M:xfsdump 可以记录存储媒体的标头,这里可以填写此媒体的建议说明
  • l:小写的 L,指定等级。有 0~9 共 10 个等级,预设为 0 完整备份
  • f:有点类似 tar,后面接产生的文件,例如 /dev/st0 装置文件名或其他一般文件名等
  • I:从 /var/lib/xfsdump/inventory 列出目前备份的信息状态

注意:xfsdump 预设仅支持文件系统的备份,并不支持目录的备份。如 /etc 不是一个独立的文件系统

# 用 xfsdump 备份完整的文件系统

# 1. 先确定 /boot 是独立的问价系统
[mrcode@study ~]$ df -h /boot/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2      1014M  171M  844M  17% /boot
# 挂载点是 /boot 文件系统是 /dev/sda2 装置,是独立的文件系统

# 2. 将完整备份的文件名记录为 /srv/boot.dump
# 指定了一个等级0,后面 lm 都是说明,后面是要保存的文件地址,最后是要备份的装置
[root@study ~]# xfsdump -l 0 -L boot_all -M boot_all -f /srv/boot.dump /boot
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 study.centos.mrcode:/boot	# level 0 开始备份本机 /boot 系统	
xfsdump: dump date: Tue Oct 29 03:03:50 2019  # 备份时间
xfsdump: session id: 49ac1ac5-c63b-4778-9b87-f77f3b1d703a # dump 的 ID
xfsdump: session label: "boot_all"	# 上面写的说明
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: 144381568 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 144048736 bytes
xfsdump: dump size (non-dir files) : 143807160 bytes
xfsdump: dump complete: 1 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /srv/boot.dump OK (success)
xfsdump: Dump Status: SUCCESS
# 如果不在指令中增加 -L 和 -M 会进入互动模式,让你手动填写确认后才会开始备份

[root@study ~]# ll /srv/boot.dump 
-rw-r--r--. 1 root root 144048736 1029 03:03 /srv/boot.dump

[root@study ~]# ll /var/lib/xfsdump/inventory/
总用量 16
-rw-r--r--. 1 root root  312 1029 03:03 13d0663f-4cbd-412d-aa9f-975eb18da590.InvIndex
-rw-r--r--. 1 root root 5080 1029 03:03 6ad3d9fa-9c06-4968-9250-c1a70b5629ed.StObj
-rw-r--r--. 1 root root  576 1029 03:03 fstab

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

# 用 xfsdump 进行增量备份(Incremental backups)

增量是在全量的基础上,也就是至少有 level 0 ,才能够几区有其他的增量备份(1~9)

# 查看 xfsdump 信息
[root@study ~]# xfsdump -I
file system 0:
        fs id:          13d0663f-4cbd-412d-aa9f-975eb18da590
        session 0:
                mount point:    study.centos.mrcode:/boot
                device:         study.centos.mrcode:/dev/sda2
                time:           Tue Oct 29 03:03:50 2019
                session label:  "boot_all"
                session id:     49ac1ac5-c63b-4778-9b87-f77f3b1d703a
                level:          0
                resumed:        NO
                subtree:        NO
                streams:        1
                stream 0:
                        pathname:       /srv/boot.dump
                        start:          ino 69 offset 0
                        end:            ino 1577745 offset 0
                        interrupted:    NO
                        media files:    1
                        media file 0:
                                mfile index:    0
                                mfile type:     data
                                mfile size:     144048736
                                mfile start:    ino 69 offset 0
                                mfile end:      ino 1577745 offset 0
                                media label:    "boot_all"
                                media id:       da10035c-1c0b-4773-a56e-409a17fe9913
xfsdump: Dump Status: SUCCESS

# 可以看到,有一个 level 0
# 下面先在 boot 中建立一个大约 10MB 的文件
[root@study ~]# dd if=/dev/zero of=/boot/testing.img bs=1M count=10
记录了10+0 的读入
记录了10+0 的写出
10485760字节(10 MB)已复制,0.00396854 秒,2.6 GB/秒

# 2. 建立差异备份,这里使用 -l 1 指定了 level 1 的被增量备份
[root@study ~]# dd if=/dev/zero of=/boot/testing.img bs=1M count=10
记录了10+0 的读入
记录了10+0 的写出
10485760字节(10 MB)已复制,0.00396854 秒,2.6 GB/秒
[root@study ~]# xfsdump -l 1 -L boot_2 -M boot_2 -f /srv/boot.dump1 /boot
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 study.centos.mrcode:/boot based on level 0 dump begun Tue Oct 29 03:03:50 2019
xfsdump: dump date: Tue Oct 29 03:11:00 2019
xfsdump: session id: 8a8a1680-2a91-4292-943d-0d0a51fa2d67
xfsdump: session label: "boot_2"
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: 10506880 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 10510952 bytes
xfsdump: dump size (non-dir files) : 10488408 bytes
xfsdump: dump complete: 0 seconds elapsed
xfsdump: Dump Summary:
xfsdump:   stream 0 /srv/boot.dump1 OK (success)
xfsdump: Dump Status: SUCCESS

[root@study ~]# ll -h /srv/boot*
-rw-r--r--. 1 root root 138M 1029 03:03 /srv/boot.dump
-rw-r--r--. 1 root root  11M 1029 03:11 /srv/boot.dump1

# 3. 最后再观察下备份情况
[root@study ~]# xfsdump -I
file system 0:
        fs id:          13d0663f-4cbd-412d-aa9f-975eb18da590
        session 0:
                mount point:    study.centos.mrcode:/boot
                device:         study.centos.mrcode:/dev/sda2
                time:           Tue Oct 29 03:03:50 2019
                session label:  "boot_all"
                session id:     49ac1ac5-c63b-4778-9b87-f77f3b1d703a
                level:          0
                resumed:        NO
                subtree:        NO
                streams:        1
                stream 0:
                        pathname:       /srv/boot.dump
                        start:          ino 69 offset 0
                        end:            ino 1577745 offset 0
                        interrupted:    NO
                        media files:    1
                        media file 0:
                                mfile index:    0
                                mfile type:     data
                                mfile size:     144048736
                                mfile start:    ino 69 offset 0
                                mfile end:      ino 1577745 offset 0
                                media label:    "boot_all"
                                media id:       da10035c-1c0b-4773-a56e-409a17fe9913
        session 1:
                mount point:    study.centos.mrcode:/boot
                device:         study.centos.mrcode:/dev/sda2
                time:           Tue Oct 29 03:11:00 2019
                session label:  "boot_2"
                session id:     8a8a1680-2a91-4292-943d-0d0a51fa2d67
                level:          1
                resumed:        NO
                subtree:        NO
                streams:        1
                stream 0:
                        pathname:       /srv/boot.dump1
                        start:          ino 113 offset 0
                        end:            ino 114 offset 0
                        interrupted:    NO
                        media files:    1
                        media file 0:
                                mfile index:    0
                                mfile type:     data
                                mfile size:     10510952
                                mfile start:    ino 113 offset 0
                                mfile end:      ino 114 offset 0
                                media label:    "boot_2"
                                media id:       2c0d4e02-11cd-45a4-b131-8bc6505403fe
xfsdump: Dump Status: SUCCESS
# 可以看到上面出现了 session 1 和 level 1
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

# XFS 文件系统还原 xfsrestore

备份文件就是在急用时可以还原系统的重要数据,用 xfsdump 备份的数据可以使用 xfsresotre 来还原

xfsrestore -I	# 观察备份文件资料
xfsrestore [-f 备份文件名][-L S_label][-s] 待复原的目录  # 单一文件全系统复原
xfsrestore [-f 备份文件名] -r 待复原目录	# 通过增量备份文件复原系统
xfsrestore [-f 备份文件名] -i 待复原目录	# 进入互动模式
1
2
3
4

选项与参数:

  • I:与 xfsdump 相同的输出
  • f:后面接的就是备份的文件,企业界可能会接 /dev/st0 等磁带机,我们这里接文件名
  • L:session 的 label name ,就是在 -I 输出中查询到的数据
  • s:需要接特定目录,复原某一个文件或目录的意思
  • r:如果是用文件来存储备份数据,这个选项就不需要使用。如果是一个磁带内有多个文件,需要这东西来达成增量复原
  • i:进入互动模式,进阶管理员使用的,一般我们不太需要操作它

# 用 xfsrestore 观察 xfsdump 后的备份数据内容

[root@study ~]# xfsrestore -I
file system 0:
        fs id:          13d0663f-4cbd-412d-aa9f-975eb18da590
        session 0:
                mount point:    study.centos.mrcode:/boot
                device:         study.centos.mrcode:/dev/sda2
                time:           Tue Oct 29 03:03:50 2019
                session label:  "boot_all"
                session id:     49ac1ac5-c63b-4778-9b87-f77f3b1d703a
                level:          0
                resumed:        NO
                subtree:        NO
                streams:        1
                stream 0:
                        pathname:       /srv/boot.dump
                        start:          ino 69 offset 0
                        end:            ino 1577745 offset 0
                        interrupted:    NO
                        media files:    1
                        media file 0:
                                mfile index:    0
                                mfile type:     data
                                mfile size:     144048736
                                mfile start:    ino 69 offset 0
                                mfile end:      ino 1577745 offset 0
                                media label:    "boot_all"
                                media id:       da10035c-1c0b-4773-a56e-409a17fe9913
        session 1:
								mount point:    study.centos.mrcode:/boot
                device:         study.centos.mrcode:/dev/sda2
                time:           Tue Oct 29 03:11:00 2019
                session label:  "boot_2"
                session id:     8a8a1680-2a91-4292-943d-0d0a51fa2d67
                level:          1
                resumed:        NO
                subtree:        NO
                streams:        1
                stream 0:
                        pathname:       /srv/boot.dump1
                        start:          ino 113 offset 0
                        end:            ino 114 offset 0
                        interrupted:    NO
                        media files:    1
                        media file 0:
                                mfile index:    0
                                mfile type:     data
                                mfile size:     10510952
                                mfile start:    ino 113 offset 0
                                mfile end:      ino 114 offset 0
                                media label:    "boot_2"
                                media id:       2c0d4e02-11cd-45a4-b131-8bc6505403fe
xfsrestore: Restore Status: SUCCESS
# 和前面输出的一样
# 这里重点是看到这个文件系统是 /boot 挂载点,有两个备份 level 0 和 level 1
# 可以看到这两个备份文件的大小,更重要的是 session label
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

# 简单复原 level 0 的文件系统

# 1. 直接将数据覆盖回去即可
# 这里使用了 -L 就是那个 session label
[root@study ~]# xfsrestore -f /srv/boot.dump -L boot_all /boot
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: examining media file 0
xfsrestore: reading directories
xfsrestore: 11 directories and 337 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /srv/boot.dump OK (success)
xfsrestore: Restore Status: SUCCESS

# 2. 将备份资料在 /tmp/boot 下解开
[root@study ~]# mkdir /tmp/boot
[root@study ~]# xfsrestore -f /srv/boot.dump -L boot_all /tmp/boot/
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: examining media file 0
xfsrestore: reading directories
xfsrestore: 11 directories and 337 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /srv/boot.dump OK (success)
xfsrestore: Restore Status: SUCCESS

# 看到两个大小不一致
[root@study ~]# du -sm /boot/ /tmp/boot/
148     /boot/
138     /tmp/boot/

# 对比下发现 /boot 下多了一个 testing.img
[root@study ~]# diff -r /boot/ /tmp/boot/
只在 /boot/ 存在:testing.img
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

这里注意了,笔者以为这样复原之后,会完全还原到复原之前的内容的,这里的规则是:同名的文件被覆盖,其他系统内的新的问价会被保留

# 3. 仅复原备份档内的 grub2 到 /tmp/boot2 里面去
[root@study ~]# mkdir /tmp/boot2
[root@study ~]# xfsrestore -f /srv/boot.dump -L boot_all -s grub2 /tmp/boot2
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.7 (dump format 3.0) - type ^C for status and control
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: examining media file 0
xfsrestore: reading directories
xfsrestore: 11 directories and 337 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /srv/boot.dump OK (success)
xfsrestore: Restore Status: SUCCESS
[root@study ~]# ll -h /tmp/boot2/
总用量 0
drwx------. 5 root root 97 104 18:32 grub2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 复原增量备份资料

增量还原有顺序,比如由 level 0 -> level1 -> level2 那么还原的时候,也需要按照这个顺序

[root@study ~]# xfsrestore -f /srv/boot.dump1 /tmp/boot
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: study.centos.mrcode
xfsrestore: mount point: /boot
xfsrestore: volume: /dev/sda2
xfsrestore: session time: Tue Oct 29 03:11:00 2019
xfsrestore: level: 1
xfsrestore: session label: "boot_2"
xfsrestore: media label: "boot_2"
xfsrestore: file system id: 13d0663f-4cbd-412d-aa9f-975eb18da590
xfsrestore: session id: 8a8a1680-2a91-4292-943d-0d0a51fa2d67
xfsrestore: media id: 2c0d4e02-11cd-45a4-b131-8bc6505403fe
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 1 directories and 12 entries processed
xfsrestore: directory post-processing
xfsrestore: restoring non-directory files
xfsrestore: restore complete: 0 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /srv/boot.dump1 OK (success)
xfsrestore: Restore Status: SUCCESS

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

# 仅还原部分文件的 xfsrestore 互动模式

使用 -s 参数可以接部分数据来还原,当文件太多时,又不知道里面有哪些文件,可以使用互动模式,比如想要知道 level0 的备份数据里面有哪些东西

[root@study ~]# xfsrestore -f /srv/boot.dump -i /tmp/boot3
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: study.centos.mrcode
xfsrestore: mount point: /boot
xfsrestore: volume: /dev/sda2
xfsrestore: session time: Tue Oct 29 03:03:50 2019
xfsrestore: level: 0
xfsrestore: session label: "boot_all"
xfsrestore: media label: "boot_all"
xfsrestore: file system id: 13d0663f-4cbd-412d-aa9f-975eb18da590
xfsrestore: session id: 49ac1ac5-c63b-4778-9b87-f77f3b1d703a
xfsrestore: media id: da10035c-1c0b-4773-a56e-409a17fe9913
xfsrestore: using online session inventory
xfsrestore: searching media for directory dump
xfsrestore: reading directories
xfsrestore: 11 directories and 337 entries processed
xfsrestore: directory post-processing

 ========================== subtree selection dialog ==========================

the following commands are available:
        pwd 
        ls [ <path> ]
        cd [ <path> ]
        add [ <path> ]		# 可以加入还原文件列表中
        delete [ <path> ] # 从还原列表中删除文件,不是删除,只是从列表中移出去
        extract 	# 开始还原动作
        quit 
        help 

-> ls		# ls 查看有哪些文档
              75 initramfs-3.10.0-1062.el7.x86_64.img 
              77 vmlinuz-0-rescue-f228ab37c368416c84c6b27971ba45a9 
              76 initramfs-0-rescue-f228ab37c368416c84c6b27971ba45a9.img 
              74 vmlinuz-3.10.0-1062.el7.x86_64 
              73 symvers-3.10.0-1062.el7.x86_64.gz 
              72 config-3.10.0-1062.el7.x86_64 
              71 System.map-3.10.0-1062.el7.x86_64 
              70 .vmlinuz-3.10.0-1062.el7.x86_64.hmac 
              68 grub/
         1572928 grub2/
              67 efi/

 -> add grub/

 -> add grub2/

 -> add config-3.10.0-229.el7.x86_64
config-3.10.0-229.el7.x86_64 not found			# 这个里面不能按退格键,我输出了,不过提示未找到

 -> extract
 --------------------------------- end dialog ---------------------------------

xfsrestore: restoring non-directory files
xfsrestore: restore complete: 182 seconds elapsed
xfsrestore: Restore Summary:
xfsrestore:   stream 0 /srv/boot.dump OK (success)
xfsrestore: Restore Status: SUCCESS

# 查看还原的目录下,的确只有我们需要的两个
[root@study ~]# ls -l /tmp/boot3
总用量 0
drwxr-xr-x. 2 root root 27 104 18:22 grub
drwx------. 5 root root 97 104 18:32 grub2

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