第九章、檔案與檔案系統的壓縮與打包
9.2 Linux 系統常見的壓縮指令
9.2.1 compress(很少人用了)
[root@www ~]# compress [-rcv] 檔案或目錄 <==這裡是壓縮 [root@www ~]# uncompress 檔案.Z <==這裡是解壓縮 選項與參數: -r :可以連同目錄下的檔案也同時給予壓縮呢! -c :將壓縮資料輸出成為 standard output (輸出到螢幕) -v :可以秀出壓縮後的檔案資訊以及壓縮過程中的一些檔名變化。
範例一:將 /etc/man.config 複製到 /tmp ,並加以壓縮 [root@www ~]# cd /tmp [root@www tmp]# cp /etc/man.config . [root@www tmp]# compress -v man.config man.config: -- replaced with man.config.Z Compression: 41.86% [root@www tmp]# ls -l /etc/man.config /tmp/man* -rw-r--r-- 1 root root 4617 Jan 6 2007 /etc/man.config <==原有檔案 -rw-r--r-- 1 root root 2684 Nov 10 17:14 /tmp/man.config.Z <==經過壓縮的檔案! |
不知道你有沒有發現,複製到 /tmp 的 man.config 不見了!因為被壓縮成為 man.config.Z 囉也就是說,在預設的情況中,被 compress 壓縮的原始檔案會不見,而壓縮檔案會被建立起來,而且副檔名會是 *.Z。仔細看一下,檔案由原本的 4617bytes 降低到 2684bytes左右,確實有減少一點啦!
範例二:將剛剛的壓縮檔解開 [root@www tmp]# uncompress man.config.Z [root@www tmp]# ll man* -rw-r--r-- 1 root root 4617 Nov 10 17:14 man.config |
解壓縮直接用 uncompress 即可!解壓縮完畢後該檔案就自動的變回來了!不過,那個壓縮檔卻又不存在囉~這樣可以理解用法了嗎?那如果我想要保留原始檔案且又要建立壓縮檔呢?可以使用 -c 的語法!
範例三:將 man.config 壓縮成另外一個檔案來備份 [root@www tmp]# compress -c man.config > man.config.back.Z [root@www tmp]# ll man* -rw-r--r-- 1 root root 4617 Nov 10 17:14 man.config -rw-r--r-- 1 root root 2684 Nov 10 17:24 man.config.back.Z # 這個 -c 的選項比較有趣!他會將壓縮過程的資料輸出到螢幕上,而不是寫入成為 # *.Z 的壓縮檔。所以,我們可以透過資料流重導向的方法將資料輸出成為另一個檔名。 # 關於資料流重導向,我們會在第十一章 bash 詳細談論的啦! |
9.4 完整備份工具:dump, restore(这个命令针对ext2/ext3文件系统)
[root@localhost tmp]# rpm -qf/sbin/dump
dump-0.4b41-6.el5
[root@localhost tmp]# yum search"dump" | grep "^dump.x86_64"
dump.x86_64 : Programs for backing up andrestoring ext2/ext3 filesystems
9.4.1 dump
圖 4.1.1、dump 運作的等級 (level)
雖然 dump 支援整個檔案系統或者是單一各別目錄,但是對於目錄的支援是比較不足的,這也是dump 的限制所在。 簡單的說,如果想要備份的資料如下時,則有不同的限制情況:
- 當待備份的資料為單一檔案系統:
如果是單一檔案系統 (filesystem) ,那麼該檔案系統可以使用完整的 dump 功能,包括利用 0~9 的數個 level 來備份, 同時,備份時可以使用掛載點或者是裝置檔名 (例如 /dev/sda5 之類的裝置檔名) 來進行備份! - 待備份的資料只是目錄,並非單一檔案系統:
例如你僅想要備份 /home/someone/ ,但是該目錄並非獨立的檔案系統時。此時備份就有限制啦!包括:- 所有的備份資料都必須要在該目錄 (本例為:/home/someone/) 底下;
- 且僅能使用 level 0 ,亦即僅支援完整備份而已;
- 不支援 -u 選項,亦即無法建立 /etc/dumpdates 這個各別 level 備份的時間記錄檔;
dump 的選項雖然非常的繁複,不過如果只是想要簡單的操作時,您只要記得底下的幾個選項就很夠用了!
[root@www ~]# dump [-Suvj] [-level] [-f 備份檔] 待備份資料 [root@www ~]# dump -W 選項與參數: -S :僅列出後面的待備份資料需要多少磁碟空間才能夠備份完畢; -u :將這次 dump 的時間記錄到 /etc/dumpdates 檔案中; -v :將 dump 的檔案過程顯示出來; -j :加入 bzip2 的支援!將資料進行壓縮,預設 bzip2 壓縮等級為 2 -level:就是我們談到的等級,從 -0 ~ -9 共十個等級; -f :有點類似 tar 啦!後面接產生的檔案,亦可接例如 /dev/st0 裝置檔名等 -W :列出在 /etc/fstab 裡面的具有 dump 設定的 partition 是否有備份過? |
1. 用 dump 備份完整的檔案系統
# 2. 先測試一下,如果要備份此檔案系統,需多少容量? [root@www ~]# dump -S /dev/hdc1 5630976 <==注意一下,這個單位是 bytes ,所以差不多是 5.6MBytes。
# 3. 將完整備份的檔名記錄成為 /root/boot.dump ,同時更新記錄檔: [root@www ~]# dump -0u -f /root/boot.dump /boot DUMP: Date of this level 0 dump: Tue Dec 2 02:53:45 2008 <==記錄等級與備份時間 DUMP: Dumping /dev/hdc1 (/boot) to /root/boot.dump <==dump的來源與目標 DUMP: Label: /boot <==檔案系統的 label DUMP: Writing 10 Kilobyte records DUMP: mapping (Pass I) [regular files] <==開始進行檔案對應 DUMP: mapping (Pass II) [directories] DUMP: estimated 5499 blocks. <==評估整體block數量 DUMP: Volume 1 started with block 1 at: Tue Dec 2 02:53:46 2008 DUMP: dumping (Pass III) [directories] <==開始 dump 工作 DUMP: dumping (Pass IV) [regular files] DUMP: Closing /root/boot.dump <==結束寫入備份檔 DUMP: Volume 1 completed at: Tue Dec 2 02:53:47 2008 DUMP: Volume 1 5550 blocks (5.42MB) <==最終備份資料容量 DUMP: Volume 1 took 0:00:01 DUMP: Volume 1 transfer rate: 5550 kB/s DUMP: 5550 blocks (5.42MB) on 1 volume(s) DUMP: finished in 1 seconds, throughput 5550 kBytes/sec DUMP: Date of this level 0 dump: Tue Dec 2 02:53:45 2008 DUMP: Date this dump completed: Tue Dec 2 02:53:47 2008 DUMP: Average transfer rate: 5550 kB/s DUMP: DUMP IS DONE # 在指令的下達方面,dump 後面接 /boot 或 /dev/hdc1 都可以的! # 而執行 dump 的過程中會出現如上的一些訊息,您可以自行仔細的觀察!
[root@www ~]# ll /root/boot.dump /etc/dumpdates -rw-rw-r-- 1 root disk 43 Dec 2 02:53 /etc/dumpdates -rw-r--r-- 1 root root 5683200 Dec 2 02:53 /root/boot.dump # 由於加上 -u 的選項,因此 /etc/dumpdates 該檔案的內容會被更新!注意, # 這個檔案僅有在 dump 完整的檔案系統時才有支援主動更新的功能。
# 4. 觀察一下系統主動建立的記錄檔: [root@www ~]# cat /etc/dumpdates /dev/hdc1 0 Tue Dec 2 02:53:47 2008 +0800 [檔案系統] [等級] [ ctime 的時間 ] |
# 0. 看一下有沒有任何檔案系統被 dump 過的資料? [root@www ~]# dump -W Last dump(s) done (Dump '>' file systems): > /dev/hdc2 ( /) Last dump: never > /dev/hdc3 ( /home) Last dump: never /dev/hdc1 ( /boot) Last dump: Level 0, Date Tue Dec 2 02:53:47 2008 # 如上列的結果,該結果會捉出 /etc/fstab 裡面第五欄位設定有需要 dump 的 # partition,然後與 /etc/dumpdates 進行比對,可以得到上面的結果啦! # 尤其是第三行,可以顯示我們曾經對 /dev/hdc1 進行過 dump 的備份動作喔!
# 1. 先惡搞一下,建立一個大約 10 MB 的檔案在 /boot 內: [root@www ~]# dd if=/dev/zero of=/boot/testing.img bs=1M count=10 10+0 records in 10+0 records out 10485760 bytes (10 MB) copied, 0.166128 seconds, 63.1 MB/s
# 2. 開始建立差異備份檔,此時我們使用 level 1 吧: [root@www ~]# dump -1u -f /root/boot.dump.1 /boot ....(中間省略)....
[root@www ~]# ll /root/boot* -rw-r--r-- 1 root root 5683200 Dec 2 02:53 /root/boot.dump -rw-r--r-- 1 root root 10547200 Dec 2 02:56 /root/boot.dump.1 # 看看檔案大小,豈不是就是剛剛我們所建立的那個大檔案的容量嗎? ^_^
# 3. 最後再看一下是否有記錄 level 1 備份的時間點呢? [root@www ~]# dump -W Last dump(s) done (Dump '>' file systems): > /dev/hdc2 ( /) Last dump: never > /dev/hdc3 ( /home) Last dump: never > /dev/hdc1 ( /boot) Last dump: Level 1, Date Tue Dec 2 02:56:33 2008 ....(中間省略).... |
2. 用 dump 備份非檔案系統,亦即單一目錄的方法
# 讓我們將 /etc 整個目錄透過 dump 進行備份,且含壓縮功能 [root@www ~]# dump -0j -f /root/etc.dump.bz2 /etc DUMP: Date of this level 0 dump: Tue Dec 2 12:08:22 2008 DUMP: Dumping /dev/hdc2 (/ (dir etc)) to /root/etc.dump.bz2 DUMP: Label: /1 DUMP: Writing 10 Kilobyte records DUMP: Compressing output at compression level 2 (bzlib) DUMP: mapping (Pass I) [regular files] DUMP: mapping (Pass II) [directories] DUMP: estimated 115343 blocks. DUMP: Volume 1 started with block 1 at: Tue Dec 2 12:08:23 2008 DUMP: dumping (Pass III) [directories] DUMP: dumping (Pass IV) [regular files] DUMP: Closing /root/etc.dump.bz2 DUMP: Volume 1 completed at: Tue Dec 2 12:09:49 2008 DUMP: Volume 1 took 0:01:26 DUMP: Volume 1 transfer rate: 218 kB/s DUMP: Volume 1 124680kB uncompressed, 18752kB compressed, 6.649:1 DUMP: 124680 blocks (121.76MB) on 1 volume(s) DUMP: finished in 86 seconds, throughput 1449 kBytes/sec DUMP: Date of this level 0 dump: Tue Dec 2 12:08:22 2008 DUMP: Date this dump completed: Tue Dec 2 12:09:49 2008 DUMP: Average transfer rate: 218 kB/s DUMP: Wrote 124680kB uncompressed, 18752kB compressed, 6.649:1 DUMP: DUMP IS DONE # 上面特殊字體的部分顯示:原本有 124680kb 的容量,被壓縮成為 18752kb, # 整個壓縮比為 6.649:1 ,還可以的壓縮情況啦! |
9.4.2 restore
[root@www ~]# restore -t [-f dumpfile] [-h] <==用來察看 dump 檔 [root@www ~]# restore -C [-f dumpfile] [-D 掛載點] <==比較dump與實際檔案 [root@www ~]# restore -i [-f dumpfile] <==進入互動模式 [root@www ~]# restore -r [-f dumpfile] <==還原整個檔案系統 選項與參數: 相關的各種模式,各種模式無法混用喔!例如不可以寫 -tC 啦! -t :此模式用在察看 dump 起來的備份檔中含有什麼重要資料!類似 tar -t 功能; -C :此模式可以將 dump 內的資料拿出來跟實際的檔案系統做比較, 最終會列出『在 dump 檔案內有記錄的,且目前檔案系統不一樣』的檔案; -i :進入互動模式,可以僅還原部分檔案,用在 dump 目錄時的還原! -r :將整個 filesystem 還原的一種模式,用在還原針對檔案系統的 dump 備份; 其他較常用到的選項功能: -h :察看完整備份資料中的 inode 與檔案系統 label 等資訊 -f :後面就接你要處理的那個 dump 檔案囉! -D :與 -C 進行搭配,可以查出後面接的掛載點與 dump 內有不同的檔案! |
1. 用 restore 觀察 dump 後的備份資料內容(前面的数字是什么?比如2009 ./grup/grub.conf)
[root@www ~]# restore -t -f /root/boot.dump Dump date: Tue Dec 2 02:53:45 2008 <==說明備份的日期 Dumped from: the epoch Level 0 dump of /boot on www.vbird.tsai:/dev/hdc1 <==說明 level 狀態 Label: /boot <==說明該 filesystem 的表頭! 2 . 11 ./lost+found 2009 ./grub 2011 ./grub/grub.conf ....底下省略....
[root@www ~]# restore -t -f /root/etc.dump Dump tape is compressed. <==加註說明資料有壓縮 Dump date: Tue Dec 2 12:08:22 2008 Dumped from: the epoch Level 0 dump of / (dir etc) on www.vbird.tsai:/dev/hdc2 <==是目錄! Label: /1 2 . 1912545 ./etc 1912549 ./etc/rpm 1912550 ./etc/rpm/platform ....底下省略.... |
2. 比較差異並且還原整個檔案系統
# 0. 先嘗試變更檔案系統的內容: [root@www ~]# cd /boot [root@www boot]# mv config-2.6.18-128.el5 config-2.6.18-128.el5-back
# 1. 看使進行檔案系統與備份檔案之間的差異! [root@www boot]# restore -C -f /root/boot.dump Dump date: Tue Dec 2 02:53:45 2008 Dumped from: the epoch Level 0 dump of /boot on www.vbird.tsai:/dev/hdc1 Label: /boot filesys = /boot restore: unable to stat ./config-2.6.18-128.el5: No such file or directory Some files were modified! 1 compare errors # 看到上面的特殊字體了吧!那就是有差異的部分!總共有一個檔案被變更! # 我們剛剛確實有更動過該檔案,嘿嘿!這樣是否能瞭解?!
# 2. 將檔案系統改回來啊! [root@www boot]# mv config-2.6.18-128.el5-back config-2.6.18-128.el5 [root@www boot]# cd /root |
如同上面的動作,透過曾經備份過的資訊,也可以找到與目前實際檔案系統中有差異的資料呢!如果你不想要進行累積備份,但也能透過這個動作找出最近這一陣子有變動過的檔案說!瞭解乎?那如何還原呢?由於 dump 是記錄整個檔案系統的,因此還原時你也應該要給予一個全新的檔案系統才行。因此底下我們先建立一個檔案系統,然後再來還原吧!
# 1. 先建立一個新的 partition 來使用,假設我們需要的是 150M 的容量 [root@www ~]# fdisk /dev/hdc Command (m for help): n First cylinder (2335-5005, default 2335): <==這裡按 Enter Using default value 2335 Last cylinder or +size or +sizeM or +sizeK (2335-5005, default 5005): +150M Command (m for help): p ....中間省略.... /dev/hdc8 2335 2353 152586 83 Linux
Command (m for help): w
[root@www ~]# partprobe <==很重要的動作!別忘記! # 這樣就能夠建立一個 /dev/hdc8 的 partition ,然後繼續格式化吧!
[root@www ~]# mkfs -t ext3 /dev/hdc8 [root@www ~]# mount /dev/hdc8 /mnt
# 2. 開始進行還原的動作!請您務必到新檔案系統的掛載點底下去! [root@www ~]# cd /mnt [root@www mnt]# restore -r -f /root/boot.dump restore: ./lost+found: File exists |
3. 僅還原部分檔案的 restore 互動模式
[root@www ~]# cd /mnt [root@www mnt]# restore -i -f /root/etc.dump restore > # 此時你就已經進入 restore 的互動模式畫面中!要注意的是: # 你目前已經在 etc.dump 這個檔案內了!所有的動作都是在 etc.dump 內!
restore > help Available commands are: ls [arg] - list directory <==列出 etc.dump 內的檔案或目錄 cd arg - change directory <==在 etc.dump 內變更目錄 pwd - print current directory <==列出在 etc.dump 內的路徑檔名 add [arg] - add `arg' to list of files to be extracted delete [arg] - delete `arg' from list of files to be extracted extract - extract requested files # 上面三個指令是重點!各指令的功能為: # add file :將 file 加入等一下要解壓縮的檔案列表中 # delete file :將 file 移除出解壓縮的列表,並非刪除 etc.dump 內的檔案!別誤會!^_^ # extract :開始將剛剛選擇的檔案列表解壓縮了去! setmodes - set modes of requested directories quit - immediately exit program what - list dump header information verbose - toggle verbose flag (useful with ``ls'') prompt - toggle the prompt display help or `?' - print this list
restore > ls .: etc/ <==會顯示出在 etc.dump 內主要的目錄,因為我們備份 /etc ,所以檔名為此!
restore > cd etc <==在 etc.dump 內變換路徑到 etc 目錄下 restore > pwd <==列出本目錄的檔名為? /etc restore > ls passwd shadow group <==看看,真的有這三個檔案喔! passwd shadow group restore > add passwd shadow group <==加入解壓縮列表 restore > delete group <==加錯了!將 group 移除解壓縮列表 restore > ls passwd shadow group *passwd <==有要被解壓縮的,檔名之前會出現 * 的符號呢! *shadow group restore > extract <==開始進行解壓縮去! You have not read any volumes yet. <==這裡會詢問你需要的volume Unless you know which volume your file(s) are on you should start with the last volume and work towards the first. Specify next volume # (none if no more volumes): 1 <==只有一個 volume set owner/mode for '.'? [yn] n <==不需要修改權限
restore > quit <==離開 restore 的功能
[root@www ~]# ll -d etc drwxr-xr-x 2 root root 1024 Dec 15 17:49 etc <==解壓縮後,所建立出來的目錄啦! [root@www ~]# ll etc total 6 -rw-r--r-- 1 root root 1945 Sep 29 02:21 passwd -r-------- 1 root root 1257 Sep 29 02:21 shadow |