1. 程式人生 > 其它 >linux精編學習教程:檔案的基本管理和XFS檔案系統備份恢復

linux精編學習教程:檔案的基本管理和XFS檔案系統備份恢復

本人從事IT行業已有十多年,有著豐富的實戰經驗,總結了大量的學習方法,更是積累了很多的學習資料,很高興能在這裡跟大家交流學習,希望能在這裡跟大家共同進步和成長!

全套學習資料移步至公眾號【學神來啦】更多學習資料新增扣扣資源群:661308959

本節所講內容:

3.1 Linux系統目錄結構和相對路徑與絕對路徑

3.2 檔案的管理

3.3 檢視檔案

3.4 實戰:xfs檔案系統的備份和恢復

3.1 Linux系統目錄結構和相對路徑與絕對路徑

3.1.1 系統目錄結構

在WIN系統中,檢視檔案先進入相應的碟符,然後進入檔案目錄

在WIN中,它是多根 c:\ d:\ e:\

Linux只有一個/根目錄

使用tree命令檢視linux目錄結構,如果沒有這個命令,需要手工安裝一下

[root@xuegod63 ~]# yum install tree #安裝tree命令

檢視/tmp目錄結構

[root@xuegod63 ~]# tree /tmp/

檢視根下有哪些檔案:

[root@xuegod63 ~]# ls /

bin dev home lib64 mnt proc run srv tmp var

boot etc lib media opt root sbin sys usr

根下的目錄作用說明:

目 錄

說 明

/

處於linux系統樹形結構的最頂端,它是linux檔案系統的入口,所有的目錄、檔案、裝置都在 / 之下

/bin

bin是Binary的縮寫。常用的二進位制命令目錄。比如 ls、cp、mkdir、cut等;和/usr/bin類似,一些使用者級工具

/boot

存放的系統啟動相關的檔案,例如:kernel.grub(引導裝載程式)

/dev

dev是Device的縮寫。裝置檔案目錄,比如音效卡、磁碟……在Linux中

一切都被看做檔案。終端裝置、磁碟等等都被看做檔案

裝置檔案:/dev/sda,/dev/sda1,/dev/tty1,/dev/tty2,/dev/pts/1, /dev/zero, /dev/null, /dev/cdrom

/etc

常用系統及二進位制安裝包配置檔案預設路徑和伺服器啟動命令目錄,如:

/etc/passwd 使用者資訊檔案

/etc/shadow 使用者密碼檔案

/etc/group 儲存使用者組資訊

/etc/fstab 系統開機啟動自動掛載分割槽列表

/etc/hosts 設定使用者自己的IP與主機名對應的資訊

/home

普通使用者的家目錄預設存放目錄

/lib

庫檔案存放目錄,函式庫目錄

/mnt

/media

/mnt和/media一般用來臨時掛載儲存裝置的掛載目錄,比如有cdrom、U盤等目錄

/mnt/hgfs:安裝vmware-tools後,使用共享資料夾功能,可以共享虛擬機器和真機的目錄,實現檔案相互複製,一般這個功能沒有用。我們直接使用xshell來上傳檔案

/opt

表示的是可選擇的意思,有些軟體包也會被安裝在這裡。如:gitlab

/proc

作業系統執行時,程序(正在執行中的程式)資訊及核心資訊(比如cpu、硬碟分割槽、記憶體資訊等)存放在這裡。/proc目錄是偽裝的檔案系統proc的掛載目錄,proc並不是真正的檔案系統。因此,這個目錄是一個虛擬的目錄,它是系統記憶體的對映,我們可以通過直接訪問這個目錄來獲取系統資訊。也就是說,這個目錄的內容不在硬碟上而是在記憶體裡

檢視咱們的CPU資訊

cat/proc/cpuinfo

/sys

系統目錄,存放硬體資訊的相關檔案

/run

執行目錄,存放的是系統執行時的資料,比如程序的PID檔案

/srv

服務目錄,存放的是我們本地服務的相關檔案

/sbin

大多數涉及系統管理的命令都存放在該目錄中,它是超級許可權使用者root的可執行命令存放地,普通使用者無許可權執行這個目錄下的命令,凡是目錄sbin中包含的命令都是root許可權才能執行的

/tmp

