Linux Shell Script To Backup and Restore MBR (Master Boot Recored)

mbrback shell script 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.

#!/bin/bash
# Script Name: mbrback    http://igurublog.wordpress.com/downloads/script-mbrback/ 
# Requires: util-linux
# License: GNU GENERAL PUBLIC LICENSE Version 3 http://www.gnu.org/licenses/gpl-3.0.txt
 
# do not change these variables!
argsneeded=1
restoretype=""
back=""
devname=""
 
help ()
{
	echo 'mbrback version 1.0.0'
	echo 'Creates MBR and partition table backups of DEVICE named:'
	echo '    HOST-DEVICE-MBR-back'
	echo '    HOST-DEVICE-partition-back.sf'
	echo 'Restores MBR and partition table from specified backup file'
	echo 'Usage: sudo mbrback DEVICE [BACKUPFOLDER]'
	echo '       (creates backup files of DEVICE)'
	echo 'Usage: sudo mbrback --restoreboot DEVICE [BACKUPFILE]'
	echo '       (restores MBR boot code only)'
	echo 'Usage: sudo mbrback --restorefullmbr DEVICE [BACKUPFILE]'
	echo '       (restores entire MBR)'
	echo 'Usage: sudo mbrback --restorepart DEVICE [BACKUPFILE.sf]'
	echo '       (restores partition table)'
	echo 'Example: sudo mbrback sda'
	echo '         (creates MBR and partition table backups of'
	echo '          /dev/sda in current folder)'
	echo 'Example: sudo mbrback /dev/sda'
	echo '         (creates MBR and partition table backups of'
	echo '          /dev/sda in current folder)'
	echo 'Example: sudo mbrback sda /mybackups'
	echo '         (creates MBR and partition table backups of'
	echo '          /dev/sda in /mybackups)'
	echo 'Example: sudo mbrback --restoreboot sda /mybackups/sys-sda-MBR-back'
	echo '         (restores MBR boot code of /dev/sda using'
	echo '          /mybackups/sys-sda-MBR-back)'
	echo 'Example: sudo mbrback --restorepart sda /mybackups/sys-sda-partition-back.sf'
	echo '         (restores partition table of /dev/sda using sfdisk file '
	echo '          /mybackups/sys-sda-partition-back.sf)'
	echo
	echo "When restoring, mbrback will always tell you what it's going to do"
	echo "and allow you to abort before it writes to disk."
	echo
	echo "Instructions and updates:"
	echo "http://igurublog.wordpress.com/downloads/script-mbrback/"
	echo
}
 
index=0
while [ "$1" != "" ];
do
	if [ "${1:0:1}" = "-" ]; then
		case "$1" in
			--help | -help )
				help
				exit
				;;
			--restoreboot )
				if [ "$restoretype" = "" ]; then
					restoretype="boot"
				else
					echo 'mbrback: can only use one restore option'
					exit 1
				fi
				;;
			--restorefullmbr )
				if [ "$restoretype" = "" ]; then
					restoretype="fullmbr"
				else
					echo 'mbrback: can only use one restore option'
					exit 1
				fi
				;;
			--restorepart )
				if [ "$restoretype" = "" ]; then
					restoretype="part"
				else
					echo 'mbrback: can only use one restore option'
					echo
					help
					exit 1
				fi
				;;
			* )
				echo "mbrback: Unknown option $1"
				echo
				help
				exit 1
				;;
		esac
	else
		let "index+=1"
		case $index in
			1 )
				devname=`basename "$1"`
				if [ ! -b "/dev/$devname" ]; then
					echo "mbrback: /dev/$devname is not a valid device"
					exit 1
				fi
				;;
			2 )
				back="$1"
				;;
			* )
				echo "mbrback: Too many arguments"
				exit 1
				;;
		esac
	fi
	shift
done
if (( index < $argsneeded )) || [ "$devname" = "" ]; then
	echo "mbrback: missing arguments"
	echo
	help
	exit 1
fi
 
if [ `whoami` != "root" ]; then
	echo 'mbrback: must be run with sudo'
	exit 1
fi
 
sysname=$HOSTNAME
 
if [ "$restoretype" = "" ]; then
	# create MBR and table backups
	if [ "$back" = "" ]; then
		back=`pwd`
	else
		if [ ! -d "$back" ]; then
			echo "mbrback: $back is not a valid backup folder"
			exit 1
		fi
	fi
	dd if=/dev/$devname of="$back/$sysname-$devname-MBR-back" bs=512 count=1
	sfdisk -d /dev/$devname > "$back/$sysname-$devname-partition-back.sf"
