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的,否則恢復後造成顛倒

小結
增量備份的優點:沒有重複的備份數據,因此備份的數據量不大,備份所需的時間很短
增量備份的缺點:數據恢復相對比較麻煩,它需要上一次全備份和所有增量備份的內容才能夠完全恢復成功,並且它們必須沿着從完全備份到依次增量備份的時間順序逐個反推恢復,因此可能會消耗更長的恢復時間。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章