Clone Disk Drives with Ubuntu. Make an Exact Copy of Your Hard Drive.

Before I start, I would like to clarify that this step by step tutorial applies not only to duplicating hard drives that have Linux OS on them. You can clone pretty much any drive. What is on the hard disk is irrelevant; it could be Windows, Mac OS, Linux, just data, etc. There have to be just a few basic things in place:

  1. The target drive should be the same size or bigger than the source disk drive.
  2. Have a Linux Live CD or a Linux bootable USB drive or some other way of booting into Linux (we will be using Ubuntu’s Live CD for this tutorial).
  3. Access to the internet.
  4. There is a presumption that you know how to install a hard drive.

Making an exact copy of a hard drive (or any drive for that matter – CD, DVD, USB, etc.) is very easy and quick with Linux. One of the most popular commands on Linux to do this is dd. It is a very powerful utility that was originally developed for the UNIX operating system and is now default on every Linux distribution. It does a bit for bit copy of the data and it does not care about cylinders, partitions or files. Here is an example of a dd command that would make an exact copy of one disk to another:

dd if=/dev/sda of=/dev/sdb bs=64k

The bs option specifies the block size and it could be omitted, but it would speed up the process since the default block size is only 512 bits. dd is very effective and powerful command but it is not very suitable when you are trying to make a copy of a failing or failed disk. dd is not designed to read and recover bad sectors.

There are a number of other open source programs developed since dd (dd variants) that would address situations where there might be some bad drive sectors and they perform faster and more efficient than dd. Some of those are: dd_rescue, dd_rhelp and GNU ddrescue. GNU ddrescue is the one that I would recommend using if you want to clone a drive. It works both for a perfectly good drives that you would like to clone and for failed drives that you would want to recover data from.

Install the new drive.

The new drive should be of the same or bigger size. You might have to get the BIOS to recognize the new disk; in most cases that is not necessary. After you have put the drive in, boot into Linux from another device. An Ubuntu Live CD would be perfect for that. You can download an ISO image from here.

Now you have to find out what the drives’ logical names are. Open up a terminal window: Accessories -> Terminal or Alt + F2, then type in gnome-terminal and hit Enter.
In the terminal window type sudo lshw -C disk:


