You are here

Home

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:

mkdir tmp

Step 2. Mount /dev/sda5 to the "tmp" directory:

mount /dev/sda5 tmp

Step 3. Change the current directory to tmp/admin:

cd 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.

Articles: 

Comments

Not to be uncivilized but you the second command is TOTALY AND DREADFULLY WRONG.
Have you ever tried it on your hard drive to see what it does??
First of all to backup your MBR you got it right but on the restoration part you say:

dd of=/dev/sda if=mbr.img bs=512 count=1

that my dear friend will leave you with a hard drive ONLY with your MBR and absolutely NOTHING else.

The correct one is the following:

dd if=mbr.img of=/dev/sda bs=512 count=1 conv=notrunc

Sorry, the original author is right. Not only is it sound theoretically, but I tested it before posting here...

I just messed around with my partition table (going to reinstall anyway), then used his command to restore my 512 byte MBR backup, excluding your suggested option. My partitions were all intact, both OSes I had on the drive booted fine, etc.

lol the "of" before "if" bug is old, but still deadly :P
remember, always type "if" before "of" if u don't want to end with whatever u wrote as "of" filled with 00000000000000000000000 (aka empty with no psobility to undelete, the same efect as a full format)

This article agrees roughly with Ubuntu Staff here

http://ubuntuforums.org/showpost.php?s=b00272aaf455bafae79650104a49982c&...

But the cd-ing to a directory was not mentioned. You got fancy, nice.
Other than that, looks the same. So the nay sayers in the comments must be jokers. I didn't try anything yet though. Trying to figure it out first!

Was wondering if there is much need to backup the MBR in the first place?
Wouldn't reinstalling GRUB have the same effect and ease for most users?

In my system I install Windows 7 then Ubuntu and leave it alone. Maybe tweak the seconds it sits at the boot screen via a Linux Startup Manager but that's no big deal to set up. So as an alternate MBR rescue, wipe the MBR with a Windows rescue disk, install Windows boot system with their rescue disk and then reinstall GRUB. But is there a reason to use an MBR backup save-restore system instead of what I just mentioned? Is it harder on the "life" your 1st partition to do one instead of the other? Other reasons?

User login
Recent comments

BackupHowTo Tweets