You are here
Backup MBR in Linux
MBR is short for Master Boot Record. It is a very small program which is located in your hard disk and executes when a computer boots up, before the operating system. Typically, it is at the first sector on your hard disk and the size is 512 bytes. The MBR maintains the DPT (Disk Partition Table), which can contains 4 primary partitions, and start the operating system. If the MBR is damaged, your operating system wouldn't be able to boot up.
Why should you backup the MBR?
Since the MBR records some information about your hard disk partitions and it loads the operating system, it is very important for a computer. Some virus can damage the MBR. They can modify the MBR so that they can start before the operating system. Or it makes your computer not to be able to run. Reinstall the operating system can solve this problem but it takes too long time. In fact, if you backup the MBR, the solution is very easy: just restore it.
How to backup MBR in Linux?
Linux or Unix provides "dd" tool to backup any file. Usually, you must be "root" so that you can use it. Before you try it, you must know it is a dangerous command, any mistake may damage your hard disk. BE CAREFUL! To backup the MBR, open the terminal and execute the following command:
dd if=/dev/sda of=~/mbr.img bs=512 count=1
Here, I suppose your hard disk is /dev/sda. It means that copy 1 block (count=1) from /dev/sda (if=/dev/sda) to ~/mbr.img (of=~/mbr.img) and the block size is 512 bytes (bs=512). After that, you can see "mbr.img" in your home directory. Its content is MBR.
Notes: Since the MBR doesn't belong to any partition, you should use if=/dev/sda, or /dev/sdb, /dev/hda, etc, instead of /dev/sda1, /dev/sdb1... You must pay attention to the "of" and "if" arguments. For example, if you type this:
dd of=/dev/sda if=~/yourfile bs=512 count=1 (DON'T try it!)
It means reading 512 bytes of ~/yourfile and writing it to /dev/sda. If ~/yourfile is a normal file instead of MBR, your MBR will be damaged. In fact, it will covers the MBR with ~/yourfile. You will find the operating system can't start next time.
How to restore MBR in Linux?
Since if the MBR is damaged, the operating system can't boot up. You need a Linux live CD to start your computer. Linux live CD is a CD that contains Linux operating system. It doesn't need to be installed on your hard disk an can be run from CDROM directly. For example, Puppy Linux. I strongly advise you to make a live CD in advance. To boot from a live CD, insert the CD into your CDROM and make sure the first boot device is the CDROM (you can change the order of boot devices in BIOS). Then restart the computer. After a while, you will enter Linux environment. For example, if the MBR image file (mbr.img) was at /home/admin/mbr.img and /dev/sda5 was mounted as /home. You should mount /dev/sda5 at first.
Step 1. Open terminal and execute the following command to make a directory:
Step 2. Mount /dev/sda5 to the "tmp" directory:
mount /dev/sda5 tmp
Step 3. Change the current directory to tmp/admin:
Step 4. Restore the MBR:
dd of=/dev/sda if=mbr.img bs=512 count=1
After that you can reject the live CD and restart your computer. If there are not other problems, your computer will work again.
If you are interested in MBR backup and restore in Windows too, check the article How to backup disk partitions.