Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I currently have a failing hard drive which is the only hard drive in the machine. I have just bought a new hard drive to replace it, and my plan is to copy the contents of the old drive onto the new one, and then replace the old drive in the machine with the new one.

I presumably can't just copy the whole directory structure (or can I)? What do I need to do to manage this, assuming it is possible? Is there a utility to do this for me? (The old drive is hopefully good for a few more hours.)

(I hope by this means to keep all the software and configuration files as they are, to avoid having to re-install everything. Can that be done?)

share|improve this question
    
If the disk doesn't give read errors, you can copy the whole directory structure when you use the correct cp parameters to keep all attributes (ownership, permissions, times, ACLs, etc.), but then you have to make the disk bootable afterwards. –  JanC Jun 6 '11 at 15:44
    
The problem using cp is not only finding the necessary parameters, but also requiring to create the partition(s), issue cp for each, and setup grub afterwards. dd (or your suggestion on ddrescue) does this all in 1 line –  MestreLion Jun 7 '11 at 17:10
    
@MestreLion: dd is (probably) useless if the new disk is smaller than the old one though, and if the new disk is bigger you'll probably have/want to resize partitions afterwards... Both approaches have pros & cons, and I think Brian will have to decide what is likely to work best for him. –  JanC Jun 9 '11 at 12:40
add comment

3 Answers

up vote 6 down vote accepted

Yes, a 100% exact copy of your HDD is possible, and it would preserve all your files, data and system configurations intact. And, as you've already correctly guessed, simply copying files over won't work. (it can be done, but not worth the trouble of doing it right)

A HD cloner tool is your best bet. There are dozens available.

  • Is the new hard drive larger than the old one?
  • Do you have any Ubuntu bootable CD / USB key?

If so, I recommend the good old dd. Its a one-liner, rudimentary, ancient solution for cloning a HDD. But it works, and its amazingly easy. dd is also pre-installed in Ubuntu.

  • Boot using your Live CD or USB stick
  • Using System > Administration > gparted (Disk Utility also works), take note which is your old and which is your new hard drive (sda, sdb, etc).

enter image description here

  • Checked? Great!
  • Now check them again:

enter image description here

  • Ready?
  • Now Triple check it. I mean it! dd will have no mercy if you use the wrong letters. dd is commonly known as "d isk d estroyer" for a reason!

From now on, Ill assume the old is /dev/sdX and the new is /dev/sdY. Check your actual values! (did I say that already?)

  • Using Terminal, issue the granddaddy of all HDD cloners:

    sudo dd if=/dev/sdX of=/dev/sdY bs=1M
    
  • bs=1M is optional, only to improve performance. The best value depends on your drives/cache/cpu/ram. Commonly used values are 4K, 64K, 1M, 16M. Try for yourself in a 1GB test (add count=256K, 16K, 1K, 64 respectively). For my machine, anything from 4K to 32M leads to very similar results.

  • If you want to monitor the copy, you may try this fancy version:

    sudo su - # neccessary to get correct pid
    dd if=/dev/sdX of=/dev/sdY bs=1M & pid=$!
    while kill -USR1 $pid 2>/dev/null; do sleep 10; done
    

It will output dd's progress every 10 seconds.

  • Now go grab a lunch. Or better, go to sleep. It will take long ;)

Post-copy procedures:

  • Remove the old drive
  • Install the new one in the same connector as the old (so you dont need to reconfigure BIOS)
  • Boot and check that everything is fine
  • Since dd makes an exact copy, partitions will be the same size. If new hdd is much larger than the old one, use gparted to expand partitions to claim the unused space.
share|improve this answer
    
I would only use dd if the disk is mostly full; otherwise you'll be copying mostly unused bytes... –  JanC Jun 6 '11 at 15:36
1  
@JanC it is a one-use-only throw away solution, so the overhead should be tolerable –  mbx Jun 6 '11 at 16:02
1  
@JanC: true, but dd one of the few solutions that copy not only the files, folders and partitions themselves, but also the partition table, MBR code (grub, for instance) and everything else. And its by far the easiest way to do this 100%-exact clone of the entire hard drive –  MestreLion Jun 7 '11 at 8:08
    
well, PartImage is probably easier than dd and can do that too. But I agree that copying the MBR and maybe the gap before the first partition (in case grub or other tools embed code there) separately from a FSArchiver or cp copy of the filesystems requires more knowledge. –  JanC Jun 7 '11 at 16:16
1  
If you use dd, stop and use dc3dd instead. It includes a patch that shows a bunch of stats, notably: throughput (MB/s), percent completed, bytes and/or GB completed. –  djeikyb Aug 30 '11 at 5:17
show 2 more comments

I have used "clonezilla" in the past,i had the same problem as you but my system had xp and ubuntu 9.04 on it,it will clone the drive to a new drive just as long if the new drive is bigger or the same size. this is a few vids on youtube on how to use it. its free and saved my butt a few times.

http://clonezilla.org/

share|improve this answer
add comment

It might be useful to use the System Rescue CD (which can be run from an USB stick too). E.g. when you get read errors you'll want to use the included ddrescue. Note that the applications are not always easy to use (they often only work from the commandline), but they can be very powerful!

share|improve this answer
    
Ive never heard of ddrescue before. Ive googled it, and it sounds very nice! Its as simple as dd, and it also has reading errors handling and logging. Sweet ;) –  MestreLion Jun 7 '11 at 17:14
    
@MestreLion: there are actually 2 tools like that in the Ubuntu repositories, ddrescue (command: dd_rescue) & gddrescue (command: ddrescue). I'm not sure which one is best... –  JanC Jun 9 '11 at 12:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.