Ubuntu system backup/transfer

How to backup / move your Ubuntu system install to another location.


Before you start, install Gparted - it's a disk partition gui. We need to get some disk IDs later on and this is the simplest way I found to get them.

> sudo apt-get install gparted

It appears in your system/adminsitraton menu.

Disconnect your current install/hdd so it doesn't get messed with, and then connect your new hdd. These will need to co-exist for a while so in this case I put one as primary master and the other as slave. My bios lets me select the boot order but that's probably not the same for all.

Install a fresh install of Ubuntu on the empty hdd. That lets you partition the disk with the gui during install and make it active, set up swap space etc, basically get it set up for how you want once your system is on it.

****EDIT****
You can skip the fresh install if you set up grub manually after the dump is done and you've done the other steps. Follow the guide here and install grub on your destination drive. See guide here
************

Reconnect and boot back into your existing install then from a shell:

> sudo nautilus

open the freshly installed drive (e.g. /media/disk-1) ) and delete everything on it.

So you should be booted on your current system drive with a blank, freshly partitioned new drive, which is where you want to copy your system to.

Then open a shell and cd to your new drive - in this case it was /media/disk-1 so:

> cd /media/disk-1

> sudo dump 0af - / | sudo restore rf -

(basically dump the contents of / (root) to here (/media/disk-1), hence needing to cd to that directory before hand so it dumps the data in the right place.

more info on this.

(Note the double sudo. I found it seems to need you to have sudo'd something else just before so it does it all in same session, since it doesn't work when it asks for two passwords but also doesn't work without the double sudo - ideas??)

So the disk does its thing for a while - it should be rattling away most of the time, I didn't have any long waiting periods.

Once the files are copied we need to change a couple of things on the new disk to make sure the new system boots from this drive, not the current system drive.

Linux seems to use disk UUIDs which means it'll boot from the right disk no matter where it's plugged in whcih is very cool but means we really have to update that stuff or you get a "file not found" error on boot cos grub is looking in the wrong place for the files.

All we're doing is updating the UUID to point to the new disk in a couple of key locations.

Ok so the data's copied so with your sudo nautilus window browse to the /boot/grub folder ON YOUR NEW DISK NOT THE CURRENT SYSTEM DISK (otherwise you won't be able to boot your current install again). So this should be something like:

/media/disk-1/boot/grub

open menu.lst in a text editor and at the same time open Gparted (gui partition editor).

Within menu.lst you should find a bit where it mentions disk UUIDs. It's quite a way down after "default options" and looks a bit like this:

## ## End Default Options ##

title Ubuntu 9.04, kernel 2.6.28-16-generic
uuid 1bc23b74-dff3-438f-9425-d0c2babd78be
kernel /boot/vmlinuz-2.6.28-16-generic root=UUID=1bc23b74-dff3-438f-9425-d0c2babd78be ro quiet splash
initrd /boot/initrd.img-2.6.28-16-generic
quiet

title Ubuntu 9.04, kernel 2.6.28-16-generic (recovery mode)
uuid 1bc23b74-dff3-438f-9425-d0c2babd78be
kernel /boot/vmlinuz-2.6.28-16-generic root=UUID=1bc23b74-dff3-438f-9425-d0c2babd78be ro single
initrd /boot/initrd.img-2.6.28-16-generic

The highlighted bits are the UUIDs which need updating.

So go to gparted and you locate your current partition (the UUID will match the one in menu.lst) or type "sudo blkid" to get the UUIDs from the console. Then find your new one and update the four highlighted bits to match your UUID. Double check the changes and then save and close.

That's the first bit...for the next you need to cd to the /etc folder on your new drive AGAIN, NOT ON THE CURRENT SYSTEM INSTALL. for me it was

/media/disk-1/etc

and then

> sudo gedit fstab

which should look like this:

# /etc/fstab: static file system information.
#
# Use 'vol_id --uuid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#
proc /proc proc defaults 0 0
# / was on /dev/sda1 during installation
UUID=1bc23b74-dff3-438f-9425-d0c2babd78be / ext3 relatime,errors=remount-ro 0 1
# swap was on /dev/sda5 during installation
UUID=6140d51d-dc93-4f09-b38c-87d243c70c55 none swap sw 0 0
/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0

again we're going to replace the highlighted bits. The first with the UUID of your new system disk (same as we just did in menu.lst) and the other to reassign the swapspace to the partition on the new drive (which we set up by doing the default ubuntu install on the disk earlier ;)

So go back to Gparted and find your disk IDs again and paste them in.

Double-check then save and close.

****EDIT****
If you skipped the fresh install this is where you need to set up grub manually. Follow the guide here and install grub on your destination drive. See guide here (you need the bit

Open a commandline and type:

> sudo -s
(enter password when prompted) then enter grub:

> grub

this is it's own little world with commands similar to the normal commandline. Next type the following to find all instances of grub on the drives.

> find /boot/grub/stage1

You'll get a response like "(hd0,1)". If you've been doing the system backup so far and then you should get two entries back, one is your current system whcih wwe don't want to touch, and the other will be the backup which is what we want to fiddle with. In my case I got:

> (hd0,0)
> (hd5,0)

Ok so (hd5,0) is the one I want to install grub on so next I would type:

> root (hd5,0)
> setup (hd5,0)

Obviously use the correct values for your drives. That should be it - you should get a generally successful message. Mine had a couple of non-fatal errors but that didn't seem to affect operation. Then type:

> quit

to exit grub and we're done :)

************

Lastly you probably want to double check that the boot flag is set for the new system drive, otherwise nothing's going to happen when you plug it in. Again go to gParted (system/administration/partition editor) and select your new system disk from the dropdown on the right. Then click the new system partition in the diagram (normally hte first one) and int he list below you should see it highlighted. At the right of that highlighted bar it should say "boot". If it doesn't you need to right-click on the partition and select "manage flags". Click boot (it can take a while to refresh) and OK. And then exit gParted.

--------------------------
edit:
As I went to reboot my machine (I'm doing this as I write it) I realised it might have been prudent to make a note of the disk IDs on a post-it *before* rebooting, since as long as you have the ID you can edit the uuid to boot from in grub at boot time if you made a typo. No uuid and you have to switch back and boot into your old system to figure them out. Fortunately I did the steps here and it worked so here I am :)
--------------------------

Then shut down the current system and reassign the boot order of your drives in the bios or disconnect the current system drive and try and boot off the new system. You should be able to move them around (e.g. master to slave) with no difference since it all goes off the UUIDs.

Once back in X you can use menu:system/administration/system monitor to verify that your swapspace is present and correct.

If it boots and runs ok then you should be able to go ahead and remove your original system disk either to keep as a backup or whatever....and that's it :)

If it doesn't boot and says "file not found" then you either got mistyped the UUID or got the wrong one.

If the bios detects the disk but keeps trying other devices (e.g. cdrom, network) then you probably haven't got the boot flag set (see above).

If it says it's going to boot from a given drive but then fails you might have missed the grub setup (see above).

-