vaheeD khoshnouD

linux, mikrotik, macosx

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

Written by vaheeD on January 4, 2013
4.00 avg. rating (84% score) - 1 vote

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

     

4.00 avg. rating (84% score) - 1 vote

Posted Under: Linux

About vaheeD

Leave a Reply

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

Protected by WP Anti Spam