Linux: How to backup hard disk partition table (MBR)

If you don’t want to take any chances with your data, it is recommended that you backup hard disk partition table. Last Friday I was discussing some issues with one of our customer and he pointed out me dd command.

Backup MBR with dd command

dd the old good command which now backup partition tables even writes CDs ;). Backing up partition is nothing but actually backing up MBR (master boot record). The command is as follows for backing up MBR stored on /dev/sdX or /dev/hdX :
# dd if=/dev/sdX of=/tmp/sda-mbr.bin bs=512 count=1

Replace X with actual device name such as /dev/sda.

Now to restore partition table to disk, all you need to do is use dd command:
# dd if= sda-mbr.bin of=/dev/sdX bs=1 count=64 skip=446 seek=446

dd command works with Solaris, HP-UX and all other UNIX like operating systems. Read man page of dd for more info.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
14 comments… add one
  • Ajinkya Oct 25, 2014 @ 18:10

    Actually I run the command dd if=/dev/sda of=/dev/sdb by mistake so I think my partition table is get deleted so may I get help to recover it

    Thanks in advance

  • Marek Dec 7, 2011 @ 11:19

    What about GPT ?

  • Mr_T Sep 13, 2011 @ 19:31

    Victor,
    In your first post, could you clarify the units you are using for mbr and the partiton table and what is the “2 as end” means. Its not intuitive to the uninitiated.

    Thanks

    • brendan Dec 1, 2012 @ 23:42

      He’s talking about bytes. 446 bytes of Bootstrap code, then 4 partition entries x 16 bytes = 64 bytes, then 2 bytes of signature (a 16 bit number = 0101010110101010). This is 55aa in hex and 21930 in decimal.

      • brendan Dec 1, 2012 @ 23:44

        446 + 64 + 2 = 512 bytes 🙂

  • victor Sep 3, 2011 @ 12:45

    i’m sorry, i’m mistake. error coud be if mbr is all zero. then you must add conv=notrunc for no file optimization.
    ej:
    2GB file
    dd if=/dev/zero of=afile bs=1 seek=2GB count=1 (file size in hd<1kb)
    or
    dd if=/dev/zero of=afile bs=1 count=2GB (file size in hd=2gb)

    size must be specific in bytes, 2gb is only for easy read.
    note: a little bs spend more time

  • victor Sep 3, 2011 @ 12:18

    jns, it could be because only one seek is needed.
    there are 446b of MBR, 64b of Partition Table and 2 as end (0x55aa). {= 512b}
    if skip and seek aren’t synonimous, you seek 446 + 446.
    thus, command line must be
    dd if=sda-mbr.bin of=/dev/sdX bs=1 count=64 skip=446
    or
    dd if=sda-mbr.bin of=/dev/sdX bs=1 count=64 seek=446

    • felipe Nov 17, 2015 @ 23:39

      from man dd
      seek=N skip N obs-sized blocks at start of output
      skip=N skip N ibs-sized blocks at start of input

      so, you need seek to start from byte 446 of the disk and skip to ignore first 446 byte of the backup image. skip and seek are, definitely, not synonymous 🙂

  • jns Aug 22, 2011 @ 6:07

    The restore line corrupted my entire partition table…
    beware, i guess…

  • Jack Oct 9, 2010 @ 9:47

    PT,

    You can use sfdisk to backup extended partition table.

    sfdisk -d /dev/sdX > backup-sdX.sf

    Restore:

    sfdisk /dev/sdX < backup-sdX.sf

  • PT Aug 10, 2010 @ 5:14

    This advice does not work if you have an extended partition table.

    • VernDog Jan 26, 2011 @ 16:26

      Wrong! This method DOES WORK with extended , primary partitions. I’ve used it all the time, to both backup & restore. Just remember to backup MBR if you add any changes to partitions.

      • brendan Dec 1, 2012 @ 23:29

        No. He’s right. The MBR lists whether a partition is active, its type, start point and number of sectors within the partition. MBR lists this for a maximum of 4 partitions. This not include logical partitions. They are described within the extended partition. They are not described in the MBR.

  • Wartin Apr 15, 2010 @ 13:37

    dd if= sda-mbr.bin

    it has a blank space after =, it should read

    dd if=sda-mbr.bin of=/dev/sdX bs=1 count=64 skip=446 seek=446

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.