用DD命令制作硬盤鏡像
本文參考http://serverfault.com/questions/4906/using-dd-for-disk-cloning寫出,轉載時請說明出處。
以下的說明都是針對備份整個硬盤,而不是備份某一個分區。
一、用DD命令制作硬盤鏡像的特點
(1)在制作鏡像時,不能對需要進行備份的硬盤經常寫操作,可以只讀掛載或者不掛載。
(2)在制作鏡像時,MBR、硬盤的分區表、bootloader也會被備份。
(3)生成的鏡像文件用於恢復時,目標硬盤的容量必須等於或大於源硬盤的容量。
(4)使用硬盤鏡像完成恢復后,由於目標硬盤的分區表跟源硬盤的分區表是一樣的,所以會造成目標硬盤的空間浪費。這個問題可以通過使用硬盤分區大小調整工具解決。
(5)在dd生成或恢復鏡像時,默認沒有顯示進度,但這個問題可以解決。解決方法請看下文。
二、備份和還原操作
(1)制作硬盤sdb的鏡像文件sdb_backup.img:
$dd if=/dev/sdb of=~/sdb_backup.imgbs=32M
注:bs即blocksize,bs根根系統的內存大小和硬盤讀寫速度而設定
(2)將硬盤sdb的內容直接克隆到硬盤sdc中(要保證sdc的容量等於或大於sdb的容量):
$dd if=/dev/sda of=/dev/sdbbs=32M
(3)需要備份的硬盤可能存在大量的空白區域(未用於存儲數據的區域),如果用壓縮工具壓縮生成的鏡像,可大大減小鏡像的大小。
在制作硬盤sdb的鏡像文件時就進行壓縮:
$dd if=/dev/sdb| gzip -c > ~/sdb_backup.img.gz
將備份的鏡像恢復到硬盤sdc中
$gunzip -c ~/sdb_backup.img.gz| dd of=/dev/sdc
(5)只備份硬盤的MBR
$dd if=/dev/sdb of=~/MBR_backup bs=512 count=1
(6)當使用dd進行鏡像備份時,如dd發現某個sector(扇區)錯誤,默認會停止備份操作。這時可以 "conv=noerror,sync" to ensure that it doesn't stop whenit encounters an error, and fills in the missing sector(s) with null bytes.This is usually the first step I take if trying to recover from a failed orfailing disk -- get a copy before doing any recovery attempts, and then dorecovery on the good (cloned) disk. I leave it to the recovery tool to copewith any blank sectors that couldn't be copied.
gunzip -c ~/sdb_backup.img.gz| dd of=/dev/sdc conv=noerror,sync
注意:
If you have a disk with bad sectors,you really should be using 'ddrescue' instead of dd. It's much more efficient,and has a much better chance of recovering more data. (Don't get it confusedwith dd_rescue, which is not as good)
If the source drive is damaged at all,you'll have more luck usingdd_rhelp withdd_rescue (my personal preference) or GNUddrescue.
The reason behind this is that, on readerrors, dd keeps trying and trying and trying - potentially waiting for along time for timeouts to occur.dd_rescue doessmart things like reading up to an error, then picking a spot further on on thedisk and reading backwards to the last error, anddd_rhelp isbasically a dd_rescue session manager - cleverly starting and resumingdd_rescue runsto make it quicker again.
The end result of dd_rhelp ismaximum data recovered in minimum time. If you leavedd_rhelprunning, in the end it does the exact same job asdd inthe same time. However, ifdd encountered read errors at byte 100 of your 100Gb disk, you'd haveto wait a long time to recover the other 9,999,900 bytes*, whereasdd_rhelp+dd_rescue wouldrecover the bulk of the data much faster.
(5)顯示制作操作或恢復操作的進度
參考A
Youcan follow the progression of the operation with :
$ddif=/dev/sda of=/dev/sdb & pid=$!
$kill-USR1 $pid; sleep 1; kill $pid
參考B
Youcan get a dd process running in the background to report status by sending it asignal with the kill command, e.g.:
$ddif=/dev/hdb of=/image.img &
$kill -SIGUSR11234
#這里假設1234為備份進程號
參考C
The man page says: Sending a USR1 signal to a running ‘dd’process makes it print I/O statistics to standard error and then resumecopying.
I use this feature regularly.
Thisis kind of a cheap hack, but it's a quick and dirty way to monitor your DDprocess.
Runyour dd command. Open a new shell and do a ps awx to find your dd process' PID.Now in the new shell type watch -n 10 kill -USR1 {pid of your DD process}
Thiswill do nothing in the watch output window, but back in the original DD shell,DD will start outputting status reports every 10 seconds. You can change the -n10 in the watch command to any other time frame of course.
OS X doesn't have watch available and -USR1 kills dd. The following command works though: while [ true ]; do killall -INFO dd; sleep 30; done
————————————————
版權聲明:本文為CSDN博主「ColorPaper」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/koupoo/article/details/8029514