XFS提供了 xfsdump 和 xfsrestore 工具协助备份XFS文件系统中的数据。xfsdump 按inode顺序备份一个XFS文件系统。
与传统的UNIX文件系统不同,XFS不需要在备份前被卸载;对使用中的XFS文件系统做备份就可以保证镜像的一致性。XFS的备份和恢复的过程是可以被中断然后继续的,无须冻结文件系统。xfsdump 甚至提供了高性能的多线程备份操作——它把一次dump拆分成多个数据流,每个数据流可以被发往不同的目的地
首先了解一下xfsdump的备份级别有以下两种,默认为0(即完全备份)
扩展:
系统CetnOS8 添加一块虚拟硬盘(准备一个测试分区)
正常使用一块磁盘过程如下:
添加磁盘大小:2G ->分区->格式化->挂载
Last login: Tue Oct 20 04:13:20 2020 from 10.0.0.1 [root@C8-3 ~]# fdisk -l Disk /dev/sda: 20 GiB, 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 Disklabel type: dos Disk identifier: 0xbfcdb101 Device Boot Start End Sectors Size Id Type /dev/sda1 * 2048 2099199 2097152 1G 83 Linux /dev/sda2 2099200 41943039 39843840 19G 8e Linux LVM Disk /dev/sdb: 2 GiB, 2147483648 bytes, 4194304 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
[root@C8-3 ~]# fdisk /dev/sdb #使用fdisk命令分区 Welcome to fdisk (util-linux 2.32.1). 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. Created a new DOS disklabel with disk identifier 0x467c42d4. Command (m for help): m #查看有哪些命令可以用 Help: DOS (MBR) a toggle a bootable flag b edit nested BSD disklabel c toggle the dos compatibility flag Generic d delete a partition F list free unpartitioned space l list known partition types n add a new partition p print the partition table t change a partition type v verify the partition table i print information about a partition Misc m print this menu u change display/entry units x extra functionality (experts only) Script I load disk layout from sfdisk script file O dump disk layout to sfdisk script file Save & Exit w write table to disk and exit q quit without saving changes Create a new label g create a new empty GPT partition table G create a new empty SGI (IRIX) partition table o create a new empty DOS partition table s create a new empty Sun partition table Command (m for help): n #选择新建一个分区 Partition type p primary (0 primary, 0 extended, 4 free) e extended (container for logical partitions) Select (default p): p #创建主分区 Partition number (1-4, default 1): #直接回车默认值 First sector (2048-4194303, default 2048): #起始扇区,回车默认 Last sector, +sectors or +size{K,M,G,T,P} (2048-4194303, default 4194303): #结束扇区,回车默认分配全部空间 Created a new partition 1 of type 'Linux' and of size 2 GiB. Command (m for help): w #将以上操作写入硬盘分区表,结束分区 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks. [root@C8-3 ~]# lsblk #查看分区是否成功 NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─cl-root 253:0 0 17G 0 lvm / └─cl-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 2G 0 disk └─sdb1 8:17 0 2G 0 part #分区成功但未挂载
[root@C8-3 ~]# type mkfs.xfs #查看是否有此工具 mkfs.xfs is /usr/sbin/mkfs.xfs [root@C8-3 ~]# mkfs.xfs --help #查看命令的帮助 mkfs.xfs: invalid option -- '-' unknown option -- Usage: mkfs.xfs /* blocksize */ [-b size=num] /* metadata */ [-m crc=0|1,finobt=0|1,uuid=xxx,rmapbt=0|1,reflink=0|1] /* data subvol */ [-d agcount=n,agsize=n,file,name=xxx,size=num, (sunit=value,swidth=value|su=num,sw=num|noalign), sectsize=num /* force overwrite */ [-f] /* inode size */ [-i log=n|perblock=n|size=num,maxpct=n,attr=0|1|2, projid32bit=0|1,sparse=0|1] /* no discard */ [-K] /* log subvol */ [-l agnum=n,internal,size=num,logdev=xxx,version=n sunit=value|su=num,sectsize=num,lazy-count=0|1] /* label */ [-L label (maximum 12 characters)] /* naming */ [-n size=num,version=2|ci,ftype=0|1] /* no-op info only */ [-N] /* prototype file */ [-p fname] /* quiet */ [-q] /* realtime subvol */ [-r extsize=num,size=num,rtdev=xxx] /* sectorsize */ [-s size=num] /* version */ [-V] devicename <devicename> is required unless -d name=xxx is given. <num> is xxx (bytes), xxxs (sectors), xxxb (fs blocks), xxxk (xxx KiB), xxxm (xxx MiB), xxxg (xxx GiB), xxxt (xxx TiB) or xxxp (xxx PiB). <value> is xxx (512 byte blocks). [root@C8-3 ~]# mkfs.xfs -V #查看命令版本 mkfs.xfs version 5.0.0 [root@C8-3 ~]# mkfs.xfs /dev/sdb1 #格式化新分区 meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131008 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=1, sparse=1, rmapbt=0 = reflink=1 data = bsize=4096 blocks=524032, 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
-f, --fs output info about filesystems*
[root@C8-3 ~]# lsblk -f NAME FSTYPE LABEL UUID MOUNTPOINT sda ├─sda1 ext4 8df39f81-d4b1-4881-9bb4-51f8f6ee964a /boot └─sda2 LVM2_member mQQNpB-AMy6-Ad3M-3RNh-NeRf-8tf9-OmosY7 ├─cl-root xfs 8dbc40ac-24cf-45c9-8c53-d07ac9cee558 / └─cl-swap swap ffc54438-efdc-45d7-a244-b9e9bfef4930 [SWAP] sdb └─sdb1 xfs d549d0dd-23a2-4223-abe5-96055a8f737a
[root@C8-3 ~]# mkdir /xfsTest [root@C8-3 ~]# mount /dev/sdb1 /xfsTest/ [root@C8-3 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 19G 0 part ├─cl-root 253:0 0 17G 0 lvm / └─cl-swap 253:1 0 2G 0 lvm [SWAP] sdb 8:16 0 2G 0 disk └─sdb1 8:17 0 2G 0 part /xfsTest
[root@C8-3 ~]# rm -rf /xfsTest/* [root@C8-3 ~]# ls /xfsTest/ [root@C8-3 ~]# cp /etc/passwd /xfsTest/ [root@C8-3 ~]# touch /xfsTest/file{1..3}{a..b}.txt [root@C8-3 ~]# mkdir -p /xfsTest/m{1..3}/n{a..b} [root@C8-3 ~]# tree /xfsTest/ /xfsTest/ ├── file1a.txt ├── file1b.txt ├── file2a.txt ├── file2b.txt ├── file3a.txt ├── file3b.txt ├── m1 │ ├── na │ └── nb ├── m2 │ ├── na │ └── nb ├── m3 │ ├── na │ └── nb └── passwd 9 directories, 7 files
先查看xfsdump帮助
[root@C8-3 ~]# type xfsdump xfsdump is /usr/sbin/xfsdump [root@C8-3 ~]# xfsdump --help xfsdump: version 3.1.8 (dump format 3.0) xfsdump: usage: xfsdump [ -a (dump DMF dualstate files as offline) ] ……
[ -f <destination> … ]
xfsdump -f 备份存放位置 要备份路径或设备文件
注意:备份的路径这里不能写成/xfsTest/。 可以是/dev/sdb1 或/xfsTest
[root@C8-3 ~]# xfsdump -f /opt/dump_sdb1 /dev/sdb1 xfsdump: using file dump (drive_simple) strategy xfsdump: version 3.1.8 (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 C8-3:/xfsTest xfsdump: dump date: Tue Oct 20 08:09:38 2020 xfsdump: session id: 3fdced05-235f-4926-9409-7c7e4081da84 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: 30016 bytes ============================= 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 32936 bytes xfsdump: dump size (non-dir files) : 1568 bytes xfsdump: dump complete: 70 seconds elapsed xfsdump: Dump Summary: xfsdump: stream 0 /opt/dump_sdb1 OK (success) xfsdump: Dump Status: SUCCESS
[root@C8-3 ~]# time xfsdump -f /opt/dump_sdb1_3 /dev/sdb1 -L dump_1_3 -M 1_3 xfsdump: using file dump (drive_simple) strategy xfsdump: version 3.1.8 (dump format 3.0) - type ^C for status and control xfsdump: level 0 dump of C8-3:/xfsTest xfsdump: dump date: Tue Oct 20 08:21:12 2020 xfsdump: session id: 5308f2ab-6d0e-476f-9160-a72a37e56ca9 xfsdump: session label: "dump_1_3" 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: 30016 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 32936 bytes xfsdump: dump size (non-dir files) : 1568 bytes xfsdump: dump complete: 10 seconds elapsed xfsdump: Dump Summary: xfsdump: stream 0 /opt/dump_sdb1_3 OK (success) xfsdump: Dump Status: SUCCESS real 0m10.058s user 0m0.000s sys 0m0.004s
-L :xfsdump 记录每次备份的 session 标头,这里可以填写针对此文件系统的简易说明
-M :xfsdump 可以记录储存媒体的标头,这里可以填写此媒体的简易说明
参数:-s 文件路径 只对指定的文件进行备份,-s指定时,路径写的是相对文件系统根的路径(-s可以是文件或目录)
要对/xfsTest下的m2/nb这个目录(完整路径是/xfsTest/m2/nb 直接写这个不行)进行备份,使用-s 参数
[root@C8-3 ~]# xfsdump -f /opt/dump_m2nb -s m2/nb /xfsTest -L dump_m2nb -M m2nb xfsdump: using file dump (drive_simple) strategy xfsdump: version 3.1.8 (dump format 3.0) - type ^C for status and control xfsdump: level 0 dump of C8-3:/xfsTest xfsdump: dump date: Tue Oct 20 10:04:58 2020 xfsdump: session id: 4cd18dc2-9a10-4538-b93c-3d4169c13eae xfsdump: session label: "dump_m2nb" 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: 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 22592 bytes xfsdump: dump size (non-dir files) : 0 bytes xfsdump: dump complete: 10 seconds elapsed xfsdump: Dump Summary: xfsdump: stream 0 /opt/dump_m2nb OK (success) xfsdump: Dump Status: SUCCESS
[root@C8-3 xfsTest]# xfsdump -I file system 0: fs id: d549d0dd-23a2-4223-abe5-96055a8f737a session 0: mount point: C8-3:/xfsTest device: C8-3:/dev/sdb1 time: Tue Oct 20 08:09:38 2020 session label: "dump_sdb1" session id: 3fdced05-235f-4926-9409-7c7e4081da84 level: 0 resumed: NO subtree: NO streams: 1 stream 0: pathname: /opt/dump_sdb1 start: ino 131 offset 0 end: ino 138 offset 0 interrupted: NO media files: 1 media file 0: mfile index: 0 mfile type: data mfile size: 32936 mfile start: ino 131 offset 0 mfile end: ino 138 offset 0 media label: "sdb1" media id: 77e98c40-d588-4562-b801-6ba7dd2425bf session 1: mount point: C8-3:/xfsTest device: C8-3:/dev/sdb1 time: Tue Oct 20 08:17:18 2020 session label: "dump_passwd" session id: e9970178-7a24-4c9d-b9c0-7e13f8d43c48 level: 0 resumed: NO subtree: NO streams: 1 stream 0: pathname: /opt/dump_sdb1 start: ino 0 offset 0 end: ino 0 offset 0 interrupted: YES media files: 0 session 2: mount point: C8-3:/xfsTest device: C8-3:/dev/sdb1 time: Tue Oct 20 08:19:23 2020 session label: "dump_12" session id: 391b7c46-47f6-4760-b454-c7835045db09 level: 0 resumed: NO subtree: NO streams: 1 stream 0: pathname: /opt/dump_sdb1 start: ino 0 offset 0 end: ino 0 offset 0 interrupted: YES media files: 0 session 3: mount point: C8-3:/xfsTest device: C8-3:/dev/sdb1 time: Tue Oct 20 08:20:12 2020 session label: "dump_12" session id: d6613e91-8c68-4f40-b68b-e836fdf35568 level: 0 resumed: NO subtree: NO streams: 1 stream 0: pathname: /opt/dump_sdb1_1 start: ino 131 offset 0 end: ino 138 offset 0 interrupted: NO media files: 1 media file 0: mfile index: 0 mfile type: data mfile size: 32936 mfile start: ino 131 offset 0 mfile end: ino 138 offset 0 media label: "12" media id: f85b0aff-d362-429c-9804-0ca1071f812f session 4: mount point: C8-3:/xfsTest device: C8-3:/dev/sdb1 time: Tue Oct 20 08:21:12 2020 session label: "dump_1_3" session id: 5308f2ab-6d0e-476f-9160-a72a37e56ca9 level: 0 resumed: NO subtree: NO streams: 1 stream 0: pathname: /opt/dump_sdb1_3 start: ino 131 offset 0 end: ino 138 offset 0 interrupted: NO media files: 1 media file 0: mfile index: 0 mfile type: data mfile size: 32936 mfile start: ino 131 offset 0 mfile end: ino 138 offset 0 media label: "1_3" media id: 78304c29-4353-4edc-a3cb-63454d898918 xfsdump: Dump Status: SUCCESS
[root@C8-3 xfsTest]# cd [root@C8-3 ~]# rm -rf /xfsTest/* [root@C8-3 ~]# ls -a /xfsTest/
语法:xfsrestore -f 指定恢复文件的位置 指定存放恢复后的文件的路径
[root@C8-3 ~]# mkdir /xfsTest-re #在其他分区创建恢复文件夹 [root@C8-3 ~]# xfsrestore -f /opt/dump_sdb1 /xfsTest-re xfsrestore: using file dump (drive_simple) strategy xfsrestore: version 3.1.8 (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: C8-3 xfsrestore: mount point: /xfsTest xfsrestore: volume: /dev/sdb1 xfsrestore: session time: Tue Oct 20 08:09:38 2020 xfsrestore: level: 0 xfsrestore: session label: "dump_sdb1" xfsrestore: media label: "sdb1" xfsrestore: file system id: d549d0dd-23a2-4223-abe5-96055a8f737a xfsrestore: session id: 3fdced05-235f-4926-9409-7c7e4081da84 xfsrestore: media id: 77e98c40-d588-4562-b801-6ba7dd2425bf xfsrestore: using online session inventory xfsrestore: searching media for directory dump xfsrestore: reading directories xfsrestore: 10 directories and 16 entries processed xfsrestore: directory post-processing xfsrestore: restoring non-directory files xfsrestore: restore complete: 0 seconds elapsed xfsrestore: Restore Summary: xfsrestore: stream 0 /opt/dump_sdb1 OK (success) xfsrestore: Restore Status: SUCCESS [root@C8-3 ~]# ll /xfsTest-re/ #查看恢复的文件 total 4 -rw-r--r--. 1 root root 0 Oct 20 07:58 file1a.txt -rw-r--r--. 1 root root 0 Oct 20 07:58 file1b.txt -rw-r--r--. 1 root root 0 Oct 20 07:58 file2a.txt -rw-r--r--. 1 root root 0 Oct 20 07:58 file2b.txt -rw-r--r--. 1 root root 0 Oct 20 07:58 file3a.txt -rw-r--r--. 1 root root 0 Oct 20 07:58 file3b.txt drwxr-xr-x. 4 root root 26 Oct 20 07:59 m1 drwxr-xr-x. 4 root root 26 Oct 20 07:59 m2 drwxr-xr-x. 4 root root 26 Oct 20 07:59 m3 -rw-r--r--. 1 root root 1469 Oct 20 07:58 passwd [root@C8-3 ~]# tree /xfsTest #对比源目录啥也没有了 /xfsTest 0 directories, 0 files [root@C8-3 ~]# tree /xfsTest-re/ #在恢复的目录中重建了目录结构 /xfsTest-re/ ├── file1a.txt ├── file1b.txt ├── file2a.txt ├── file2b.txt ├── file3a.txt ├── file3b.txt ├── m1 │ ├── na │ └── nb ├── m2 │ ├── na │ └── nb ├── m3 │ ├── na │ └── nb └── passwd 9 directories, 7 files
使用 xfsdump 时,请注意下面下面的几个限制:
增量备份是指在一次全备份或上一次增量备份后,以后每次的备份只需备份与前一次相比增加或者被修改的文件。这就意味着,第一次增量备份的对象是进行全备后所产生的增加和修改的文件;第二次增量备份的对象是进行第一次增量备份后所产生的增加和修改的文件,以此类推。
[root@C8-3 ~]# tree /xfsTest /xfsTest ├── m1 │ ├── na │ └── nb ├── m2 │ ├── na │ └── nb ├── m3 │ ├── na │ └── nb └── passwd
[root@C8-3 ~]# xfsdump -f /opt/xFull /xfsTest -L xF1 -M x1 xfsdump: using file dump (drive_simple) strategy xfsdump: version 3.1.8 (dump format 3.0) - type ^C for status and control xfsdump: level 0 dump of C8-3:/xfsTest xfsdump: dump date: Tue Oct 20 10:43:59 2020 xfsdump: session id: 4bc4262b-afa1-493f-8b21-33428325961d xfsdump: session label: "xF1" 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: 28096 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 29000 bytes xfsdump: dump size (non-dir files) : 1568 bytes xfsdump: dump complete: 10 seconds elapsed xfsdump: Dump Summary: xfsdump: stream 0 /opt/xFull OK (success) xfsdump: Dump Status: SUCCESS
[root@C8-3 ~]# echo hellow >> /xfsTest/passwd [root@C8-3 ~]# touch /xfsTest/nb3.txt
[root@C8-3 ~]# rm -rf /xfsTest/m2 [root@C8-3 ~]# ls /xfsTest m1 m3 nb3.txt passwd [root@C8-3 ~]# ll /xfsTest total 4 drwxr-xr-x. 4 root root 26 Oct 20 07:59 m1 drwxr-xr-x. 4 root root 26 Oct 20 07:59 m3 -rw-r--r--. 1 root root 0 Oct 20 10:47 nb3.txt -rw-r--r--. 1 root root 1476 Oct 20 10:46 passwd
[root@C8-3 ~]# xfsdump -l 1 -f /opt/xBL1 /xfsTest -L xBL1 -M xB1
添加点东西、再删点东西
[root@C8-3 ~]# mkdir /xfsTest/xBl2 [root@C8-3 ~]# rm -rf /xfsTest/m3
然后接茬进行一次2级增量备份
[root@C8-3 ~]# xfsdump -l 2 -f /opt/xBL2 /xfsTest -L xBL2 -M xB2
[root@C8-3 ~]# rm -rf /xfsTest/* [root@C8-3 ~]# ll /xfsTest total 0
现在进行恢复,要想恢复全部全部数据,包括新添加的文件,如何恢复?
步骤:
1、先恢复完全备份
2、情况1: 恢复最后一次增量备份(如果两次增量备份都是1级的,所以只需要恢复最后一个增量就可以了。
3、情况2:如果你做的是第一次是1级备,第二次是2级备,那么你在恢复的时候就需要先恢复完全备份,然后是1级备,最后是2级备)
####直接恢复最后一次增量备份
[root@C8-3 ~]# xfsrestore -f /opt/xBL2 /xfsTest [root@C8-3 ~]# tree /xfsTest /xfsTest └── xBl2
发现只恢复了最后一次建立的文件夹
[root@C8-3 ~]# rm -rf /xfsTest/* [root@C8-3 ~]# ll /xfsTest total 0 [root@C8-3 ~]# xfsrestore -f /opt/xBL1 /xfsTest [root@C8-3 ~]# tree /xfsTest /xfsTest ├── nb3.txt └── passwd
发现只恢复了第一次增量备份前添加和修改的文件、文件夹
[root@C8-3 ~]# xfsrestore -f /opt/xFull /xfsTest [root@C8-3 ~]# tree /xfsTest /xfsTest ├── m1 │ ├── na │ └── nb ├── m2 │ ├── na │ └── nb ├── m3 │ ├── na │ └── nb └── passwd 9 directories, 1 file [root@C8-3 ~]# ll /xfsTest total 4 drwxr-xr-x. 4 root root 26 Oct 20 07:59 m1 drwxr-xr-x. 4 root root 26 Oct 20 07:59 m2 drwxr-xr-x. 4 root root 26 Oct 20 07:59 m3 -rw-r--r--. 1 root root 1469 Oct 20 07:58 passwd
这时候就恢复到了第一次增量备份之前的状态
如果要恢复到最后删除所有文件时候的状态,就需要按顺序依次恢复增量一和增量二的备份内容。
到此,数据恢复成功了。