用dd將硬碟直接備份到另外一顆硬碟 / How To Do Eveything With DD

目錄

[隱藏]

範例

主要的功能是使用 if= (輸入檔) 及 of= (輸出檔)。預設 dd會自stdin讀取,並寫入stdout中。以下為一些 dd的使用範例:

將硬碟直接備份到另外一顆硬碟

# dd if=/dev/hda of=/dev/sda conv=noerror,sync bs=4k

這個命令常用來將(/dev/hda)直接備份至(/dev/sda)磁碟。(通常裝置名稱/dev/hda為IDE硬碟,而/dev/sda常是USB儲存碟。)這個指令只有在磁碟有足夠夠空間儲存來源磁碟的整個檔案系統時才能使用。其優點是你不必將磁碟掛上來進行備分, and the only reference to hda is in /dev and in the command which is usually in a script in cron.

其中的參數”bs=4k”是用來指定拷貝中所用block的大小。dd指令預設中為512 bytes:使用小的block會明顯地導致拷貝速度變慢。然而,使用大的block則可能會在遇到錯誤時,block其餘部分都被填入zero-bytes。因此,如果你在拷貝一個有問題的裝置時增加你的block大小,你可能會遺失較多的data,雖然你可以花較短的時間在嘗試讀取破損的磁區。其他工具如dd_rescuedd_rhelp 可在這種情況下提供更為彈性的解決方案,在無錯誤的磁區得以大block的速度進行,而有問題的區域則採小block拷貝。

建立硬碟的映像檔

# dd if=/dev/hda | gzip > /mnt/hdb1/system_drive_backup.img.gz

在此處,用dd 來製作一個第一部硬碟的影像檔,並且將其結果導入gzip壓縮程式。其產生的壓縮結果則被置於另一磁碟的一個檔案中。反向的程序方法:

# gzip -dc /mnt/hdb1/system_drive_backup.img.gz | dd of=/dev/hda

如此,gzip加上-d的參數可以解壓這個備份檔,再把結果傳至stdout (用參數 -c ),以 pipe 傳入dd並寫至 /dev/hda。

拷貝軟碟

# dd if=/dev/fd0 of=/tmp/floppy.img bs=10240

這將會將軟碟的內容拷貝為一個檔案,然後如果要把這個影像檔於到一片新的軟碟片上,把 “if” 與 “of” 兩個參數對調即可。

# dd if=/tmp/floppy.img of=/dev/fd0 bs=10240

備份你的主要開機記錄磁區(Master Boot Record, MBR

在你編輯你的磁碟分割表前,你應該要先做這個動作,如果你搞砸了才能將之復原。

# dd if=/dev/hda of=/root/hda.boot.mbr bs=512 count=1

如果真的搞砸了,你可以用 Knoppix開機,掛上含有 /root(本例中為hda1)的分割區,以下述命令置回MBR:

# dd if=/mnt/hda1/root/hda.boot.mbr of=/dev/hda bs=512 count=1

明顯地,如果你有GPT系統(如Intel MAC),這將需要一些修改。

見: http://forum.onmac.net/showthread.php?t=136

你可以用這個指令僅備份MBR而不含分割表:

 # dd if=/dev/hda of=/root/hda.mbr.noparttab bs=446 count=1

將映像檔依照檔案大小作分割

When making images, it’s quite easy to run up against various file size limitations. One way to work around a given file size limitation is to use the split command.

# dd if=/dev/hda1 | gzip -c | split -b 2000m - /mnt/hdc1/backup.img.gz.
  1. This example is using dd to take an image of the first partition on the first harddrive.
  2. The results are passed through to gzip for compression
    • The -c option switch is used to output the result to stdout.
  3. The compressed image is then piped to the split tool
    • The -b 2000m switch tells split how big to make the individual files. You can use k and m to tell switch kilobytes and megabytes (this option uses bytes by default).
    • The – option tells split to read from stdin. Otherwise, split would interpret the /mnt/hdc1… as the file to be split.
    • The /mnt/hdc1… is the prefix for the created files. Split will create files named backup.img.gz.aa, backup.img.gz.ab, etc.

To restore the multi-file backup, do the following:

# cat /mnt/hdc1/backup.img.gz.* | gzip -dc | dd of=/dev/hda1
  1. Cat recombines contents of the compressed and split image files to stdout, in order.
  2. Results are piped through gzip for decompression.
  3. And are then written to the first partition of the hard drive with dd.

建立空白映像檔

To create an empty disk image, to be used as the disk for an emulator for example, one can get data from /dev/zero. To create a 10mb image:

$ dd if=/dev/zero of=myimage bs=1024 count=10240

A clever alternative is:

$ dd of=myimage bs=1024 count=0 seek=10240

Here we don’t write anything, not even zeroes, we just seek 10mb into the file and close it. The result is a sparse file that is implicitly full of 10mb of zeroes, but that takes no disk space. ls -l will report 10mb, while du and df will report 0. When the file is written to, either as an emulator disk or a loopback device, Linux will allocate disk space for the data. ls will still show 10mb, while du will gradually approach 10mb.

For swap images, where it’s more important to reserve the data than to save disk space, a non-sparse file is better.

Jargon File Entry

This is what the Jargon File has to say about dd:

[Unix: from IBM JCL] Equivalent to cat or BLT. Originally the name of a Unix copy command with special options suitable for block-oriented devices; it was often used in heavy-handed system maintenance, as in “Let’s dd the root partition onto a tape, then use the boot PROM to load it back on to a new disk”. The Unix dd(1) was designed with a weird, distinctly non-Unixy keyword option syntax reminiscent of IBM System/360 JCL (which had an elaborate DD “Dataset Definition” specification for I/O devices); though the command filled a need, the interface design was clearly a prank. The jargon usage is now very rare outside Unix sites and now nearly obsolete even there, as dd(1) has been deprecated for a long time (though it has no exact replacement). The term has been displaced by BLT or simple English “copy”.

Although deprecated, dd is still widely in use on many systems.

See also

External links

This is the most comprehensive documentation and example sheet for one of the most useful, and least understood linux commands, called “dd”. This command has been part of UNIX since the 1970’s. It is a bitstream duplicator for copying data, but can use input or output pipes to another command.

Template:Jargon File/Attribution

http://www.linuxquestions.org/linux/answers/Applications_GUI_Multimedia/How_To_Do_Eveything_With_DD