該目錄用於存放臨時檔案,有時使用者執行程式的時候,會產生一些臨時檔案。/tmp就是用來存放臨時檔案的。/var/tmp目錄和該目錄的作用是相似的,不能存放重要資料,系統會定期刪除這個目錄下的沒有被使用的檔案。

它的許可權比較特殊

[root@xuegod63 ~]# ls-ld /tmp

drwxrwxrwt 10 root root 12288 Oct 3 20:45 /tmp/ à粘滯位(stickybit)目錄的sticky位表示這個目錄裡的檔案只能被檔案的owner(所有者)和root刪除

/var

系統執行和軟體執行時產生的日誌資訊,該目錄的內容是經常變動的,存放的是一些變化的檔案。比如/var下有/var/log目錄用來存放系統日誌的目錄,還有mail、/var/spool/cron

/usr

存放應用程式和檔案,
/usr/bin 普通使用者使用的應用程式
/usr/sbin 管理員使用的應用程式
/usr/lib 庫檔案Glibc(32位)
/usr/lib64 庫檔案Glibc

/lib

/lib64

是/usr/lib和/usr/lib64的軟連結

這個目錄裡存放著系統最基本的動態連結共享庫,包含許多被/bin/和/sbin/中的程式使用的庫檔案,目錄/usr/lib/中含有更多用於使用者程式的庫檔案。作用類似於windows裡的DLL檔案,幾乎所有的應用程式都需要用到這些共享庫

注:lib***.a是靜態庫lib***.so是動態庫

靜態庫在編譯時被載入到二進位制檔案中
動態庫在執行時載入到程序的記憶體空間中

簡單的說:這些庫是為了讓你的程式能夠正常編譯執行的

其實類似於WIN中.dll檔案,幾乎所有的應用程式都需要用到這些共享庫

想要獲得更多視訊學習資料,新增小姐姐微信獲取哦!

3.1.2 絕對路徑和相對路徑

路徑:在我們平時使用計算機時要找到需要的檔案就必須知道檔案的位置,而表示檔案的位置的方式就是路徑

