Partition Table Backup

Many of us are doing backups of all kinds of data: from regular files, databases, to full partitions or hard drives. What I have noticed that very few peoples even think about the partition table. Given the importance of the partition table I would suggest to have a backup of it, in case you will have a corrupted partition table, if you made a change by mistake or even if that gets deleted somehow (by mistake or intentionally). You still have the data on the disk but without recreating the correct partition table it will not be very easy to get the data back. Anyway this is very easy to do, so it is better to have it on hand, than regret that you have not done this when needed.

We can get a quick look on all the existing partitions on all the available hard drives with fdisk using the -l switch without any other parameter:

fdisk -l
Disk /dev/sda: 74.3 GB, 74355769344 bytes
255 heads, 63 sectors/track, 9039 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1          16      128488+  83  Linux
/dev/sda2              17         259     1951897+  82  Linux swap / Solaris
/dev/sda3             260        2083    14651280   83  Linux
/dev/sda4            2084        9039    55874070   83  Linux

Disk /dev/sdb: 73.4 GB, 73407820800 bytes
255 heads, 63 sectors/track, 8924 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        8924    71681998+  83  Linux

If we add to the command line the particular device that corresponds to a hard disk, like /dev/sdb we get the same result only for that device:

fdisk -l /dev/sdb

Disk /dev/sdb: 73.4 GB, 73407820800 bytes
255 heads, 63 sectors/track, 8924 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        8924    71681998+  83  Linux

Of course someone might use only this information to recreate the partition table by manually creating the same partitions with the same start/end blocks (in case you want to do that you just have to save the above output in a file so you have that information on hand when needed), but there is a simpler way using sfdisk:

sfdisk -d /dev/sda
# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=       63, size=   256977, Id=83
/dev/sda2 : start=   257040, size=  3903795, Id=82
/dev/sda3 : start=  4160835, size= 29302560, Id=83
/dev/sda4 : start= 33463395, size=111748140, Id=83

Using sfdisk with the -d option we can get a dump of the current partition table in a regular file, and if needed we can restore it from that file:

sfdisk -d /dev/sda > sda_table

and to restore the partition table:

sfdisk /dev/sda < sda_table

Regardless on the method used, having a backup of the partition table might be handy. ;)

One more tip: another usage of sfdisk with -d is to create an identical partition table from another hard disk. In this example we are partitioning sdb with the exact same partitions as sda:

sfdisk -d /dev/sda | sfdisk /dev/sdb

of course this can be handy when the hard drives are identical, in raid1 setups, etc.

comments powered by Disqus