用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_rhelp
running, 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 |