sudo lshw -C disk
  *-disk:0
       description: ATA Disk
       product: WDC WD400BB-75FJ
       vendor: Western Digital
       physical id: 0
       bus info: scsi@0:0.0.0
       logical name: /dev/sda
       version: 14.0
       serial: WD-WMAJA3488275
       size: 37GiB (40GB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=5 signature=000b280f
  *-disk:1
       description: ATA Disk
       product: ST340016A
       vendor: Seagate
       physical id: 1
       bus info: scsi@0:0.1.0
       logical name: /dev/sdb
       version: 3.75
       serial: 3HS63J2C
       size: 37GiB (40GB)
       capabilities: partitioned partitioned:dos
       configuration: ansiversion=5 signature=000b280f
  *-cdrom:0
       description: SCSI CD-ROM
       physical id: 2
       bus info: scsi@1:0.0.0
       logical name: /dev/cdrom
       logical name: /dev/scd0
       logical name: /dev/sr0
       capabilities: audio
       configuration: status=nodisc

In my case I have two disks- disk:0 and disk:1. The logical name of disk:0 is /dev/sda and the one for the second disk is /dev/sdb. Make a note of that. In your case that might be different. Identify which drive will be the source and which one the copy. There are 2 things in the above output that will help you do that- the product and the size. You can also use the command sudo fdisk -l. It will show you the hard drives and their partitions.

Prepare the target drive.

Now that you have identified the target drive you need to put an initial partition on it. In the terminal window you have opened execute:

cfdisk -z /dev/sdb

The cfdisk program will start, then type W and then yes to confirm. This is simple enough but you could also use the GParted program that comes with Ubuntu to do the same.

Install the GNU ddrescue program

Before you can install ddrescue you need to enable the Universe Software Repository. Go to System -> Administration -> Software Sources and then check the box next to “Community-maintained Open Source software (universe)“. Close the window. It will ask you to whether you want to refresh the list of software- go ahead and agree to that. After it finishes you can install ddrescue by running this in the terminal window:

sudo apt-get install gddrescue

Clone the disk.

Now you are ready to clone the drive by executing ddrescue. Specify the source disk first and then the target disk. You can use the -v option to be able to see the progress of the operation:

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

Make sure you get the order of the drives right or you could overlay the old drive with the new drive and loose all the data!

Depending on the size of your source drive this operation could take a couple of hours or even more. Once it finishes the new drive will be an exact copy of the old one. You can run a quick check on the file systems of the new drive:

e2fsck -fp /dev/sdb1

If the new drive is bigger than the old one you need to extend the partition(s) on it or create another one to make use of the rest of the space. The GParted program that comes with Ubuntu is ideal for this.

Once you are done, remove the old drive and boot from the new disk.

Credit: Ubuntu Kung Fu. Published article from the book.

Saturday, November 7th, 2009 Linux, Ubuntu

24 Comments to Clone Disk Drives with Ubuntu. Make an Exact Copy of Your Hard Drive.

  1. Great article, thank you! It appears you don’t even need to set up the partitions – just run the

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

    boom! perfect copy. Well… b..ooo..oo..(several hours later)m

  2. Mike on November 17th, 2009
  3. Thanks for the instructions, I’m working through them now. Fingers crossed…

    Anyhow, just dropping you a note to say I don’t know whether you do or don’t need to create the partition first, but if you choose to, I found that sudo cfdisk was required, otherwise I got an error ‘cannot open disk’.

    Thanks buddy.

  4. luc on February 10th, 2010
  5. Great Instructions on cloning!! I hate having to setup someone else’s box… so much easier to just clone your own rig so you already have it configured and ready to go!

    I have done it now successfully twice going from same size to same size USB flash drives.

    I just tried to clone a 4GB to an 8GB usb flash drive and then tried to use gparted to resize the partition… Now it won’t boot? Did I skip a step?

    Here’s what I did:
    I completed the clone process using ddrescue running on a separate ubuntu 9.10 box. Once ddrescue completed i then rebooted from the 8GB and it successfully booted into Ubuntu with all of my settings and preferences (of course with roughly 4GB now showing as “unallocated” in GParted) I then rebooted the computer from the original 4GB stick, once connected I then plugged in the 8gb stick to an available USB port. It was recognized by the system so I unmounted it before running GParted. Then finally I went into GParted and chose to resize the partition on the 8GB stick. After all this work, when I reboot to the 8GB stick i get a “Boot error” message on a blank black screen with a cursor blinking underneath it. What am I missing?

  6. MGG on February 13th, 2010
  7. In fact, am I wrong to assume that I should be resizing the live cd partition? Am I only limited to creating a second partition for data?

    I want to be able to give these to my wife and kids without having to explain why all the photos, music, documents aren’t in their system folders, but rather on a second drive (partition), etc, etc…

    resizing the drive in the future is important to me also as the price on 16GB and 32GB or larger drives come down in price in the future.

    Thanks again for your help!

  8. MGG on February 14th, 2010
  9. Been playing with unetbootin for windows and having a similar problem creating persistent drives where I can save my settings and preferences for future live sessions.

    Just wasted a bunch of time setting up Moblin and EasySUSE on separate drives only to find out that they couldn’t save any of my settings… Half the drive is “unallocated” and when they do boot up they seem to still think they are “live cd’s” rather than usb flash drives.

    Even still, unetbootin would only create default installations from the original source image files…

    Is there a way to just create my own disk image of MY current 4GB USB bootable drive running Ubuntu 9.10 with the CompizFusion all dialed in, etc, and then just burn those images to new USB sticks in the future letting me pick right up where I last left off (all network configs, installed packages and plug-ins still intact)?

    Am I best off doing something like that under Linux, Windows or Mac?

    This looks interesting:
    http://usb-image-tool.en.softonic.com/

    Anyone have any experiences positive or negative? Could be malware or spyware for all i know? What do you use?

    I came really close to pulling it off recently with this:
    https://wiki.ubuntu.com/Win32DiskImager

    but I still wasn’t able to make the new usb stick bootable…

    One of you guys is about to open the flood gates for me with some small thing that I am overlooking, or some small piece missing from my GParted puzzle!

    Thanks for the help!

    I really want to try Moblin, Fedora and EasySUSE – but if I can’t make a persistent drive where it saves the settings in between live sessions then they are useless to me!

  10. MGG on February 14th, 2010
  11. MGG on February 14th, 2010
  12. Hi,

    Thanks for these instructions. Just one remark: I’ve found that I had to reboot after ddrescue and before ‘e2fsck -fp /dev/sdb1′. No big deal, but your readers might get an error message when trying to check (which could scare the **** out of them ;-)

    Regards,
    Tom.

  13. Tom on June 17th, 2010
  14. Hi guys, this is the best and easiest way to clone your drive. I have made a clone in a couple of hours.
    I am on Ubuntu 8.04
    Thanks for the tutorial.

  15. Richard on August 9th, 2010
  16. Hi guys,

    I will try that tonight. I have to clone a USB stick plugged to my IPBOX9000HD with an Enigma distribution on it. Let’s see if it works.

    Just a question. As a perfect clone, my boot loader should not be lost, right ?

    Thanks.

  17. ranskalex on September 2nd, 2010
  18. @ ranskalex,

    Yes, that is correct. The boot loader will not be lost.

  19. dimitar on September 2nd, 2010
  20. THIS IS ANOTHER SOLUTION to COPYING A BOOTCAMP PARTITION.
    For example you have a 2TB drive Mac 500gb Windows Bootcamp 1.5TB (HD1)
    You wish to replace this with a new 2TB Drive. (HD2)
    Format the new drive HD1 journaled.
    Carbon copy your Mac partition from HD1 to HD2.
    Make a new Bootcamp partition whatever size you want but large enough to conatin all your windows files and data.
    Install Windows, Apple Drivers, and updates as for a NEW installation.
    MAKE SURE you keep the same USER NAME and COMPUTER NAME as your current Bootcamp.
    Now Boot into OSX in either drive
    Zip your working Windows folder on HD1.
    On your new Bootcamp partition (HD2) prefix the following with z
    “Program Files” “Program Files (x86)” “ProgramData” “Recovery” “System Volume Information”
    Copy these folders from HD1 to the new Bootcamp drive
    Copy windows.zip to the new drive and unzip it.
    rename your new Bootcamp user profile by prefixing “old” this is your User Name folder in “Users” HD2. Do this even though it has the same name as your old one. Copy your old user profile (within users) to the new users folder.
    Copy any other non-system folder eg Documents etc from the old to the new Bootcamp drive and any folders that your working windows accesses..
    DO NOT OVERWRITE any other system files such as bootmgr, Intel etc

    REBOOT and it will work.

  21. Peter on December 23rd, 2010
  22. Great Tutorial….

    but i have a questions, if i clone one of few partitions…
    example from disk 1 part 2 /dev/sda2 to /dev/sdb1
    how to get it work? cause it didn’t clonethe boot sector also…
    i lost the website address which explained about this….
    i have done dd before… but i cloned the whole disk just like ur tutorial….
    any idea to get this work?

  23. h4uw1n3 on February 17th, 2011
  24. Great tutorial,

    I have just upgrade the HDD of my Synology Ds106e with your solution.

    I work fine.

    Many thanks.

    Regards.

    Neo.

  25. Neo on February 20th, 2011
  26. [...] be able to view the file system.  The command for this is: dd if=/dev/sda of=/dev/sdb bs=64k Clone disk drives Posted in [...]

  27. Clone by dd command | Carson City Computer Corps Blog - Just trying to establish some communications here on March 15th, 2011
  28. Hey, I’m currently getting ” Unable to locate package gddrescue” every time I attempt to download the rescue package, any ideas?

    Running: Ubuntu Live CD

    Thanks

  29. Steven West on October 6th, 2011
  30. @Steven,

    Most likely you do not have the right repositories enabled that contain the gddrescue package. If my memory serves me right, the package is part of the Universe repository. Here is a quick guide about repositories and how to enable them:

    https://help.ubuntu.com/community/Repositories/CommandLine

  31. dimitar on October 6th, 2011
  32. Oh, cheers for that, I’ve currently got it now, and I’ve used the rescue code, I’m attempting to copy a entire drive to an external usb one, taking quite a long time, and I’m seeing there’s a few errors while I’m doing this, is this to be expected?

    Thanks for your speedy reply.

  33. Steven West on October 6th, 2011
  34. @Steven,

    Yes, your drive could have errors. So it is not unusual.

  35. dimitar on October 6th, 2011
  36. Thanks,

    I’ve left it on for two hours and have only saved 465MB out of 60GB, I assume this is an over night job lol…

  37. Steven West on October 7th, 2011
  38. Thank you for the article. Started the clone using ddrescue.

    Also, running sudo cfdisk only started the console and I had to use the interface to create a partition and write to it. (the key ‘w’ didn’t work for me.)

    And while running ddrescue, it prompted me to use the –force parameter as it had a partition.

    Just wanted to inform you.

    Thank you again for the article.

  39. tag on March 31st, 2012
  40. I was able to clone the hard drives. Thank you.

    The ‘w’ key worked. It was CAPS W (shift + w).

    Thank you.

  41. tag on April 2nd, 2012
  42. I know this post isn’t new, still hoping for help.

    Im running 12.04 LTS hoping to clone a failed/failing 160gb mac hard drive. I think Im ready to go, except where I read I need to prepare the target drive by putting an initial partition on it.

    I was hoping to use a 750gb external drive that I already have. Thing is, I currently use it for extra storage and am afraid I’ll lose all of that data if I proceed.

    Is that so? Am I missing something?

  43. Aron on May 12th, 2012
  44. [...] 8. I have read also those two tutorials here http://www.ghacks.net/2010/08/01/clo…with-ddrescue/ http://dimitar.me/clone-disk-drives-with-ubuntu/ but I can not see why it failed so much. Any help now? [...]

  45. Hard disk failing?? - Page 3 on March 23rd, 2013
  46. I give up – downloaded Ubuntu Live to USB stick, running off USB, cannot get around “unable to locate package gddrescue”. I don’t have administrative access to the live version on the USB stick to add the repositories. I despise Linux, because command line instructions ALWAYS result in an error not mentioned by the instructions I find in google. Will just go and pay the $50 to buy a Windows XP version of a disk cloner I looked at earlier. Considering I have already battled this for 4 hours, it is well worth it. Linux is horrible, no wonder it will never be mainstream.

  47. Heiko on November 17th, 2013

Leave a comment

*

Search

 

Archive

August 2014
M T W T F S S
« Jul    
 123
45678910
11121314151617
18192021222324
25262728293031

Other