How do I copy MBR from one hard disk to another hard disk under Debian Linux?
To copy MBR simply use the dd command. dd command works under all Linux distros and other UNIX like operating systems too. A master boot record (MBR) is the 512-byte boot sector that is the first sector of a partitioned data storage device of a hard disk.
MBR Total Size
446 + 64 + 2 = 512
- 446 bytes – Bootstrap.
- 64 bytes – Partition table.
- 2 bytes – Signature.
512 vs 446 Bytes
- Use 446 bytes to overwrite or restore your /dev/XYZ MBR boot code only with the contents of $mbr.backup.file.
- Use 512 bytes to overwrite or restore your /dev/XYZ the full MBR (which contains both boot code and the drive’s partition table) with the contents of $mbr.backup.file.
dd command to copy MBR (identically sized partitions only)
Type dd command as follows:
dd if=/dev/sda of=/dev/sdb bs=512 count=1
Above command will copy 512 bytes (MBR) from sda to sdb disk. This will only work if both discs have identically sized partitions.
dd command for two discs with different size partitions
# dd if=/dev/sda of=/tmp/mbrsda.bak bs=512 count=1
Now to restore the image to any sdb:
# dd if=/tmp/mbrsda.bak of=/dev/sdb bs=446 count=1
The above commands will preserve the partitioning schema.
Linux sfdisk Command Example
Linux sfdisk command can make a backup of the primary and extended partition table as follows. It creates a file that can be read in a text editor, or this file can be used by sfdisk to restore the primary/extended partition table. To back up the partition table /dev/sda, enter:
# sfdisk -d /dev/sda > /tmp/sda.bak
To restore, enter:
# sfdisk /dev/sda < /tmp/sda.bak
The above command will restore extended partitions.
Task: Backup MBR and Extended Partitions Schema
Backup /dev/sda MBR, enter:
# dd if=/dev/sda of=/tmp/backup-sda.mbr bs=512 count=1
Next, backup entries of the extended partitions:
# sfdisk -d /dev/sda > /tmp/backup-sda.sfdisk
Copy /tmp/backup-sda.sfdisk and /tmp/backup-sda.mbr to USB pen or somewhere else safe over the network based nas server.
Task: Restore MBR and Extended Partitions Schema
To restore the MBR and the extended partitions copy backup files from backup media and enter:
# dd if=backup-sda.mbr of=/dev/sda
# sfdisk /dev/sda < backup-sda.sfdisk