Problem solve Get help with specific problems with your technologies, process and projects.

Backing up the Linux master boot record

The Linux master boot record (MBR) contains GRUB and the partition table, and to avoid crippling boot errors, it is a good idea to make a backup of it. Learn how to backup the MBR and how this effort can pay off when troubleshooting a Linux boot problem.

In the previous three parts of this series you've learned about recovering Linux using a Knoppix rescue CD, how...

to troubleshoot Linux boot problems and how to fix problems related to the GRUB boot loader, which often is installed in the master boot record (MBR) of your computer. As the MBR is an essential part of your computer that not only contains GRUB, but the partition table as well, it's a good idea to make a backup of it. In this article you'll learn how to do that.

Hopefully you are reading this article before you are in trouble. If that's the case, it is a good idea to make a backup of the MBR of your primary hard disk. This sector of only 512 bytes contains information that is vital to boot your computer and by creating a backup, you make it a lot easier to fix any problem that is related to either GRUB or the partition table. If you have such a backup (don't forget to create it again after making major changes!), you just have to restore the backup if you get into serious trouble.

Finding the master boot record on Linux

To make a backup of your MBR, you need to know what the name is of your primary hard disk. On most Linux computers, this will be /dev/sda, but other names may occur as well. You can find out which name is used for your computers primary hard drive by using the fdisk –l command. The storage device that is listed first is most likely your computer's primary hard disk.

Backing up the MBR using dd

I'll assume that the name of the hard disk that contains the MBR is /dev/sda. You can use the dd command to make a backup of it. One of the major benefits of dd, is that it allows you to copy blocks, no matter where on your computer's hard drive these blocks are. The following command for instance would create a backup of the MBR:

dd if=/dev/sda of=/boot/backup_mbr bs=512 count=1

The structure of this command is not too hard to understand. The dd command always works with two parts, the input file, which is referred to by using the if parameter, and the output file, which is referred to by using the of parameter. The nice thing about dd is that for both parameters, you can use the name of a regular file, but also the name of a block device. So in the example above, the input file is your computer hard drive and the output file is a file with the name backup_mbr, which is created in the directory /boot. Next, the parameter bs=512 makes sure that a 512 bytes block is written, and by using count=1 you make sure that only one of those blocks is written.

As you can see, in this example I've chosen to write this backup file to the directory /boot. That is on purpose: /boot is always on a traditional partition and cannot be on an LVM logical volume (this will be discussed later in this series of articles), and by putting the backup file on a traditional partition, you make it much easier to access it later in case of trouble.

Restoring the Linux master boot record

Now if you are having boot problems that are related to the MBR, you can boot your computer from a rescue CD, such as a Knoppix CD. Once you have mounted the /boot partition on the appropriate device, you can use the following command to restore the backup of your MBR to hard disk:

dd if=/boot/backup_mbr of=/dev/sda bs=512 count=1

Troubleshooting is much easier if you take some precautionary measures. One of these is making a backup of the MBR of your computer's primary hard disk. In this article you've read how to do that. This back up comes in handy if you're having problems that are related to the MBR, such as GRUB or partition problems. If that occurs, you just have to restore the backup as described in this article.

About the auhtor:
Sander van Vugt is an author and independent technical trainer, specializing in Linux since 1994. Vugt is also a technical consultant for high-availability (HA) clustering and performance optimization, as well as an expert on SLED 10 administration.

Dig Deeper on Linux servers