Categories
FreeBSD/Unix

Mirroring failed hard drive with dd

As long as the hard drive is rotating and it is identified by BIOS at boot time, one can make a copy out of it using dd.

Creating a hard drive image:

dd bs=512 if=/dev/adx of=drive.dmg conv=noerror,sync

if=file: Specifies the input path. Standard input is the default.
of=file Specifies the output path. Standard output is the default. If the seek=expr conversion is not also specified, the output file will be truncated before the copy begins, unless conv=notrunc is specified. If seek=expr is specified, but conv=notrunc is not, the effect of the copy will be to preserve the blocks in the output file over which dd seeks, but no other portion of the output file will be preserved. (If the size of the seek plus the size of the input file is less than the previous size of the output file, the output file is shortened by the copy.)

bs=n: Sets both input and output block sizes to n bytes, superseding ibs= and obs=. If no conversion other than sync, noerror, and notrunc is specified, each input block is copied to the output as a single block without aggregating short blocks.

conv=value[,value. . . ]: Where values are comma-separated symbols

noerror: Does not stop processing on an input error. When an input error occurs, a diagnostic message is written on standard error, followed by the current input and output block counts in the same format as used at completion. If the sync conversion is specified, the missing input is replaced with null bytes and processed normally. Otherwise, the input block will be omitted from the output.

sync: Pads every input block to the size of the ibs= buffer, appending null bytes. (If either block or unblock is also specified, appends SPACE characters, rather than null bytes.)

More Examples:

dd bs=4k if=/dev/ad1 of=/dev/ada2 conv=noerror,sync

This command is used often to create a backup of a drive (/dev/ad1) directly to another hard drive (/dev/ad2). The option bs=4k is used to specify the block size used in the copy. The default for the dd command is 512 bytes: use of this small block size can result in significantly slower copying. However, the tradeoff with larger block sizes is that when an error is encountered, the remainder of the block is filled with zero-bytes. So if you increase your block size when copying a failing device, you lose more data but also spend less time trying to read broken sectors.

If you’re limited on local space you can use a pipe to gzip instead of the of= option.

dd bs=4k if=/dev/ad1 conv=noerror,sync | gzip -9 > backup.dmg.gz

Here dd is making an image of the hard drive, and piping it through the gzip compression program. The compressed image is then placed in a file on a seperate drive.