The other day I got a call from a customer whose laptop was having some major performance issues.  The hard drive was full of bad sectors and was causing Windows to perform very poorly, if at all.  I got the laptop from them and started down the road of data recovery and hard drive replacement.  Thankfully, I was able to boot the laptop with a Fedora Live CD, mount the NTFS OS partition and recover most of the files in the various user profiles.  I purchased a replacement hard drive that was the same size as the bad one and thought I'd first try to image the old drive to the new one.  This proved to be very slow (because of the 1000s of bad sectors) and never really worked.  The old hard drive had a recovery partition at the tail end of the disk.  Unfortunately, I wasn't able to get Windows to perform well enough to burn recovery media from the recovery partition.  So, I was a little stuck.  I decided I'd try to capture the partition table and the recovery partition off the failing hard drive.  To do this, I used the Fedora Live CD and the 'dd' command. [more]

To capture the partition layout from the first sector of the disk:
dd if=/dev/sda of=partition_layout.img bs=512 count=1

To capture the recovery partition:
dd if=/dev/sda2 of=recovery_partition.img bs=512k conv=noerror

Thankfully, since the recovery partition is put on the hard drive long before it starts to fail, and isn't ever re-written, it didn't have any errors.

After successfully capturing the images, I was able to replace the failing hard drive with the new drive.  I booted the machine again with the Fedora Live CD and restored the images using similar dd commands.

Restore partition layout:
dd if=partition_layout.img of=/dev/sda

After restoring the partition layout, I had to run 'partprobe' to have the OS re-read the partitions on the disk.  Then I restored the recovery partition.
dd if=recovery_partition.img of=/dev/sda2

I used 'fdisk' to set the recovery partition as the "active" partition (so it would boot).
fdisk /dev/sda

After this, I rebooted the machine and the HP recovery process started up and I was able to get the laptop back to its original factory condition.