xfs提供了xfsdump和xfsrestore工具,协助备份xfs文件系统中的数据。xfsdump按inode顺序备份一个xfs文件系统8 xfs备份级别有2种:0表示完全备份,1-9表示增量备份,其中默认为0
下面进行使用xfsdump及xfsrestore恢复文件。
在VMware里的Centos中添加一块虚拟的硬盘 在虚拟机中进行分区并挂载分区
[root@client-2 ~]# 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 0x91666197. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): Using default response 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): Using default value 41943039 Partition 1 of type Linux and of size 20 GiB is set Command (m for help): wq The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@client-2 ~]# mkfs.xfs /dev/sdb1 meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310656 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=5242624, 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注意:这里我们使用的是xfsdump以及xfsrestore工具,因此必须使用xfs格式化
挂载
[root@client-2 ~]# mkdir /test [root@client-2 ~]# mount /dev/sdb1 /test 准备测试文件 [root@client-2 test]# cp /etc/passwd . [root@client-2 test]# mkdir file [root@client-2 test]# echo "test" > file/1.txt [root@client-2 test]# md5sum passwd 9dc5f89fde54117b9f7db7ee27de82f7 passwd [root@client-2 test]# tree . ├── file │ └── 1.txt └── passwd 1 directory, 2 files这里,我们使用md5sum命令查看passwd的md5值,以便恢复之后做对比
备份整个分区 [root@client-2 /]# mkdir /backup [root@client-2 /]# xfsdump -f /backup/dump_sdb1_1 /dev/sdb1 -L sdb1_version_1 -M sdb1 xfsdump: using file dump (drive_simple) strategy xfsdump: version 3.1.4 (dump format 3.0) - type ^C for status and control xfsdump: level 0 dump of client-2:/test xfsdump: dump date: Thu Sep 3 15:27:11 2020 xfsdump: session id: 82c52562-6166-4e9f-8364-2a37b34ee063 xfsdump: session label: "sdb1_version_1" 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: 29952 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 26096 bytes xfsdump: dump size (non-dir files) : 3136 bytes xfsdump: dump complete: 0 seconds elapsed xfsdump: Dump Summary: xfsdump: stream 0 /backup/dump_sdb1_1 OK (success) xfsdump: Dump Status: SUCCESS其中,各项参数含义如下: -f:指定备份文件夹 -L:指定标签session label -M:指定设备标签media label
查看备份信息 [root@client-2 /]# xfsdump -I file system 0: fs id: 8e6f40dc-d52a-48ec-85ae-601cb5191f46 session 0: mount point: client-2:/test device: client-2:/dev/sdb1 time: Thu Sep 3 15:27:11 2020 session label: "sdb1_version_1" session id: 82c52562-6166-4e9f-8364-2a37b34ee063 level: 0 resumed: NO subtree: NO streams: 1 stream 0: pathname: /backup/dump_sdb1_1 start: ino 67 offset 0 end: ino 70 offset 0 interrupted: NO media files: 1 media file 0: mfile index: 0 mfile type: data mfile size: 26096 mfile start: ino 67 offset 0 mfile end: ino 70 offset 0 media label: "sdb1" media id: 8ce8ca7e-2d6c-47c1-8f43-b6dcc50da0ef xfsdump: Dump Status: SUCCESS 删除文件 [root@client-2 /]# rm -rf /test/* [root@client-2 /]# ll /test total 0 恢复文件 [root@client-2 /]# xfsrestore -f /backup/dump_sdb1_1 /test/ xfsrestore: using file dump (drive_simple) strategy xfsrestore: version 3.1.4 (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: client-2 xfsrestore: mount point: /test xfsrestore: volume: /dev/sdb1 xfsrestore: session time: Thu Sep 3 15:27:11 2020 xfsrestore: level: 0 xfsrestore: session label: "sdb1_version_1" xfsrestore: media label: "sdb1" xfsrestore: file system id: 8e6f40dc-d52a-48ec-85ae-601cb5191f46 xfsrestore: session id: 82c52562-6166-4e9f-8364-2a37b34ee063 xfsrestore: media id: 8ce8ca7e-2d6c-47c1-8f43-b6dcc50da0ef 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: 0 seconds elapsed xfsrestore: Restore Summary: xfsrestore: stream 0 /backup/dump_sdb1_1 OK (success) xfsrestore: Restore Status: SUCCESS 检查文件 [root@client-2 /]# cd /test [root@client-2 test]# ll total 4 drwxr-xr-x. 2 root root 19 Sep 3 15:22 file -rw-r--r--. 1 root root 2156 Sep 3 15:21 passwd [root@client-2 test]# tree . ├── file │ └── 1.txt └── passwd 1 directory, 2 files [root@client-2 test]# md5sum passwd 9dc5f89fde54117b9f7db7ee27de82f7 passwd [root@client-2 test]#至此,文件已恢复完毕,passwd的md5值也完全一致。
注:使用xfsdump时,需要注意以下限制
xfsdump不支持没有挂载的系统备份,所以只能备份已经挂载的xfsdump必须使用root权限才能操作(涉及文件系统的关系)xfsdump只能备份xfs文件系统xfsdump备份下来的数据(档案或储存的媒体)只能让xfsrestore解析xfsdump是透过文件系统的UUID来分辨各个备份档的,因此不能备份两个具有相同UUID的文件系统