Backup Restore MBR and Partition Table

De Wiki do Bernardino
Ir para: navegação, pesquisa

Backup

To backup the Disk partition boot (MBR) and partition table info use:

dd if=/dev/sdX of=/mbr_backup bs=512 count=1

Where sdX is the disk you want do the backup and mbr_backup the file who have the backup content.

Restore

To restore previous backup, use:

dd if=/mbr_backup of=/dev/sdX bs=512 count=1

ATTENTION - use this command with careful, because you may loose all your data.

Backup with info

Backup MBR, Partition info and Disk Info:

#!/bin/bash
#Created by jose@bernardino.org

[ $# -gt 0 ] || { echo "usage: $0 /dev/disk" ;  exit 1;}
[ -b $1 ] || { echo "ERROR: Disk $1 dont exist!..." ;  exit 2;}
SERIAL=`hdparm -i $1 | grep SerialNo | awk -F' ' '{print $4 }' | awk -F'=' '{print $2 }'`
echo SerialNo=$SERIAL
dd if=$1 of=~/disk_"$SERIAL"_mbr_backup bs=512 count=1
hdparm -i $1 > ~/disk_"$SERIAL".info
fdisk -l  $1 >> ~/disk_"$SERIAL".info
echo -e "\nContents:" >> ~/disk_"$SERIAL".info
fdisk -l /dev/sda | grep Linux$ > /tmp/fdi$$
while read Linha
do
  Part=`echo $Linha | awk -F' ' '{print $1 }'`
  UUID=`blkid $Part`
  echo Partition $UUID  >>  ~/disk_"$SERIAL".info
done < /tmp/fdi$$
rm /tmp/fdi$$
MBRFILE=`echo ~/disk_"$SERIAL"_mbr_backup`
echo -e "\nTo restore MBR and Partition info do:\n  dd if=$MBRFILE of=$1 bs=512 count=1" >>  ~/disk_"$SERIAL".info

This create in the root directory two files, one the MBR file and another with info about the disk.