絕對路徑在Linux中,絕對路徑是從"/"開始的,比如/usr、/etc/passwd。如果一個路徑是從根(/開始的,它一定是絕對路徑.

相對路徑相對路徑是以 . 或 .. 開始的,

[root@xuegod63 etc]# pwd#判斷使用者當前所處的位置

絕對路徑:/開始的路徑 /home/user1

相對路徑: 相對於當前目錄開始a.txt ./a.txt ../root/b.txt當前目錄在/etc

[root@xuegod63 ~]# cd /etc/

[root@xuegod63 etc]# ll passwd

-rw-r--r-- 1 root root 2116 11月 16 14:57 passwd

[root@xuegod63 etc]# ll /etc/passwd

-rw-r--r-- 1 root root 2116 11月 16 14:57 /etc/passwd

3.2 檔案的管理

檔案管理方式有多種:

改變目錄: cd

建立/修改/移動/刪除: touch mkdir mv vi rm cp

3.2.1 建立檔案和資料夾

命令之:touch  ; touch[tʌtʃ]觸控;接觸;

作用:常用來建立空檔案,如果檔案存在,則修改這個檔案的時間

語法:touch 檔名

[root@xuegod63 ~]# cd /opt/

[root@xuegod63 opt]# touch a.txt

[root@xuegod63 opt]# touch file1 file2

[root@xuegod63 opt]# touch file{6..20}#建立file6到file20的檔案

[root@xuegod63 opt]# ls

a.txt file10 file12 file14 file16 file18 file2 file6 file8 rh

file1 file11 file13 file15 file17 file19 file20 file7 file9

[root@xuegod63 ~]# touch a.txt

[root@xuegod63 ~]# ll a.txt

-rw-r--r--. 1 root root 0 8月 10 21:13a.txt #這個時間是檔案的修改時間。

[root@xuegod63 ~]# stat a.txt#檢視檔案屬性(其中包括檔案時間屬性)

檔案:"a.txt"

大小:4 塊:8 IO 塊:4096 普通檔案

裝置:803h/2051dInode:18217320 硬連結:1

許可權:(0755/-rwxr-xr-x) Uid:( 0/ root) Gid:( 0/ root)

最近訪問:2021-03-09 23:03:19.747577494 +0800

最近更改:2021-03-09 23:03:58.216892266 +0800

最近改動:2021-03-09 23:04:12.116010166 +0800

注:

訪問時間:atime 檢視內容 cat a.txt

修改時間:mtime 修改內容 vim a.txt

改變時間:ctime 檔案屬性,比如許可權 change time。 chmod +w a.txt

[root@xuegod64 ~]# echo $LANG #檢視當前語言設定

zh_CN.UTF-8

[root@xuegod64 ~]# LANG=en_US.UTF-8 #切換為英文

[root@xuegod63 ~]# stat a.txt

[root@xuegod64 ~]# LANG=zh_CN.UTF-8 #切換為中文

3.2.2 使用vim和重定向建立一個新檔案

用vim命令建立一個新檔案

[root@xuegod63 mnt]# vim xuegod-1.txt

用重定向建立一新檔案

[root@xuegod63 mnt]#echo aaa>xuegod.txt

3.2.3 命令之:mkdir

作用:建立目錄

語法:mkdir (選項) 資料夾名子

例:

[root@xuegod63 opt]# mkdir dir1

[root@xuegod63 opt]# mkdir dir2dir3/home/dir4

[root@xuegod63 opt]# ls /home/

dir4 user1

[root@xuegod63 opt]# mkdir /tmp/a/b/c

mkdir: 無法建立目錄"/tmp/a/b/c": 沒有那個檔案或目錄

[root@xuegod63 opt]# mkdir -p /tmp/a/b/c#在建立一個目錄的時候,如果這個目錄的上一級不存在的話,要加引數-p

[root@xuegod63 opt]# tree /tmp/a/b/c

互動:在當前目錄下,已經存在一個檔案後,可以建立同名的目錄檔案嗎?

[root@xuegod63 opt]# touch aaa

[root@xuegod63 opt]# mkdir aaa

#不可以建立成功,因為目錄下不能存在兩個同名的檔案。

3.2.4 刪除檔案和目錄

用到的命令:rm

作用:可以刪除一個目錄中的一個或多個檔案或目錄,對於連結檔案,只是刪除整個連結檔案,而原檔案保持不變的

語法:rm (選項) 處理物件

選項:

-f 強制刪除,沒有提示

-r 刪除目錄,遞迴刪除,把目錄下的檔案和資料夾也刪除

例子:

[root@xuegod63 opt]# rm -rf a.txt

[root@xuegod63 opt]# rm -rf a.txt dir

[root@xuegod63 opt]# rm -rf file*

rm -rf (慎用,一定要在刪除以前確定一下所在目錄,防止誤刪除重要資料)

3.2.5 複製檔案

命令:cp 原始檔/目錄 目標檔案/目錄

選項:-R/r:遞迴處理,將指定目錄下的所有檔案與子目錄一併處理。複製目錄時會使用r引數

例子:

[root@xuegod63 ~]# cp /etc/passwd /opt/#複製檔案

[root@xuegod63 ~]# cp -r /boot/grub2 /opt/#複製目錄

[root@xuegod64 ~]# cp -r /boot/grub2 /etc/passwd /opt #複製多個檔案到目錄

選項:\:覆蓋檔案,不提示

例子:

[root@xuegod64 ~]# \cp -r /boot/grub2 /etc/passwd /opt #\反斜槓,覆蓋檔案,不提示

選項:-p:複製檔案會保留檔案屬性

例子:

[root@xuegod64 ~]# touch c.txt

[root@xuegod64 ~]# chmod a+w c.txt #新增寫許可權

[root@xuegod64 ~]# ll c.txt #有寫許可權

[root@xuegod64 ~]# cp c.txt /opt/ #複製到目錄

互動:複製到目錄後,這個檔案會帶上w寫許可權嗎?

[root@xuegod64 ~]# ll /opt/c.txt #檢查,沒有寫許可權

[root@xuegod64 ~]# \cp -p c.txt /opt #-p選項會保留檔案屬性

[root@xuegod64 ~]# ll /opt/c.txt #檢查,有寫許可權

[root@xuegod63 opt]# mkdir dir1 dir2 dir3

[root@xuegod63 opt]# \cp -rp/opt/!(dir1 | dir2) /root/ #除了dir1或dir2以外其他檔案都複製到root目錄下

3.2.6 移動檔案

[root@xuegod63 ~]# cd /opt

[root@xuegod63 opt]# mv passwd bbbpasswd#重新命名

[root@xuegod63 opt]# mv bbbpasswd /opt/passwd#在移動檔案的時候支援改名操作

[root@xuegod63 opt]# touch file1 file2 file3 file4

[root@xuegod63 opt]# mv /opt/!(file1 | file2) /root/ #除了file1或file2以外其他檔案都移動到root目錄下

3.3 檢視檔案

3.3.1 命令之:cat

語法:cat 檔名

作用:檢視檔案內容,一次顯示整個檔案的內容

例子: [root@xuegod63 ~]# cat /etc/passwd

3.3.2 命令之:more

作用:以分頁形式顯示檔案內容

語法:more + 檔案

說明: 按下回車重新整理一行,按下空格重新整理一屏,輸入q鍵退出

例:

[root@xuegod63 ~]# cat /proc/cpuinfo | more

[root@xuegod63 ~]# ifconfig | more

[root@xuegod63 ~]# more /proc/cpuinfo

3.3.3 命令之:less

作用:和more功能一樣

語法:less +檔名

說明:linux中more與less的區別

more:不支援後退,但幾乎不需要加引數,空格鍵是向下翻頁,Enter鍵是向下翻一行,在不需要後退的情況下比較方便

less:支援前後翻滾,既可以向上翻頁(pageup按鍵),也可以向下翻頁(pagedown按鍵)。,空格鍵是向下翻頁,Enter鍵是向下翻一行

想要獲得

3.3.4 命令之:head

作用: 用於顯示檔案的開頭的內容。在預設情況下,head命令顯示檔案的頭10行內容

語法:head(選項)檔名

引數: -n 顯示從檔案頭開始的行數

[root@xuegod63 opt]# head /etc/passwd

[root@xuegod63 opt]# head -n 3 /etc/passwd#顯示前3行

3.3.5 命令之:tail

作用: 用於顯示檔案中的尾部內容。預設在螢幕上顯示指定檔案的末尾10行

語法:tail (選項)檔名

引數:

-n 顯示檔案尾部多少行的內容(n為數字)

-f 動態顯示資料(不關閉),常用來檢視日誌

[root@xuegod63 ~]# tail -n 3/var/log/secure #檢視最後3行記錄

[root@xuegod63 ~]# tail -3/var/log/secure #檢視最後3行記錄

[root@xuegod63 ~]# tail -f /var/log/secure#在一個終端執行此命令動態檢視檔案內容

[root@xuegod63 ~]# tailf /var/log/secure#在一個終端執行此命令動態檢視檔案內容

[root@xuegod63 ~]# ssh [email protected]#在另一個終端遠端登入Linux,登入成功後

[root@xuegod63 ~]# tail -f /var/log/secure#可以動態檢視到登入成功的日誌

Nov 17 00:08:32 xuegod63 sshd[2924]: Accepted password for root from 192.168.1.63 port 39904 ssh2

3.4 實戰:xfs檔案系統的備份和恢復

XFS提供了 xfsdumpxfsrestore工具協助備份XFS檔案系統中的資料。xfsdump 按inode順序備份一個XFS檔案系統。

centos7開始選擇xfs格式作為預設檔案系統,而且不再使用以前的ext,仍然支援ext4,xfs專為大資料產生,每個單個檔案系統最大可以支援8EB(1EB=1024PB,1PB=1024TB,1TB=1024GB),單個檔案可以支援16TB,不僅資料量大,而且擴充套件性高。還可以通過xfsdump,xfsrestore來備份和恢復。

與傳統的UNIX檔案系統不同,XFS不需要在備份前被解除安裝;對使用中的XFS檔案系統做備份就可以保證映象的一致性。XFS的備份恢復的過程是可以被中斷然後繼續的,無須凍結檔案系統。xfsdump 甚至提供了高效能的多執行緒備份操作——它把一次dump拆分成多個數據流,每個資料流可以被髮往不同的目的地

首先了解一下xfsdump的備份級別有以下兩種,預設為0(即完全備份)

0 級別代表: 完全備份

1 到9級別代表: 增量備份

xfsdump level級別

對完整的檔案系統進行備份,用xfsdump。

他除了可以進行完整備份,還可以進行累積性備份。第一次使用xfsdump,是完整備份,定義為level 0,而第二次備份,則不會完整備份,而是和第一次的備份(level 0)進行比較,僅備份有差異的檔案(level 1)。

xfsdump常用引數

-L:xfsdump會記錄每次備份的session Label,這裡可以填寫針對此檔案系統的簡易說明;

-M:xfsdump可以記錄儲存Media Label,這裡可以填寫此媒體的簡易說明。

-l:是L的小寫,就是指定level,有0~9共10個等級,預設為0,即完整備份。

-f:後面接產生的檔案和destination file 。例如/dev/st0裝置檔名或其他一般檔案檔名

-I:大寫的“i”,從/var/lib/xfsdump/inventory 列出目前備份的資訊狀態。

擴充套件:

完全備份:每次都把指定的備份目錄完整的複製一遍,不管目錄下的檔案有沒有變化; 如:cp

增量備份:每次將之前(第一次、第二次、直到前一次)做過備份之後有變化的檔案進行備份;

差異備份:每次都將第一次完整備份以來有變化的檔案進行備份。如: rsync 備份

3.4.1 環境準備

實驗環境:

新增磁碟大小:20G à分割槽à格式化à掛載

選擇硬碟,下一步。

型別選擇

預設即可

下一步

位置和名稱預設即可

新增第二塊硬碟後效果

開機後對新新增的硬碟進行格式化:

[root@xuegod63 ~]# fdisk /dev/sdb

#指定分割槽的裝置,centos8第二塊硬碟的第一分割槽名為/dev/sdb1

歡迎使用 fdisk (util-linux 2.23.2)。

更改將停留在記憶體中,直到您決定將更改寫入磁碟。

使用寫入命令前請三思。

Device does not contain a recognized partition table

使用磁碟識別符號 0x06d5a427 建立新的 DOS 磁碟標籤。

命令(輸入 m 獲取幫助):n #建立一個新的分割槽

Partition type:

p primary (0 primary, 0 extended, 4 free)

e extended

Select (default p):p#建立一個主分割槽

分割槽號 (1-4,預設 1): #直接回車

起始 扇區 (2048-41943039,預設為 2048):

將使用預設值 2048

Last 扇區, +扇區 or +size{K,M,G} (2048-41943039,預設為 41943039):+1G #指定分割槽大小

分割槽 1 已設定為 Linux 型別,大小設為 1 GiB

命令(輸入 m 獲取幫助):p #列印分割槽表

磁碟 /dev/sdb:21.5 GB, 21474836480 位元組,41943040 個扇區

Units = 扇區 of 1 * 512 = 512 bytes

扇區大小(邏輯/物理):512 位元組 / 512 位元組

I/O 大小(最小/最佳):512 位元組 / 512 位元組

磁碟標籤型別:dos

磁碟識別符號:0x06d5a427

裝置 啟動 起點 末尾 扇區 大小 Id 型別

/dev/sdb12048 2099199 2097152 1G 83 Linux

命令(輸入 m 獲取幫助):w #儲存

The partition table has been altered!

Calling ioctl() to re-read partition table.

正在同步磁碟。

[root@xuegod63 ~]# ls /dev/sdb*

/dev/sdb/dev/sdb1

使用新的分割槽,格式化分割槽,並進行掛載

[root@xuegod63 ~]# mkfs.xfs /dev/sdb1#使用xfs格式對分割槽進行格式化

[root@xuegod63 ~]# blkid /dev/sdb1 #檢視分割槽資訊

[root@xuegod63 ~]# mkdir /test #建立掛載點

[root@xuegod63 ~]# mount /dev/sdb1 /test #掛載

[root@xuegod63 ~]# df -hT #或者用lsblk命令

準備備份測試檔案

[root@xuegod63 ~]# cd /test/ #進入test目錄

[root@xuegod63 test]# cp /etc/passwd ./#拷貝passwd檔案到當前目錄

[root@xuegod63 test]# mkdir ./xuegod#在當前目錄內建立xuegod目錄

[root@xuegod63 test]# touch ./xuegod/a #在當前目錄的xuegod目錄內建立a檔案

[root@xuegod63 test]# tree /test/

/test/

├── passwd

└── xuegod

└── a

3.4.2 備份

1、備份整個分割槽。 (這個功能就像是虛擬機器的快照,伺服器被黑後,進行快速恢復)

語法格式:xfsdump -f 這裡寫備份後儲存的位置和檔名這裡寫要備份的檔案路徑或裝置

注意:備份的路徑這裡不能寫成/test/。 可以是/dev/sdb1 或/test 即目錄後不能有/

[root@xuegod63 test]# xfsdump -f /opt/dump_sdb1 /dev/sdb1

#備份的檔案放在/opt下, 要備份的裝置是/dev/sdb1

xfsdump: using file dump (drive_simple) strategy

xfsdump: version 3.1.8 (dump format 3.0) - type ^C for status and control

=============================dumplabeldialog===========================

please enter label for this dump session (timeout in 300 sec)

-> dump_sdb1這裡寫對此次備份的說明。如:dump_sdb1,可以隨意寫。

session label entered: "dump_sdb1"

--------------------------------- end dialog ---------------------------------

xfsdump: level 0 dump of xuegod63.cn:/test

xfsdump: dump date: Tue Mar 10 16:03:07 2020

xfsdump: session id: e7d1aab1-dfbd-4b77-9379-56d63828a013

xfsdump: session label: "dump_nvme0n2p1"

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

============== media label dialog =============================

please enter label for media in drive 0 (timeout in 300 sec)

-> sdb1這裡寫對要備份的裝置做一個說明。如: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 26072 bytes

xfsdump: dump size (non-dir files) : 3104 bytes

xfsdump: dump complete: 27 seconds elapsed

xfsdump: Dump Summary:

xfsdump: stream 0 /opt/dump_sdb1OK (success)

xfsdump: Dump Status: SUCCESS

擴充套件: 備份前先檢視分割槽的檔案系統型別。

[root@xuegod63 ~]# df -T

檔案系統 型別 1K-塊 已用 可用 已用% 掛載點

devtmpfs devtmpfs 975248 0 975248 0% /dev

tmpfs tmpfs 1003548 0 1003548 0% /dev/shm

tmpfs tmpfs 1003548 9908 993640 1% /run

tmpfs tmpfs 1003548 0 1003548 0% /sys/fs/cgroup

/dev/sda3 xfs 52403200 5352704 47050496 11% /

/dev/sda1 ext4 999320 175832 754676 19% /boot

(發現boot預設是ext4檔案系統型別,因此boot分割槽不能執行xfs備份)

/dev/sdb1 xfs1038336 40296 998040 4% /test

2、 指定備份時免互動操作,方便後期做定時備份

[root@xuegod63 test]# xfsdump -f /opt/dump_sdb1_v2 /dev/sdb1 -L dump_sdb1_v2 -M sdb1_v2

-L : <session Label>備份的會話標籤,這裡寫對此次備份的說明。如:dump_sdb1_v2,可以隨意寫。

-M :<media label> 儲存裝置的標籤,這裡寫對備份裝置的簡易描述。如: sdb1_v2 ,可以隨意寫。

3、指定只備份分割槽中某個目錄

引數:-s 檔案路徑 只對指定的檔案進行備份,-s指定時,路徑寫的是相對路徑(-s可以是檔案或目錄)

[root@xuegod63 ~]# xfsdump -f /opt/xuegod_dump1 -s xuegod /dev/sdb1 -L dump_xuegod -M sdb1

4、檢視備份資訊與內容

備份成功後,我們就可以在/var/lib/xfsdump/inventory目錄下看到生成的檔案資訊

[root@xuegod63 opt]# xfsdump-I(字母大寫I)不是L

可以看到session資訊

測試恢復:先刪除之前建立的內容

[root@xuegod63 test]# ls

passwd xuegod

[root@xuegod63 test]# pwd

/test

[root@xuegod63 sdb1]# rm -rf ./*

3.4.3 檔案系統恢復

語法:xfsrestore -f 指定恢復檔案的位置 指定存放恢復後的檔案的路徑

[root@xuegod63 opt]# xfsrestore -f /opt/dump_sdb1 /test/

#檢視恢復情況

[root@xuegod63 ~]# ls /test/

恢復單個檔案如下:

[root@xuegod63 ~]# mkdir /var/test/

[root@xuegod63 ~]# xfsrestore -f /opt/xuegod_dump1 -sxuegod /var/test/#恢復目錄

檢視備份檔案的內容:

[root@xuegod63 ~]# xfsrestore -f /opt/dump_sdb1 -t

注:

使用xfsdump時,請注意下面下面幾個限制:

1、xfsdump不支沒有掛載的檔案系統備份!所以只能備份已掛載的!

2、xfsdump必須使用root的許可權才能操作(涉及檔案系統的關係)

3、xfsdump只能備份XFS檔案系統

4、xfsdump備份下來的資料(檔案或儲存媒體)只能讓xfsrestore解析

5、xfsdump是透過檔案系統的UUID來分辨各個備份檔的,因此不能備份兩個具有相同UUID的檔案系統

3.4.4 增量備份

概念

增量備份是指在一次全備份或上一次增量備份後,以後每次的備份只需備份與前一次相比增加或者被修改的檔案。這就意味著,第一次增量備份的物件是進行全備後所產生的增加和修改的檔案;第二次增量備份的物件是進行第一次增量備份後所產生的增加和修改的檔案,以此類推。

優缺點

優點:沒有重複的備份資料,因此備份的資料量不大,備份所需的時間很短。

缺點:增量備份的資料恢復比較麻煩,您必須具有上一次全量備份和所有增量備份資料(一旦丟失或損壞,就會導致恢復失敗)並且恢復過程中,必須遵循從最近一次全量備份開始,並以最近一次全量備份為起點,按備份等級依次進行資料恢復。如圖:

實戰: 增量備份檔案系統

準備一個備份目錄進行備份

[root@xuegod63 test]# tree /test/

/test/

├── passwd

└── xuegod

└── a

1 directory, 2 files對上面的內容進行第一次全備

[root@xuegod63 test]# xfsdump -f /opt/test-full /test -L test-full -M sdb1

#增加一些內容,然後進行第1次增量備份

[root@xuegod63 test]# touch /test/1.txt /test/2.txt

[root@xuegod63 test]# echo 'i love you' > /test/1.txt

[root@xuegod63 test]# tree

├── 1.txt

├── 2.txt

├── passwd

└── xuegod

└── a

1 directory, 4 files

[root@xuegod63 test]# xfsdump -l 1 -f /opt/test-bak1 /test -L test-bak1 -M sdb1

-l <level> 小寫的L,不是大寫的i 做一個等級為1的備份

再次增加內容,然後進行level 2級別的增量備

[root@xuegod63 test]# touch /test/xuegod/a.txt /test/xuegod/b.txt

[root@xuegod63 test]# echo 'i hateyou' > /test/1.txt

[root@xuegod63 test]# tree /test/

/test/

├── 1.txt

├── 2.txt

├── passwd

└── xuegod

├── a

├── a.txt

└── b.txt

1 directory, 6 files

[root@xuegod63 test]# xfsdump -l 2 -f /opt/test-bak2 /test -L test-bak2 -M sdb1

[root@xuegod63 test]# xfsrestore -f /opt/test-bak2 -t

[root@xuegod63 test]# xfsrestore -f /opt/test-bak1 -t

[root@xuegod63 ~]# rm -rf /test/*#刪除所有資料

現在進行恢復,要想恢復全部全部資料,包括新新增的檔案,如何恢復?

步驟:

1、先恢復完全備份

2、情況1: 恢復最後一次增量備份(如果兩次增量備份都是1級的,只需要恢復最後一個增量就可以了。

3、情況2:如果你做的是第一次是1級備,第二次是2級備,那麼你在恢復的時候就需要先恢復完全備份,然後是1級備,最後是2級備)

注:xfs增量備份恢復時。要想把資料完全恢復,必須按順序恢復。

不按順序恢復是可以恢復一部分資料,但是不完整,比如例子中,1.txt的值最後是I hate you,如果先恢復bak2,然後恢復bak1,那麼1.txt中的值將是I love you

[root@xuegod63 ~]# xfsrestore -f /opt/test-full /test/

[root@xuegod63 ~]# xfsrestore -f /opt/test-bak1/test/#先恢復back1,檢視

[root@xuegod63 ~]# tree /test/#檢視恢復的檔案

[root@xuegod63 ~]# xfsrestore -f /opt/test-bak2/test/#再恢復back2

[root@xuegod63 ~]# tree /test/

到此,資料恢復成功了。