IgnorantGuru's Blog

Linux software, news, and tips

Script: mbrback

«Downloads

Download Links: downloadbrowseauthenticate
Description: Creates MBR and partition table backups and restores from backups
Recommended For: Linux
Requires: util-linux
License: GNU GPL v3     * SEE DISCLAIMER *
Related:
Feedback: commentsissues

Overview

mbrback creates a backup of your hard drive’s MBR and its partition table. You can then use mbrback to restore the MBR boot code, full MBR, or partition table from the backup files.

mbrback --help

Creates MBR and partition table backups of DEVICE named:
    HOST-DEVICE-MBR-back
    HOST-DEVICE-partition-back.sf
Restores MBR and partition table from specified backup file
Usage: sudo mbrback DEVICE [BACKUPFOLDER]
       (creates backup files of DEVICE)
Usage: sudo mbrback --restoreboot DEVICE [BACKUPFILE]
       (restores MBR boot code only)
Usage: sudo mbrback --restorefullmbr DEVICE [BACKUPFILE]
       (restores entire MBR)
Usage: sudo mbrback --restorepart DEVICE [BACKUPFILE.sf]
       (restores partition table)
Example: sudo mbrback sda
         (creates MBR and partition table backups of
          /dev/sda in current folder)
Example: sudo mbrback /dev/sda
         (creates MBR and partition table backups of
          /dev/sda in current folder)
Example: sudo mbrback sda /mybackups
         (creates MBR and partition table backups of
          /dev/sda in /mybackups)
Example: sudo mbrback --restoreboot sda /mybackups/sys-sda-MBR-back
         (restores MBR boot code of /dev/sda using
          /mybackups/sys-sda-MBR-back)
Example: sudo mbrback --restorepart sda /mybackups/sys-sda-partition-back.sf
         (restores partition table of /dev/sda using sfdisk file
          /mybackups/sys-sda-partition-back.sf)

When restoring, mbrback will always tell you what it's going to do
and allow you to abort before it writes to disk.

To create backups of your first hard drive’s MBR and partition table:

sudo mbrback sda

That command will create two files: “systemname-sda-MBR-back”, which is a backup of the full MBR, and “systemname-sda-partition-back.sf”, which is a human- and machine-readable partition table backup created by sfdisk.

Note: You can run the above command even if /dev/sda is a mounted system partition.

mbrback will overwrite old backup files without prompting.

For more information on the MBR, consult How The Computer Boots – The MBR Explained.

Restoring MBR Boot Code

There are various ways to use these backup files for restoration. One common scenario is installing Windows after Linux. The Windows installation program will overwrite the MBR boot code, rendering Linux unbootable. To correct this, you can boot with a live Linux CD, such as SystemRescueCD, mount the partition containing mbrback and your backup files, and tell mbrback to restore the MBR boot code. For example:

# Note: You need to specify the full location of mbrback and the backup file
sudo /mnt/myscripts/mbrback --restoreboot /mnt/mybackups/systemname-sda-MBR-back

Assuming GRUB was installed when you made your MBR backup, the above command is generally equivalent to reinstalling GRUB to your MBR. One exception is if the physical disk sector locations of GRUB’s stage files have changed, perhaps due to restoration with a tool like FSArchiver. In this case, GRUB will need to be reinstalled to the MBR to prevent a ‘file not found’ error.

Restoring A Partition Table

A less common scenario is having a damaged partition table, or a partition table that has accidentally had a partition entry changed or deleted. In this case you may want to restore the partition table.

IMPORTANT WARNINGS: Most users will never have occassion to restore a partition table or full MBR. Only restore the partition table if it has been damaged. Restoring a partition table or the full MBR from an out-of-date backup file may render ALL the data on your drive unreadable! NEVER restore a partition table or full MBR to a mounted drive.

If you do need to restore a partition table, there are two ways to do this:

The full MBR contains both boot code and the primary partition table. Thus restoring the full MBR will restore the primary partition table. However, it may NOT restore the extended partitions. To restore the full MBR, use a command like:

# Note: You need to specify the full location of mbrback and the backup file
sudo mbrback --restorefullmbr "systemname-sda-MBR-back"

A second way to restore the partition table is using the sfdisk backup file, e.g. “systemname-sda-partition-back.sf”. This may also restore the extended partition table. This can be done with a command like:

# Note: You need to specify the full location of mbrback and the backup file
sudo mbrback --restorepart "systemname-sda-partition-back.sf"

When restoring, mbrback will always tell you what it’s going to do, issue applicable warnings, and allow you to abort before it writes to disk.

See ‘man sfdisk’ for more information.

Installation Instructions

Follow the standard Script Installation Instructions. Also, because you will sometimes want to run mbrback when the main system partition is unbootable or unreadable, you may want to save or install a copy to another partition or drive.

mbrback also requires the “util-linux” package (aka “util-linux-ng” in some distributions). This provides sfdisk. However, this package is generally installed by default and does not need to be added when installing mbrback.

6 Comments »

  1. When i ran this script, it creates 512 bytes of file for MBR backup.

    I thought the true MBR Boot Code was the 446 bytes

    Comment by Nick | March 17, 2011 | Reply

    • The full 512 byte MBR contains both the boot code (actually 448 bytes) and the primary partition table. For restoring, the script has several options to restore the full MBR, just the boot code, or just the partition table. Generally you will only restore the boot code. It will warn you and tell you the consequences before it does anything.

      Some more info…
      The MBR Explained
      &
      MBR Restoration

      Comment by igurublog | March 18, 2011 | Reply

  2. This is a nice script…Very much usefull…I would like to add if its possible or neccesary..maybe you can add a filesystem backup info or simple copy of backup superblock “Primary and Backup” for data restoration also..like an example i used.
    “dumpe2fs /dev/sda | grep -i superblock > FILENAME_BACKUP”

    Comment by Jaomadn | December 20, 2012 | Reply

    • Thanks for the feedback and suggestion – I’ll consider that for future updates.

      Comment by IgnorantGuru | December 21, 2012 | Reply

  3. Hi, I am trying to automate mbrback using a bash script, and typing “yes” would cause an issue in my case, is there way to auto answer “yes” and let mbrback to restore without the “yes” confirmation. Thanks.

    Comment by netrock69 | February 5, 2014 | Reply

    • Hi, there is no option for this, but mbrback is a bash script itself, so you can hack the script and disable that prompt, or add an option to override.

      Comment by IgnorantGuru | February 8, 2014 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 94 other followers