There're several ways to accomplish this, if you want an image of your complete installation and have a secondary hard disk (or a network location) as destination, then assuming your installation is on /dev/sda (in this example boot might be /dev/sda1, swap is /dev/sda2, root is /dev/sda3) and your secondary hard disk or network location is mounted at /mnt/backup
dd if=/dev/sda1 | bzip2 -9 > /mnt/backup/boot-image.bz2
dd if=/dev/sda3 | bzip2 -9 > /mnt/backup/root-image.bz2
dd if=/dev/sda count=512 > /mnt/backup/sda.mbr
You'll notice that swap is left out.
To restore the image, you'd boot using a livecd or similar, mount the backup location then run:
# caution this will erase /dev/sda
dd if=/mnt/backup/sda.mbr of=/dev/sda
At this point /dev/sda's partition, table should be restored & visible when you run:
fdisk -l
So you can restore with:
bzcat /mnt/backup/boot-image.bz2 | dd of=/dev/sda1
bzcat /mnt/backup/root-image.bz2 | dd of=/dev/sda3
mkswap /dev/sda2
Remove your bootable media, reboot and your system has been restored
However, it's often not preferable to image large filesystem because garbage in the free space will be included, I'd recommend you skip the steps for sda3 above and instead to backup use:
cd /; tar -cjf /mnt/backup/root.tar.bz2 / -X /tmp/exclude.txt
You would obviously first need to create /tmp/exclude.txt which should list locations to exclude from the backup, for example:
/dev/*
/proc/*
/sys/*
/tmp/*
/mnt/*
Then to restore from your LiveCD or whatever:
mkfs.ext3 -L root /dev/sda3
mkdir /mnt/restore; mount /dev/sda3 /mnt/restore; cd /mnt/restore
tar -xjvpf /mnt/backup/root.tar.bz2
Or using rsync, in which case just create the filesystem & mount it as above & then rsync the contents back.
Great answer. I have two questions. First: unfortunately, I have no separate boot partition, but a single one where boot and / are, and a swap partition. But I have a copy of the first cylinder I did with: dd if=/dev/sda of=mbr_63.backup bs=512 count=63. Will it work if I restore that fist cylinder with dd (after that, fdisk -l should say the partitions are back, is that right?) and then I merely rsync everything back? – Mephisto – 2013-10-02T13:51:30.480
Second question: Have you figured out this answer from scratch, or it is something you have already applied successfully? It is important to know that. If you are a beginning BOFH as your profile say, chances are that you have already restored cloned installations more than once. Is that the case? – Mephisto – 2013-10-02T13:55:33.917
I've never backed up the first cylinder, so I can't speak to that. I would test the method before relying on it. You would also need to recreate the file systems before being able to rsync your data back. In answer to your question, I use a combination of the methods in my answer on a daily basis to create/backup/restore VM infrastructure. – Nanzikambe – 2013-10-02T17:05:07.883