How do I clone a failing hard drive to a new hard drive using ubuntu

Caution: It is highly advised you run ddrescue from a live USB or similar, and not on the damaged hard drive. Using the damaged hard drive any more than absolutely necessary may cause more (and worse!) data loss.

First off, you're going to enable the universe repository:

sudo add-apt-repository universe

On Ubuntu 18.04 (and later, assumed for this guide), this will automatically update your package repositories as well.

Once you have universe enabled, we can go ahead and install ddrescue:

sudo apt install gddrescue

Next, find your partitions/disk data. This is usually achievable through the lsblk and blkid commands. On my (example) system, we will be cloning /dev/sda to /dev/sdb. Once we have our names, we can just run ddrescue:

sudo ddrescue -d -r3  /dev/sda /dev/sdb /path/to/rescue.log

We're going to use the -d option to read the disk directly, and the -r3 to retry a bad block three times before moving on. rescue.log is an (optional) log file used to restart a recovery if it is interrupted. You may store it in-memory, but it is highly recommended to store it to an external (third) USB drive.

Let this run, and hopefully your drive will be restored.

For further options, read the manpage for ddrescue after installing it. You can access it with the following command:

man ddrescue

A full disk ddrescue is doable, but is somewhat frowned upon due to putting unnecessary stress on the disk. If at all possible, try to only recover the minimum amount of data necessary - a single partition, for example. In order to do this, you'll need to create a partition on your target drive first.


I suggest that you use `ddrescue',

sudo add-apt-repository universe
sudo apt update
sudo apt install gddrescue

which is a tool, that can clone in two steps,

  • a quick step to clone the sectors, that are good (and easy to read)
  • a slow step to clone the sectors, that are difficult to read, but may still be readable, if trying slowly and repeatedly.

When ddrescueis installed, there is a very good tutuorial at

info ddrescue

Please read it carefully before starting the work. You can probably work along the instructions of one of the examples.

Example 1: Fully automatic rescue of a whole disc with two ext2 partitions in /dev/sda to /dev/sdb. Note: you don't need to partition /dev/sdb beforehand, but if the partition table on /dev/sda is damaged, you'll need to recreate it somehow on /dev/sdb.

 ddrescue -f -r3 /dev/sda /dev/sdb mapfile
 fdisk /dev/sdb
 e2fsck -v -f /dev/sdb1
 e2fsck -v -f /dev/sdb2

Example 2: Rescue an ext2 partition in /dev/sda2 to /dev/sdb2. Note: you need to create the sdb2 partition with fdisk first. sdb2 should be of appropriate type and size.

 ddrescue -f -n /dev/sda2 /dev/sdb2 mapfile
 ddrescue -d -f -r3 /dev/sda2 /dev/sdb2 mapfile
 e2fsck -v -f /dev/sdb2
 mount -t ext2 -o ro /dev/sdb2 /mnt
   (read rescued files from /mnt)

Example 3: While rescuing the whole drive /dev/sda to /dev/sdb, /dev/sda freezes up at position 12345678.

 ddrescue -f /dev/sda /dev/sdb mapfile       <-- /dev/sda freezes here
   (restart /dev/sda or reboot computer)
   (restart copy at a safe distance from the troubled sector)
 ddrescue -f -i 12350000 /dev/sda /dev/sdb mapfile
   (then copy backwards down to the troubled sector)
 ddrescue -f -R /dev/sda /dev/sdb mapfile

...


  1. Do not use the drive (more than what is necessary for the cloning).

  2. Boot from another drive, for example an external drive, typically a USB pendrive (a persistent live drive made by mkusb is a good candidate, and many linux repair tools serve the purpose, actually an Ubuntu live-only drive can do it, but if you install ddrescue, it will not survive a reboot).

  3. Have a third drive ready as the target drive for the cloning. This drive must be at least as big as the original failing source drive (not one single byte smaller), and should have the same size of physical clusters. Check that with parted,

    sudo parted /dev/sdx u b p
    

    where x is the drive letter, for example a and b, so in this example

    sudo parted /dev/sda u b p
    
    sudo parted /dev/sdb u b p
    
  4. It is important to write the mapfile to a persistent location, because you want to reuse it from the first step to the next step.

  5. If the target drive with the cloned copy has a different size than the source drive (the failing one), and there is a GUID partition table, GPT, you have to repair the backup partition table with gdisk or easier with gpt-fix. This is not necessary if there is an MSDOS partition table.

See also the following link,

Repair the partition table and file system of a pendrive - Scroll down to

Advanced repair of a partition table, file system and/or recovery of files

If you still have problems, you can use other methods for repair or recovery of the files, that are more powerful but also more risky. If the data are valuable it is a very good idea to make a cloned copy to a card or pendrive of at least the same size, and try to repair the file system and/or recover files from the cloned copy.

Particularly if you suspect bad sectors (a hardware damage on the drive), it is a good idea to clone with ddrescue.