Backup and Restore a MBR on Linux

Backing up Master Boot Record (MBR)
The MBR  is a 512 byte segment on the very first sector of your hard drive composed of three parts: 1) the boot code which is 446 bytes long, 2) the partiton table which is 64 btyes long, and 3) the boot code signature which is 2 bytes long.
The core of the backup command is dd—which will be familiar to every system administrator, especially to those who intend to clone an entire hard disk. To see all the options type man dd. As we want to back up only the first 512 bytes we need to append some arguments to it. Here is the full command you need (and remember to run it as the root user, su (and sudo for Ubuntu users):

[root@host]# dd if=/dev/sda of=/tmp/mbr.img bs=512 count=1

Restoring the MBR
You can use a live CD to access your hard drive and read the backup off any removable media such as a USB stick. Here is the command:

[root@host]# dd if=/tmp/mbr.img of=/dev/sda bs=512 count=1

Again, amend sda to read where you saved the MBR and run the command as root. If you wish to kill the MBR altogether, including the partition table, then you can overwrite it with a series of zeros:

[root@host]# dd if=/dev/zero of=/dev/sda bs=512 count=1

If you want to kill the MBR but leave the partition table intact then simply change 512 to 446.

1 comment:

  1. Excellent if you change the line:

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


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