天天看點

Linux其他常見壓縮備份工具 - dd,cpio

dd

        dd 可以讀取磁碟裝置的内容(幾乎是直接讀取磁區"sector"),然後将整個裝置備份成一個檔案呢!真的是相當的好用啊~ dd 的用途有很多啦~但是我們僅講一些比較重要的選項,如下:

[[email protected] ~]# dd if="input_file" of="output_file" bs="block_size" \
> count="number"
選項與參數:
if   :就是 input file 羅~也可以是裝置喔!
of   :就是 output file 喔~也可以是裝置;
bs   :規劃的一個 block 的大小,若未指定則預設是 512 bytes(一個 sector 的大小)
count:多少個 bs 的意思。

範例一:将 /etc/passwd 備份到 /tmp/passwd.back 當中
[[email protected] ~]# dd if=/etc/passwd of=/tmp/passwd.back
3+1 records in
3+1 records out
1945 bytes (1.9 kB) copied, 0.000332893 seconds, 5.8 MB/s
[[email protected] ~]# ll /etc/passwd /tmp/passwd.back
-rw-r--r-- 1 root root 1945 Sep 29 02:21 /etc/passwd
-rw-r--r-- 1 root root 1945 Dec 17 18:09 /tmp/passwd.back
# 仔細的看一下,我的 /etc/passwd 檔案大小為 1945 bytes,因為我沒有配置 bs ,
# 是以預設是 512 bytes 為一個機關,是以,上面那個 3+1 表示有 3 個完整的 
# 512 bytes,以及未滿 512 bytes 的另一個 block 的意思啦!
# 事實上,感覺好像是 cp 這個指令啦~

範例二:将自己的磁碟之第一個磁區備份下來
[[email protected] ~]# dd if=/dev/hdc of=/tmp/mbr.back bs=512 count=1
1+0 records in
1+0 records out
512 bytes (512 B) copied, 0.0104586 seconds, 49.0 kB/s
# 第一個磁區内含有 MBR 與 partition table ,透過這個動作,
# 我們可以一口氣将這個磁碟的 MBR 與 partition table 進行備份哩!