else
	# restore
	if [ "$back" = "" ]; then
		echo "mbrback: you must specify a backup file"
		exit 1
	elif [ ! -f "$back" ]; then
		echo "mbrback: file not found - $back"
		exit 1
	fi
	if [ "$restoretype" = "boot" ] || [ "$restoretype" = "fullmbr" ]; then
		sfhead=`head --bytes=21 "$back"`
		if [ "$sfhead" = "# partition table of " ]; then
			echo "mbrback: $back is not an MBR backup file"
			exit 1
		fi
		if [ "$(stat -c%s "$back")" != "512" ]; then
			echo "mbrback: $back is wrong size for an MBR backup file"
			exit 1
		fi
	fi
	if [ "$restoretype" = "part" ]; then
		sfhead=`head --bytes=21 "$back"`
		if [ "$sfhead" != "# partition table of " ]; then
			echo "mbrback: $back not a valid sfdisk backup file"
			exit 1
		fi
		echo
		echo "You are about to overwrite your /dev/$devname partition table with"
		echo "the contents of $back"
		echo
		echo "WARNING!!! Unless the partition table has been damaged or you"
		echo "           have accidentally deleted a partition, you should abort."
		echo
		echo "WARNING!!! Restoring the partition table from an out-of-date backup"
		echo "           may render ALL the data on your drive unreadable."
		echo 
		echo "WARNING!!! Do not proceed if /dev/$devname is mounted."
		echo
	elif [ "$restoretype" = "boot" ]; then
		echo
		echo "You are about to overwrite your /dev/$devname MBR boot code with"
		echo "the contents of $back"
		echo 
		echo "WARNING: Restoring your MBR boot code from an out-of-date MBR backup"
		echo "         file may render your computer unbootable."
	elif [ "$restoretype" = "fullmbr" ]; then
		echo
		echo "You are about to overwrite your ENTIRE /dev/$devname MBR with"
		echo "the contents of $back"
		echo
		echo "WARNING!!! The full MBR contains both boot code and the drive's"
		echo "           partition table.  Unless the partition table has been"
		echo "           damaged or you have accidentally deleted a partition"
		echo "           you should abort and restore boot code only with"
		echo "           --restoreboot instead."
		echo
		echo "WARNING!!! Restoring your full MBR from an out-of-date MBR backup may"
		echo "           render your computer unbootable and ALL the data on your"
		echo "           drive unreadable."
		echo 
		echo "WARNING!!! Do not proceed if /dev/$devname is mounted."
	fi
	echo
	echo "Do you want to proceed? (you must type yes to proceed)"
	read s1
	if [ "$s1" != "yes" ]; then
		echo "mbrback: no changes made - aborted at user request"
		exit 2
	fi
	if [ "$restoretype" = "part" ]; then
		sfdisk /dev/$devname < "$back"
	elif [ "$restoretype" = "boot" ]; then
		dd if="$back" of=/dev/$devname bs=448 count=1
	elif [ "$restoretype" = "fullmbr" ]; then
		dd if="$back" of=/dev/$devname bs=512 count=1
	fi
	echo "/dev/$devname was updated"
fi
 
exit 0

( source, released under GPL v3.0 )

Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
6 comments… add one
  • george Dec 20, 2012 @ 9:10

    why not just use the dd command instead. It’s a one liner, and the command is already installed on any linux or unix system …

  • jai singh Dec 1, 2011 @ 11:06

    you can install rpm packages by that command
    rpm -ivh firefox(package name )

  • Ashish Jaiswal Nov 30, 2011 @ 9:35

    @nagendra

    # rpm -ivh package-name.rpm

    i = Install
    v = verbosity
    h = hash (# marks)

  • Ashish Jaiswal Nov 30, 2011 @ 9:34

    @nagendra

    # rpm -iv

    i = Install
    v = verbosity
    h = hash (# marks)

  • nagendra Aug 5, 2011 @ 10:58

    how to install the Rpm packages in red hat Linux

  • cant use this May 28, 2011 @ 21:11

    your table is too narrow for the code on Chromium. it chops off the right end.

    I have a screen width of 1336 so it is not me :D

    Had to download the page source and pull it from there. BAD DESIGN. Why not put the code in a scrolling text area like everyone else does … for this reason?

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.