"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "backup-mbr-1.7/backup-mbr" of archive backup-mbr-1.7.tar.gz:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 #!/bin/bash
    2 #
    3 PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PATH"
    4 
    5 help() {
    6 less -M << EOF
    7 
    8 Press 'q' to Quit, Enter to scroll!
    9 
   10 =============================================================================
   11 You can use this script to backup and restore the MBR. v1.7 (11-6-2007)
   12 
   13 The (M)aster (B)oot (R)ecord is the first 512 bytes of a hard drive,
   14 you should always have a current backup of the MBR!
   15 
   16 To hold the MBR, you can use any media with a filesystem on it,
   17 but the filesystem on the media must be supported by the kernel!
   18 
   19 In addition to backing up the MBR of the hard drive you specify, it also
   20 makes a backup of the boot sector from each partition on the hard drive.
   21 
   22 Backup MBR from /dev/sda to directory /mbr on usb device /dev/sdb.
   23 # backup-mbr -b -d /mbr sda sdb  "If directory doesn't exist, will create."
   24 
   25 Restore MBR to /dev/sda from backup in directory /mbr on /dev/sdb.
   26 # backup-mbr -r -d /mbr sda sdb
   27 
   28 Restore boot sector to /dev/sda1 from backup in directory /mbr on /dev/sdb.
   29 # backup-mbr -r -d /mbr sda1 sdb
   30 
   31 If you want to backup the MBR to a multi-session CD-R/RW, do this.
   32 # backup-mbr -cd-multi -b -d /mbr sda scd0
   33 
   34 You can specify the cd device the old (pre kernel v2.6) way, like this.
   35 # backup-mbr -cd-multi -b -d /mbr sda 0,0,0
   36 
   37 You'll need 'cdrecord' and 'mkisofs' installed, and have a CD in the
   38 drive '/dev/scd0', or 0,0,0 etc.
   39 
   40 If it's not a multi-session CD-R/RW, do it this way.
   41 # backup-mbr -cd -b -d /mbr sda scd0  "CD will be written multi-session."
   42 
   43 The non multi-session CD-RW must be blank, blank it first if it's not.
   44 cdrecord blank=fast dev=/dev/scd0  "You'll loose anything on the CD!"
   45 
   46 Or, if you want this script to blank the CD-RW.
   47 
   48 # backup-mbr -cd-blank -b -d /mbr sda scd0
   49 
   50 Restore MBR to /dev/sda from backup in directory /mbr on /dev/scd0.
   51 # backup-mbr -r -d /mbr sda scd0
   52 =============================================================================
   53 EOF
   54 exit
   55 }
   56 
   57 fdisk_mbr() {
   58 echo "You can restore the whole MBR (512 bytes) like this.
   59 dd if=$1.mbr of=/dev/$1 bs=512 count=1
   60 
   61 If you just want to restore the MBR boot sector (first 446 bytes), do this.
   62 dd if=$1.mbr of=/dev/$1 bs=446 count=1
   63 
   64 If you just want to restore the MBR partition table (last 64 bytes), do this.
   65 dd if=$1.mbr of=/dev/$1 bs=1 count=64 skip=446 seek=446" >/tmp/floppy/$2/mbr-$1.txt
   66 
   67   fdisk -l /dev/$1 >/tmp/floppy/$2/fdisk.$1
   68   fdisk -ul /dev/$1 >>/tmp/floppy/$2/fdisk.$1
   69 }
   70 
   71 sfdisk_mbr() {
   72   sfdisk -d /dev/$1 >/tmp/floppy/$2/sfdisk.$1
   73 echo "
   74 You can also use sfdisk to restore the MBR partition table.
   75 sfdisk /dev/$1 < sfdisk.$1
   76 
   77 Or, you can use the backup-mbr script.
   78 
   79 Read sfdisk.$1, but don't edit.
   80 
   81 Read fdisk.$1 for a listing of the partition table." >>/tmp/floppy/$2/mbr-$1.txt
   82 }
   83 
   84 mbr_cd() {
   85 error() {
   86   rm -rf /tmp/floppy
   87   rm -f /tmp/mbr.iso
   88   cat /tmp/error
   89   echo
   90   echo "ERROR! $1"
   91   rm -f /tmp/error
   92   exit
   93 }
   94 
   95 if ! grep -q -w $2 /proc/partitions ; then
   96   echo "ERROR! I can't find \`$2' listed in /proc/partitions."
   97   exit
   98 elif ! echo $2 | grep -q "[a-z]$" ; then
   99   echo "ERROR! The device \`$2' is not a whole disk."
  100   exit
  101 fi
  102   mkdir -p /tmp/floppy/$1
  103   dd if=/dev/$2 of=/tmp/floppy/$1/$2.mbr bs=512 count=1 1>/dev/null 2>/tmp/error && \
  104 if [ ! $? = 0 ]; then
  105   rm -rf /tmp/floppy 
  106   cat /tmp/error
  107   echo
  108   echo "ERROR! There was a problem."
  109   rm -f /tmp/error
  110   exit
  111 else
  112   echo
  113   echo "Copied MBR from \`/dev/$2' for \`$1/$2.mbr' on $3."
  114   if [ -x "`type -path fdisk`" ]; then
  115     pt=`fdisk -l /dev/$2 | grep "^/dev/$2" | awk '{print $1}'` 
  116   for p in $pt ; do
  117     dev=`echo $p | cut -b 6-10`
  118     dd if=/dev/$dev of=/tmp/floppy/$1/$dev.bs bs=512 count=1 1>/dev/null 2>/dev/null && \
  119     echo "Copied boot sector from \`/dev/$dev' for \`$1/$dev.bs' on $3."
  120   done
  121     fdisk_mbr $2 $1
  122   fi
  123     sfdisk_mbr $2 $1
  124 fi
  125 
  126 echo "*** Creating mbr.iso..."
  127 
  128 if [ "$cd_multi" = "yes" ]; then
  129 multi="`cdrecord -msinfo dev=$3 2>/tmp/error | head -n 1`"
  130 echo $multi | grep "^[0-9]" | grep -q "[0-9]$" || error=yes 
  131 if [ "$error" = "yes" ]; then
  132   echo "There's not a multi-session CD in $3."
  133   exit
  134 fi 
  135   (cd /tmp/floppy && mkisofs -R -J -o ../mbr.iso -C $multi -M $3 . 1>/dev/null 2>/tmp/error) || error
  136 else
  137   (cd /tmp/floppy && mkisofs -R -J -o ../mbr.iso . 1>/dev/null 2>/tmp/error) || error
  138 fi
  139 
  140 if [ "$cd_blank" = "yes" ]; then
  141 echo "*** Blanking CD..."
  142 cdrecord blank=fast dev=$3 1>/dev/null 2>/tmp/error || error
  143 fi
  144 
  145 echo "*** Writing mbr.iso multi-session to $3..."
  146 cdrecord -multi -tao -data dev=$3 /tmp/mbr.iso 1>/dev/null 2>/tmp/error || error
  147 
  148   rm -rf /tmp/floppy
  149   rm -f /tmp/mbr.iso /tmp/error
  150   echo "*** Read $1/mbr-$2.txt on $3."
  151   echo "*** Done!"
  152   exit
  153 }
  154 
  155 if [ -z "$1" ]; then
  156    help
  157 elif [ "$1" = "-cd" -o "$1" = "-cd-multi" -o "$1" = "-cd-blank" ]; then
  158 if [ ! -x "`type -path cdrecord`" -o ! -x "`type -path mkisofs`" ]; then
  159    echo "ERROR! I can't find cdrecord and/or mkisofs."
  160    exit  
  161 else
  162    if [ "$1" = "-cd-multi" ]; then
  163      cd_multi=yes
  164    elif [ "$1" = "-cd-blank" ]; then
  165      cd_blank=yes
  166    fi
  167      shift
  168      cd=yes
  169      if echo $5 | grep -q "," ; then
  170         cd_dev=$5
  171         dev=$4
  172      else
  173         cd_dev=/dev/$5
  174         dev=$5
  175      fi
  176 fi
  177 else
  178 dev=$5
  179 fi
  180 
  181 if [ "$1" = "-b" -a "$2" = "-d" -a -n "$3" -a -n "$4" -a -n "$5" ]; then
  182 
  183 if [ -b "/dev/$4" -a -b "/dev/$dev" ]; then
  184 
  185 if [ -n "$cd" ]; then
  186   mbr_cd $3 $4 $cd_dev $cd_multi $cd_blank 
  187 fi
  188 
  189 if ! grep -q -w $4 /proc/partitions ; then
  190   echo "ERROR! I can't find \`$4' listed in /proc/partitions."
  191   exit
  192 fi
  193 if ! echo $4 | grep -q "[a-z]$" ; then
  194   echo "ERROR! The device \`$4' is not a whole disk."
  195   exit
  196 fi
  197   umount /tmp/floppy 2>/dev/null
  198   umount /dev/$5 2>/dev/null
  199   mkdir -p /tmp/floppy
  200 if ! mount -o rw /dev/$5 /tmp/floppy 1>/dev/null 2>/dev/null ; then 
  201   echo
  202   echo "ERROR! I can't mount device \`/dev/$5'!"
  203   exit
  204 fi
  205 if [ ! -d "/tmp/floppy/$3" ]; then
  206     mkdir -p /tmp/floppy/$3 || exit
  207 fi
  208     dd if=/dev/$4 of=/tmp/floppy/$3/$4.mbr bs=512 count=1 1>/dev/null 2>/tmp/error && \
  209 if [ ! $? = 0 ]; then
  210   umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy 
  211   cat /tmp/error
  212   echo
  213   echo "ERROR! There was a problem!"
  214   rm -f /tmp/error
  215   exit
  216 else
  217     echo -e "\nCopied MBR from \`/dev/$4' to \`$3/$4.mbr' on /dev/$5."
  218   if [ -x "`type -path fdisk`" ]; then
  219     pt=`fdisk -l /dev/$4 | grep "^/dev/$4" | awk '{print $1}'` 
  220   for p in $pt ; do
  221     dev=`echo $p | cut -b 6-10`
  222     dd if=/dev/$dev of=/tmp/floppy/$3/$dev.bs bs=512 count=1 1>/dev/null 2>/dev/null && \
  223     echo "Copied boot sector from \`/dev/$dev' to \`$3/$dev.bs' on /dev/$5."
  224   done
  225     fdisk_mbr $4 $3
  226   fi
  227     sfdisk_mbr $4 $3
  228 fi
  229   umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy
  230   echo
  231   echo "Read $3/mbr-$4.txt on /dev/$5."
  232   echo "Done!"
  233   exit
  234 else
  235   echo
  236   echo "$0 $1 $2 $3 $4 $5"
  237   echo "ERROR! I can't find device \`/dev/$4' and/or \`/dev/$5'."
  238   exit
  239 fi
  240 
  241 elif [ "$1" = "-r" -a "$2" = "-d" -a -n "$3" -a -n "$4" -a -n "$5" ]; then
  242 if [ -b "/dev/$4" -a -b "/dev/$5" ]; then
  243   umount /tmp/floppy 2>/dev/null
  244   umount /dev/$5 2>/dev/null
  245 if echo $4 | cut -b 4 | grep -q "[1-9]" ; then
  246   mbr=$4.bs
  247 else
  248   mbr=$4.mbr
  249 fi
  250   mkdir -p /tmp/floppy 2>/dev/null
  251 if ! mount -o ro /dev/$5 /tmp/floppy 1>/dev/null 2>/dev/null ; then 
  252   echo
  253   echo "ERROR! I can't mount device \`/dev/$5'."
  254   exit
  255 fi  
  256 if [ ! -s /tmp/floppy/$3/$mbr ]; then
  257   umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy 
  258   echo
  259   echo "ERROR! There's no \`$3/$mbr' on \`/dev/$5'."
  260   exit
  261 fi
  262   if [ "$mbr" = "$4.mbr" ]; then
  263   echo
  264   echo "Restoring \`/dev/$4' from MBR backup \`$4.mbr'."
  265   echo
  266   echo "Do you just want to restore the boot sector (first 446 bytes), or the" 
  267   echo "partition table (last 64 bytes), or the whole MBR (512 bytes)?"
  268   echo
  269   echo "Answer 1 for boot sector (first 446 bytes)"
  270   echo "Answer 2 for the whole MBR (512 bytes)"
  271   echo "Answer 3 for just the partition table (last 64 bytes)"
  272   echo
  273   echo -n "Answer 1, 2, or 3 "
  274   read ans
  275 if [ "$ans" = "1" ]; then
  276   dd if=/tmp/floppy/$3/$mbr of=/dev/$4 bs=446 count=1 1>/dev/null 2>/tmp/error
  277 if [ ! $? = 0 ]; then
  278   umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy 
  279   cat /tmp/error
  280   echo
  281   echo "ERROR! There was a problem."
  282   rm -f /tmp/error
  283   exit
  284 fi
  285   MBR="MBR boot sector (first 446 bytes)"
  286 elif [ "$ans" = "2" ]; then
  287   dd if=/tmp/floppy/$3/$mbr of=/dev/$4 bs=512 count=1 1>/dev/null 2>/tmp/error
  288 if [ ! $? = 0 ]; then
  289   umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy 
  290   cat /tmp/error
  291   echo
  292   echo "ERROR! There was a problem."
  293   rm -f /tmp/error
  294   exit
  295 fi
  296   MBR="whole MBR (512 bytes)"
  297 elif [ "$ans" = "3" ]; then
  298 if [ -x "`type -path sfdisk`" -a -s "/tmp/floppy/$3/sfdisk.$4" ]; then
  299   sfdisk /dev/$4 < /tmp/floppy/$3/sfdisk.$4 1>/dev/null 2>/tmp/error
  300 else
  301   dd if=/tmp/floppy/$3/$mbr of=/dev/$4 bs=1 count=64 skip=446 seek=446 1>/dev/null 2>/tmp/error
  302 fi
  303 if [ ! $? = 0 ]; then
  304   umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy 
  305   cat /tmp/error
  306   echo
  307   echo "ERROR! There was a problem!"
  308   rm -f /tmp/error
  309   exit
  310 fi
  311   MBR="MBR partition table (last 64 bytes)"
  312 else
  313   umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy
  314   echo
  315   echo "Cancelled!"
  316   exit
  317 fi
  318 else
  319   dd if=/tmp/floppy/$3/$mbr of=/dev/$4 bs=512 count=1 1>/dev/null 2>/tmp/error
  320 if [ ! $? = 0 ]; then
  321   umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy 
  322   cat /tmp/error
  323   echo
  324   echo "ERROR! There was a problem."
  325   rm -f /tmp/error
  326   exit
  327 fi
  328   MBR="boot sector ($mbr)"
  329 fi
  330   umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy
  331   blockdev --rereadpt /dev/$4 2>/dev/null
  332   echo
  333   echo "Copied $MBR to \`/dev/$4'."
  334   echo "Done!"
  335   exit
  336 else
  337   echo
  338   echo "$0 $1 $2 $3 $4 $5"
  339   echo "ERROR! I can't find device \`/dev/$4' and/or \`/dev/$5'."
  340   exit
  341 fi
  342 else
  343   help
  344 fi