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

by on December 3, 2010 · 6 comments

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.

  1. #!/bin/bash
  2. # Script Name: mbrback http://igurublog.wordpress.com/downloads/script-mbrback/
  3. # Requires: util-linux
  4. # License: GNU GENERAL PUBLIC LICENSE Version 3 http://www.gnu.org/licenses/gpl-3.0.txt
  5.  
  6. # do not change these variables!
  7. argsneeded=1
  8. restoretype=""
  9. back=""
  10. devname=""
  11.  
  12. help ()
  13. {
  14. echo 'mbrback version 1.0.0'
  15. echo 'Creates MBR and partition table backups of DEVICE named:'
  16. echo ' HOST-DEVICE-MBR-back'
  17. echo ' HOST-DEVICE-partition-back.sf'
  18. echo 'Restores MBR and partition table from specified backup file'
  19. echo 'Usage: sudo mbrback DEVICE [BACKUPFOLDER]'
  20. echo ' (creates backup files of DEVICE)'
  21. echo 'Usage: sudo mbrback --restoreboot DEVICE [BACKUPFILE]'
  22. echo ' (restores MBR boot code only)'
  23. echo 'Usage: sudo mbrback --restorefullmbr DEVICE [BACKUPFILE]'
  24. echo ' (restores entire MBR)'
  25. echo 'Usage: sudo mbrback --restorepart DEVICE [BACKUPFILE.sf]'
  26. echo ' (restores partition table)'
  27. echo 'Example: sudo mbrback sda'
  28. echo ' (creates MBR and partition table backups of'
  29. echo ' /dev/sda in current folder)'
  30. echo 'Example: sudo mbrback /dev/sda'
  31. echo ' (creates MBR and partition table backups of'
  32. echo ' /dev/sda in current folder)'
  33. echo 'Example: sudo mbrback sda /mybackups'
  34. echo ' (creates MBR and partition table backups of'
  35. echo ' /dev/sda in /mybackups)'
  36. echo 'Example: sudo mbrback --restoreboot sda /mybackups/sys-sda-MBR-back'
  37. echo ' (restores MBR boot code of /dev/sda using'
  38. echo ' /mybackups/sys-sda-MBR-back)'
  39. echo 'Example: sudo mbrback --restorepart sda /mybackups/sys-sda-partition-back.sf'
  40. echo ' (restores partition table of /dev/sda using sfdisk file '
  41. echo ' /mybackups/sys-sda-partition-back.sf)'
  42. echo
  43. echo "When restoring, mbrback will always tell you what it's going to do"
  44. echo "and allow you to abort before it writes to disk."
  45. echo
  46. echo "Instructions and updates:"
  47. echo "http://igurublog.wordpress.com/downloads/script-mbrback/"
  48. echo
  49. }
  50.  
  51. index=0
  52. while [ "$1" != "" ];
  53. do
  54. if [ "${1:0:1}" = "-" ]; then
  55. case "$1" in
  56. --help | -help )
  57. help
  58. exit
  59. ;;
  60. --restoreboot )
  61. if [ "$restoretype" = "" ]; then
  62. restoretype="boot"
  63. else
  64. echo 'mbrback: can only use one restore option'
  65. exit 1
  66. fi
  67. ;;
  68. --restorefullmbr )
  69. if [ "$restoretype" = "" ]; then
  70. restoretype="fullmbr"
  71. else
  72. echo 'mbrback: can only use one restore option'
  73. exit 1
  74. fi
  75. ;;
  76. --restorepart )
  77. if [ "$restoretype" = "" ]; then
  78. restoretype="part"
  79. else
  80. echo 'mbrback: can only use one restore option'
  81. echo
  82. help
  83. exit 1
  84. fi
  85. ;;
  86. * )
  87. echo "mbrback: Unknown option $1"
  88. echo
  89. help
  90. exit 1
  91. ;;
  92. esac
  93. else
  94. let "index+=1"
  95. case $index in
  96. 1 )
  97. devname=`basename "$1"`
  98. if [ ! -b "/dev/$devname" ]; then
  99. echo "mbrback: /dev/$devname is not a valid device"
  100. exit 1
  101. fi
  102. ;;
  103. 2 )
  104. back="$1"
  105. ;;
  106. * )
  107. echo "mbrback: Too many arguments"
  108. exit 1
  109. ;;
  110. esac
  111. fi
  112. shift
  113. done
  114. if (( index < $argsneeded )) || [ "$devname" = "" ]; then
  115. echo "mbrback: missing arguments"
  116. echo
  117. help
  118. exit 1
  119. fi
  120.  
  121. if [ `whoami` != "root" ]; then
  122. echo 'mbrback: must be run with sudo'
  123. exit 1
  124. fi
  125.  
  126. sysname=$HOSTNAME
  127.  
  128. if [ "$restoretype" = "" ]; then
  129. # create MBR and table backups
  130. if [ "$back" = "" ]; then
  131. back=`pwd`
  132. else
  133. if [ ! -d "$back" ]; then
  134. echo "mbrback: $back is not a valid backup folder"
  135. exit 1
  136. fi
  137. fi
  138. dd if=/dev/$devname of="$back/$sysname-$devname-MBR-back" bs=512 count=1
  139. sfdisk -d /dev/$devname > "$back/$sysname-$devname-partition-back.sf"
  140. else
  141. # restore
  142. if [ "$back" = "" ]; then
  143. echo "mbrback: you must specify a backup file"
  144. exit 1
  145. elif [ ! -f "$back" ]; then
  146. echo "mbrback: file not found - $back"
  147. exit 1
  148. fi
  149. if [ "$restoretype" = "boot" ] || [ "$restoretype" = "fullmbr" ]; then
  150. sfhead=`head --bytes=21 "$back"`
  151. if [ "$sfhead" = "# partition table of " ]; then
  152. echo "mbrback: $back is not an MBR backup file"
  153. exit 1
  154. fi
  155. if [ "$(stat -c%s "$back")" != "512" ]; then
  156. echo "mbrback: $back is wrong size for an MBR backup file"
  157. exit 1
  158. fi
  159. fi
  160. if [ "$restoretype" = "part" ]; then
  161. sfhead=`head --bytes=21 "$back"`
  162. if [ "$sfhead" != "# partition table of " ]; then
  163. echo "mbrback: $back not a valid sfdisk backup file"
  164. exit 1
  165. fi
  166. echo
  167. echo "You are about to overwrite your /dev/$devname partition table with"
  168. echo "the contents of $back"
  169. echo
  170. echo "WARNING!!! Unless the partition table has been damaged or you"
  171. echo " have accidentally deleted a partition, you should abort."
  172. echo
  173. echo "WARNING!!! Restoring the partition table from an out-of-date backup"
  174. echo " may render ALL the data on your drive unreadable."
  175. echo
  176. echo "WARNING!!! Do not proceed if /dev/$devname is mounted."
  177. echo
  178. elif [ "$restoretype" = "boot" ]; then
  179. echo
  180. echo "You are about to overwrite your /dev/$devname MBR boot code with"
  181. echo "the contents of $back"
  182. echo
  183. echo "WARNING: Restoring your MBR boot code from an out-of-date MBR backup"
  184. echo " file may render your computer unbootable."
  185. elif [ "$restoretype" = "fullmbr" ]; then
  186. echo
  187. echo "You are about to overwrite your ENTIRE /dev/$devname MBR with"
  188. echo "the contents of $back"
  189. echo
  190. echo "WARNING!!! The full MBR contains both boot code and the drive's"
  191. echo " partition table. Unless the partition table has been"
  192. echo " damaged or you have accidentally deleted a partition"
  193. echo " you should abort and restore boot code only with"
  194. echo " --restoreboot instead."
  195. echo
  196. echo "WARNING!!! Restoring your full MBR from an out-of-date MBR backup may"
  197. echo " render your computer unbootable and ALL the data on your"
  198. echo " drive unreadable."
  199. echo
  200. echo "WARNING!!! Do not proceed if /dev/$devname is mounted."
  201. fi
  202. echo
  203. echo "Do you want to proceed? (you must type yes to proceed)"
  204. read s1
  205. if [ "$s1" != "yes" ]; then
  206. echo "mbrback: no changes made - aborted at user request"
  207. exit 2
  208. fi
  209. if [ "$restoretype" = "part" ]; then
  210. sfdisk /dev/$devname < "$back"
  211. elif [ "$restoretype" = "boot" ]; then
  212. dd if="$back" of=/dev/$devname bs=448 count=1
  213. elif [ "$restoretype" = "fullmbr" ]; then
  214. dd if="$back" of=/dev/$devname bs=512 count=1
  215. fi
  216. echo "/dev/$devname was updated"
  217. fi
  218.  
  219. exit 0

( source, released under GPL v3.0 )



4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • cant use this

    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?

  • nagendra

    how to install the Rpm packages in red hat Linux

  • Ashish Jaiswal

    @nagendra

    # rpm -iv

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

  • Ashish Jaiswal

    @nagendra

    # rpm -ivh package-name.rpm

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

  • jai singh

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

  • george

    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 …

Previous Script:

Next Script: