Monday, November 1, 2010

Restoring a Linux system state backup

[Abstract]
In this post i show how to restore a system state backup created with dd.
It is a follow-up on my earlier post "Linux system state backup".

[Prerequisites]
You will need basic knowledge of Linux and the commands used for managing disks. You should also have read my earlier post "Linux system state backup".
This has been tested on Ubuntu 10.04 Server 64-bit.


[Restore]
In my previous guide i outlined how a simple system state backup can be performed with dd. That information can be found in a million different blog and forum posts. They also tell you to simply reverse the commands to restore the backup. I found that that just won't work, at least not on Ubuntu 10.04.

Instead we have to go through a slightly (this might be a serious understatement) more complex restore process. For this section i will assume that my hard drive has died, fortunately i have a spare and i also have the original installation disc lying around, although any linux boot disc should suffice.

To restore, follow these steps
  1. Boot the disc, i my case it's Ubuntu 10.04 Server 64-bit, selecting "Rescue a broken system"
  2. Go through any steps to get to a shell.
  3. Mount the partition where your backup data exists. I my case it's /dev/sdc1 and i mounted it as /mnt/sdc1
  4. Find out what disk is the new system disk, i simply use parted for this by looking at the partitions on each disk. In this scenario the new system disk is /dev/sda.
  5. Restore the mbr and partition table by issuing the following command dd if=/mnt/sdc1/Backups/2010-10-31/mbr.img of=/dev/sda bs=512 count=1
  6. At this point we have to refresh the partition table but for the life of me i couldn't find a way to do this with the ubuntu rescue mode. So i simply rebooted the machine and went through step 1 through 3 again.
  7. When the partition table has been refreshed it's time to restore the actual data. For some reason i can't do a pipe in Ubuntu's rescue mode so instead I'll have to unpack the image file manually first. The command is: gunzip -c  ???? sda1-backup.img.gz which results in the new file sda1-backup.img
  8. When the image file in unpacked, simply write it to the correct partition with the following command: dd if=/mnt/sdc1/Backups/2010-10-31/sda1-backup.img of=/dev/sda1 (This might take a while and there is no progress bar so be patient).
  9. Next up we need to format the swap partition. We only created the partition and marked it as swap, we didn't actually initialize the file system on it. So issue the following command, my swap partition is /dev/sda2. mkswap /dev/sda2
  10. I also had a temporary data partition, /dev/sda3. Since Ubuntu 10.04 will hang on boot if not all partitions are available i also need to create that file system and possibly edit fstab in /mnt/sda1/etc. Your config will probably differ so take appropriate action to make sure all partitions are accessible.
  11. At this point we have restored the mbr, partition table and data. However, every time i did this the boot loader got corrupt or was missing all together (it's hard to tell the difference). So we need to chroot to our new partition and run grub. Mount the partition, in my case it's /dev/sda1 and i mounted it as /mnt/sda1.
  12. Chrooting into the system isn't quite as easy as one might imagine. To make grub work you actually have to take three system directories with us. So issue the following three commands one at the time. mount --bind /dev /mnt/sda1/dev and mount --bind /proc /mnt/sda1/proc and finally mount --bind /sys /mnt/sda1/sys
  13. Now we can chroot into the correct file system by running the following command. chroot /mnt/sda1
  14. Finally, we reinstall grub by running the following command. grub-install /dev/sda
  15. You should see a message saying no errors were encountered.
Feel free to ask any questions or add comments regarding this post in the comments. I'll try and answer best i can, promise.

Cheers

No comments:

Post a Comment