範例三:找出你系統最小的那個分割槽,并且将他備份下來:
[[email protected] ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hdc2             9.5G  3.9G  5.1G  44% /
/dev/hdc3             4.8G  651M  3.9G  15% /home
/dev/hdc1              99M   21M   73M  23% /boot  <==就捉他好了!
[[email protected] ~]# dd if=/dev/hdc1 of=/tmp/boot.whole.disk
208782+0 records in
208782+0 records out
106896384 bytes (107 MB) copied, 6.24721 seconds, 17.1 MB/s
[[email protected] ~]# ll -h /tmp/boot.whole.disk
-rw-r--r-- 1 root root 102M Dec 17 18:14 /tmp/boot.whole.disk
# 等於是将整個 /dev/hdc1 通通捉下來的意思~如果要還原呢?就反向回去!
# dd if=/tmp/boot.whole.disk of=/dev/hdc1 即可!非常簡單吧!
# 簡單的說,如果想要整個硬碟備份的話,就類似 Norton 的 ghost 軟體一般,
# 由 disk 到 disk ,嘿嘿~利用 dd 就可以啦~厲害厲害!
      

例題:

        你想要将你的 /dev/hdc1 進行完整的複制到另一個 partition 上,請使用你的系統上面未分割完畢的容量再建立一個與 /dev/hdc1 差不多大小的分割槽 (隻能比 /dev/hdc1 大,不能比他小!),然後将之進行完整的複制 (包括需要複制 boot sector 的區塊)。

答:

        由於需要複制 boot sector 的區塊,是以使用 cp 或者是 tar 這種指令是無法達成需求的! 此時那個 dd 就派的上用場了。你可以這樣做:

# 1. 先進行分割的動作
[[email protected] ~]# fdisk -l /dev/hdc
   Device Boot   Start    End    Blocks   Id  System
/dev/hdc1   *        1     13    104391   83  Linux
# 上面鳥哥僅撷取重要的資料而已!我們可以看到 /dev/hdc1 僅有 13 個磁柱

[[email protected] ~]# fdisk /dev/hdc
Command (m for help): n
First cylinder (2354-5005, default 2354): 這裡按 enter
Using default value 2354
Last cylinder or +size or +sizeM or +sizeK (2354-5005, default 5005): 2366

Command (m for help): p
   Device Boot   Start    End    Blocks   Id  System
/dev/hdc9         2354   2366    104391   83  Linux

Command (m for help): w
# 為什麼要使用 2366 呢?因為 /dev/hdc1 使用 13 個磁柱,是以新的 partition
# 我們也給她 13 個磁柱,是以 2354 + 13 -1 = 2366 羅!

[[email protected] ~]# partprobe

# 2. 不需要格式化,直接進行 sector 表面的複制!
[[email protected] ~]# dd if=/dev/hdc1 of=/dev/hdc9
208782+0 records in
208782+0 records out
106896384 bytes (107 MB) copied, 16.8797 seconds, 6.3 MB/s

[[email protected] ~]# mount /dev/hdc9 /mnt
[[email protected] ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hdc1               101086     21408     74459  23% /boot
/dev/hdc9               101086     21408     74459  23% /mnt
# 這兩個玩意兒會『一模一樣』喔!
[[email protected] ~]# umount /mnt
      

cpio

[[email protected] ~]# cpio -ovcB  > [file|device] <==備份
[[email protected] ~]# cpio -ivcdu < [file|device] <==還原
[[email protected] ~]# cpio -ivct  < [file|device] <==察看
備份會使用到的選項與參數:
  -o :将資料 copy 輸出到檔案或裝置上 
  -B :讓預設的 Blocks 可以添加至 5120 bytes ,預設是 512 bytes ! 
     這樣的好處是可以讓大檔案的儲存速度加快(請參考 i-nodes 的觀念) 
還原會使用到的選項與參數:
  -i :将資料自檔案或裝置 copy 出來系統當中 
  -d :自動建立目錄!使用 cpio 所備份的資料内容不見得會在同一層目錄中,是以我們
       必須要讓 cpio 在還原時可以建立新目錄,此時就得要 -d 選項的幫助!
  -u :自動的将較新的檔案覆寫較舊的檔案!
  -t :需配合 -i 選項,可用在"察看"以 cpio 建立的檔案或裝置的内容 
一些可共享的選項與參數:
  -v :讓儲存的過程中檔案名稱可以在螢幕上顯示 
  -c :一種較新的 portable format 方式儲存 
      
範例:找出 /boot 底下的所有檔案,然後将他備份到 /tmp/boot.cpio 去!
[[email protected] ~]# find /boot -print
/boot
/boot/message
/boot/initrd-2.6.18-128.el5.img
....以下省略....
# 透過這個 find 我們可以找到 /boot 底下應該要存在的檔名!包括檔案與目錄

[[email protected] ~]# find /boot | cpio -ocvB > /tmp/boot.cpio
[[email protected] ~]# ll -h /tmp/boot.cpio
-rw-r--r-- 1 root root 16M Dec 17 23:30 /tmp/boot.cpio
      
範例:将剛剛的檔案給他在 /root/ 目錄下解開
[[email protected] ~]# cpio -idvc < /tmp/boot.cpio
[[email protected] ~]# ll /root/boot
# 你可以自行比較一下 /root/boot 與 /boot 的内容是否一模一樣!
      

其實系統裡面已經含有一個使用 cpio 建立的檔案喔!那就是 /boot/initrd-xxx 這個檔案啦!

# 1. 我們先來看看該檔案是屬於什麼檔案格式,然後再加以處理:
[[email protected] ~]# file /boot/initrd-2.6.18-128.el5.img
/boot/initrd-2.6.18-128.el5.img: gzip compressed data, ...
# 唔!看起來似乎是使用 gzip 進行壓縮過~那如何處理呢?

# 2. 透過更名,将該檔案添加擴充名,然後予以解壓縮看看:
[[email protected] ~]# mkdir initrd
[[email protected] ~]# cd initrd
[[email protected] initrd]# cp /boot/initrd-2.6.18-128.el5.img initrd.img.gz
[[email protected] initrd]# gzip -d initrd.img.gz
[[email protected] initrd]# ll
-rw------- 1 root root 5408768 Dec 17 23:53 initrd.img
[[email protected] initrd]# file initrd.img
initrd.img: ASCII cpio archive (SVR4 with no CRC)
# 嘿嘿!露出馬腳了吧!确實是 cpio 的檔案檔喔!

# 3. 開始使用 cpio 解開此檔案:
[[email protected] initrd]# cpio -iduvc < initrd.img
sbin
init
sysroot
....以下省略....
# 瞧!這樣就将這個檔案解開羅!這樣了解乎?      

繼續閱讀