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:
- The target drive should be the same size or bigger than the source disk drive.
- 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).
- Access to the internet.
- 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.
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
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.
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?
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!
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!
getting warmer:
http://www.pendrivelinux.com/casper-rw-creator-make-a-persistent-file-from-windows/
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.
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.
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.
@ ranskalex,
Yes, that is correct. The boot loader will not be lost.
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.
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?
Great tutorial,
I have just upgrade the HDD of my Synology Ds106e with your solution.
I work fine.
Many thanks.
Regards.
Neo.
Pingback:Clone by dd command | Carson City Computer Corps Blog - Just trying to establish some communications here
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
@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
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.
@Steven,
Yes, your drive could have errors. So it is not unusual.
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…
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.
I was able to clone the hard drives. Thank you.
The ‘w’ key worked. It was CAPS W (shift + w).
Thank you.
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?
Pingback:Hard disk failing?? - Page 3
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.
Pingback:save the hard drive | Research Journal
Hi, thanks for your guide. It’s been really helpful so far.
However, after 6 hours of working my disks ddrescue sits at “Splitting failed blocks…” with more than an hour since the last successful read. A suggestion is to add a logfile in the example command above, to be able to retry/resume with possibly different parameters.
From the gnu ddrescue manual:
Always use a logfile unless you know you won’t need it. Without a logfile, ddrescue can’t resume a rescue, only reinitiate it.
Kind regards, Robert
For all those wanting to install ddrescue, but getting the message “unable to locate package gddrescue”. Try these commands before calling “sudo apt-get install gddrescue”:
sudo add-apt-repository “deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) universe”
sudo apt-get update
Source:
http://askubuntu.com/questions/148638/how-do-i-enable-the-universe-repository
Great article and comments as well. For those having issues with not finding gddrescue: whenever you first run a system (live disc/usb or otherwise), you *always* have to do an repository update, otherwise you will may be unable to locate certain packages. So in Ubuntu or Debian: `sudo apt-get update` or a ‘Refresh’ in a package management tool like Synaptic.
gddrescue is available in the default Ubuntu repositories.
And for those complaining about not having administrative access to the live system…. Really??? You cant do a simple search for the default root password for the particular live system you have booted into???? And, good luck with finding a reliable windows live-boot system to use….
Anyway, other than running into the initial msg about needing to use the –force option (see ++note below), ddrescue has been a dream. Where other tools failed, ddrescue has excelled without even blinking (or more properly, gddrescue). I’ve wasted so much time on tools such as SeaTools and Clonezilla — even with the repair option that just gave up (even my usual goto paragon disc backup suite). ddrescue indicated that were only a few error bytes on the source disc which it has addressed (previously preventing a disc backup/copy).
I ended up using the following commands:
Live-booted into a live system such as makululinux.
plugged in a usb disc to save the log file to (for persistent storage and to avoid using the source and target disks).
cd to a directory on the usb stick (cd /media/makulu/KNOPPIX/ddrescuelogs)
and run the following:
1) Copy the error free areas first:
sudo ddrescue -nv –force /dev/sdc /dev/sda rescue.log
Take CAREFUL NOTE — the ordering of my devices is reversed from the norm. Do not copy and paste. Use gparted or like tool to determine what your has named your devices. As mentioned in this article:
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 -nv –force /dev/sdX[SOURCE] /dev/sdY[TARGET] rescue.log
2) Now go back and attempt to recover any bad sectors (again, make sure you are in the usb log save directory used above).
sudo ddrescue -r 1 /dev/sdc /dev/sda rescue.log
** Again, notice my device ordering above is different from the norm per previous comment — do not copy and paste (and, I ran gparted to determine what names the system had assigned to my devices).
The source disk in my case was ‘sdc’ and the target is the internal disc ‘sda’ since I had already installed the blank disc drive into my computer (always be sure to triple check yourself when using powerful tools like dd or ddrescue as you can end up wiping out the wrong disc by reversing target and destination).
———————————
++Previously running the command above without the –force option returned:
makulu@makulu:/media/makulu/KNOPPIX/ddrescue$ sudo ddrescue -nv /dev/sdc /dev/sda rescue.log
>>>
ddrescue: Output file exists and is not a regular file.
ddrescue: Use ‘–force’ if you really want to overwrite it, but be
aware that all existing data in the output file will be lost.
Try ‘ddrescue –help’ for more information.
<<<
Fearing I possibly had made a mistake in the ordering of my source and target devices, I once again checked the ordering in my command to be sure I wasn't about to do irreversible damage to the drive I was trying to recover. Nope, just needed to use the –force option. Aside, I think the error message should be a little clearer as it seems misleading and vague.
———————————————-
https://help.ubuntu.com/community/DataRecovery
Returning to my previous post, I ended up not needing to run ddrescue again with the -r option. ddrescue worked excellently. I wish I wiuld have discovered it before wasting time trying to use Clonezilla and the other tools (I mean, really. What kind of lame correction algorithm are they using? – it couldnt even handle 4000 bytes of bad data?)…
I have read through the procedure and all of the comments. I am trying to clone my failing Windows 8.1 2 TB MBR formatted HD to a new 4 TB HD that I have already formatted to GPT to make an attempt to rescue my setup. Luckily, I have the data saved over, so I have not lost data, I am just trying to avoid re-installing Windows and all of my programs/apps. If I were to use gddrescue, I assume that it will clone not only the data on the failing drive (obviously minus what data cannot be rescued from the failing areas) but also the MBR formatting, correct? I already found out the hard way that MBR formatted drives over 2 TB are a pain to work with in Windows (I could not find a way to make Disk Manager to format the “missing” 2 TB into another partition). Can Gparted convert an MBR drive over to GPT once I get my failing drive cloned? Is there a setting within gddrescue that I can use to keep the target drive’s formatting while still cloning everything else?
Thnx.
Follow-up. For clarity, the data I saved over to my external HD’s, not the HD that I am trying to clone to.
After giving it a try, it left the GPT formatting alone but still attempted to clone the original drive’s data and partition sizes. Because something in the Windows or boot data got damaged on the original drive, I still was not able to clone the original drive onto the new one to be recoverable in the sense that Windows would be able to repair the bad data to allow the new drive to boot and work with the existing installed apps as if nothing had happened. I ended up having to re-install windows on the new drive and the data that was cloned ended up in the Windows.old folder created after the re-install.
Figured I’d post this for others who may have the question about using gddresecue to clone a MBR disk onto a GPT disk.
great article!
please make a small update
sudo ddrescue -v /dev/sda /dev/sdb bs=64k
you mention the bs=64k above but not on the actual command. Unfortunately for me, I forgot to put it in before I hit enter.
Also,I had to do the following before I could get gddrescue
sudo add-apt-repository “deb http://archeive.ubuntu.com/ubuntu $(lsb_release -sc) main universe restricted multiverse
sudo apt-get update
sudo apt-get install gddrescue
Hi!
I have two identical hard disks in my computer
Active one is /dev/sda
I prepared and cloned (sda -> sdb) disk without erros
I tried to mount this cloned disk but had no luck!
Failed to mount ‘/dev/sdb’: Invalid argument
The device ‘/dev/sdb’ doesn’t seem to have a valid NTFS.
NTFS signature is missing.
Failed to mount ‘/dev/sdb1’: Invalid argument
The device ‘/dev/sdb1’ doesn’t seem to have a valid NTFS.
mount: /dev/sdb2 is not a block device
mount: /dev/sdb3 is not a block device
???
I read carefully and followed instructions. Everything worked, with a few added “sudo.” Thank you very much!! A few unresolved errors remained after ddrescue. e2fsck gave errors I didn’t understand, but I unplugged the old drive and booted to the replacement drive.
I first got a scary blue screen, but Windows re-booted and started an automatic repair that worked in about 2 minutes. So, my old drive had a damaged portion, but the new rescued drive could be fixed by Windows built-in fixes. It’s pretty amazing that it was this easy.
Will this method work if the drive being cloned is the active Ubuntu boot drive? Or does this procedure only work if booted into Ubuntu from a live CD or USB drive? More specifically, I have an old netbook with a 120GB hard drive running Ubuntu and I want to migrate to a new 120GB SSD. Can I connect the SSD via SATA/USB adapter and then use the ddrescue command to clone to HDD to the SSD while booted into Ubuntu from the existing HDD? Or should I create a Live Ubuntu USB drive, boot from that and then clone the HDD to the SSD?