Clone your Linux disk with ddrescue - gHacks Tech News

Clone your Linux disk with ddrescue

What do you do when you KNOW your hard disk is failing but you're not ready to do a re-install or lose any of that precious data? Obviously the first solution is to always have a backup of your data. This is great, but it doesn't solve the issue of having to re-install the operating system which would then lead to having to install all of your applications to get your PC back where it was.

There is a better, quicker way to do this which is to clone your entire drive onto a new drive. You can do this with a tool like GParted, but there is also a command line tool that will get the job done as well. This tool is ddrescue. Now with ddrescue you will have to have your new disc partitioned (it will not partition for you). In this article I am going to show you how to get your dying Linux drive cloned onto a new drive.

Installation of ddrescue

The first step in this process is to install ddrescue. To do this follow these steps:

  1. Open up a terminal window.
  2. Issue the command sudo apt-get install ddrescue
  3. Issue the command sudo apt-get install gparted

That's it. You will notice I installed gparted as well. This will make the partitioning of the drives much easier for you. You can, of course, partition a drive via the command line, but using a tool like gparted is far easier.

Paritioning your new drive

What you need to do is shut down your machine, install your new drive, and reboot your machine. Once you have your machine booted with the new drive powered up do the following:

  1. Fire up GParted.
  2. Create a partition scheme on the new drive that is identical to the old drive. If there is space remaining on the new drive, that's fine (you can resize the partitions with Gparted later).
  3. Save the new partition scheme and you are ready to clone.
  4. Make note of the precise drive letters assigned to the two drives (you do not want to clone the new drive onto the old drive now do you?).

Cloning the drive

Okay, it's go time. Open up a terminal window. Remember the exact letters of your drives earlier? Let's say your old drive was /dev/sda and your new drive is /dev/sdb. To make the clone you would issue the following command:

sudo ddrescue -v /dev/sda /dev/sdb

Depending upon the size of your disk, this can take some time. But once it is complete you should be ready to boot into that new drive. Go ahead and shut down and then unplug the old drive. Now reboot the machine and it should boot. Once it completes the boot you should  take a precautionary step and check the new disk for errors. Do that with the following command:

e2fsck -fp /dev/sdb1

If it gives you any complaint about the disk not being found issue the command mount without any arguments. This will show you if the drive retained the same letter (sdb). If not, then make the correction in the command above.

Finally, if you need, you can fire up GParted and resize those partitions. You can't actually run GParted on a mounted drive, so what you should do is download and use the GParted Live CD which will allow you to resize the partitions to what you need.

Final thoughts

The paths to getting your Linux disk cloned are many. Some are easier than others, some are more proven. Do you have a different method of cloning a drive? If so, share it with your fellow Ghacks readers.





  • We need your help

    Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

    We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats (video ads) or subscription fees.

    If you like our content, and would like to help, please consider making a contribution:

    Comments

    1. David Legg said on August 2, 2010 at 10:01 am
      Reply

      It would be useful to have the instructions about downloading and using the gparted live cd a bit higher up, nearer the bit where gparted is installed, because most users will need the live CD and not be able to use gparted.

    2. Kevin said on August 2, 2010 at 2:46 pm
      Reply

      I think you can accomplish the same thing without installing any extra apps by simply using the dd command.

      dd if=/dev/sda of=/dev/sdb conv=notrunc,noerror

      replacing the source and destination drive letters as needed. This will clone the entire drive so you don’t have to worry about partitioning.

    3. Dean said on February 25, 2011 at 5:44 pm
      Reply

      As Kevin said, you don’t need gparted to partition on the drive before cloning a simple command like this

      ddrescue -v /dev/sdb /dev/sdc

      would do the job. If you are cloning to a bigger drive, then you can of course use gparted to resize the partitions. This is also a great tool for recovering Windows installations as well. Just make sure you at least run a chkdsk /r

    4. Clement said on January 18, 2017 at 12:55 pm
      Reply

      am trying ddrescue on my valuable 500GB SATA and this is the output I had:
      [email protected]:~$ ddrescue -f -r1 -R /dev/sda /dev/sdc /home/knoppix/Desktop/log2.log
      GNU ddrescue 1.21
      Press Ctrl-C to interrupt
      ipos: 495735 MB, non-trimmed: 4826 MB, current rate: 0 B/s
      opos: 495735 MB, non-scraped: 0 B, average rate: 0 B/s
      non-tried: 495281 MB, errsize: 0 B, run time: 1h 38m 53s
      rescued: 0 B, errors: 0, remaining time: n/a
      percent rescued: 0.00% time since last successful read: 1h 38m 53s
      Copying non-tried blocks… Pass 3 (backwards)

      Here is the logfile:
      # Mapfile. Created by GNU ddrescue version 1.21
      # Command line: ddrescue -f -r1 -R /dev/sda /dev/sdc /home/knoppix/Desktop/log2.log
      # Start time: 2017-01-18 09:34:51
      # Current time: 2017-01-18 11:19:33
      # Copying non-tried blocks… Pass 3 (backwards)
      # current_pos current_status
      0x735B320000 ?
      # pos size status
      0x00000000 0x00010000 *
      0x00010000 0x00010000 ?
      0x00020000 0x00010000 *
      0x00030000 0x00020000 ?
      0x00050000 0x00010000 *
      0x00060000 0x00040000 ?
      0x000A0000 0x00010000 *
      0x000B0000 0x00080000 ?
      0x00130000 0x00010000 *
      0x00140000 0x00100000 ?
      0x00240000 0x00010000 *

      Please is there any hope? If not what can I do? PLEASE HELP

    Leave a Reply