Monday, May 18, 2009

Using sfdisk to backup and restore partition tables

I needed to backup a partition table and restore it to a new hard-drive, I tried a little bit of googling using parted with my search terms because I had forgotten that it was actually sfdisk I needed/wanted (from previous similar endeavours), so after coming up with only a few posts that poorly inform readers that copying the first 512bytes of a drive will get them their entire partition table in case of disaster, I finally came across something that mentioned sfdisk, which twigged my memory and I thusly changed my google search to include sfdisk as one of my search terms instead of parted. I came upon the article/post that this post's Title links to (also here), which nicely points out a good method of backing up your hard-drive's partition tables properly using sfdisk, which I will quote the relevant snippets here with a couple pieces edited for clarity or additional information:

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 to the same drive (or change device name for another destination drive):

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 (ie. copying partition table from SDA to SDB):

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

of course this can be handy when the hard drives are identical, in raid1 setups, etc. (actually, they don't even have to be identical, but the destination drive needs to be the same size or larger (exactly the same size, not "sales pitch gigabytes size")).

End Quote.

So there you have it (and I have it for my permanent memory now!).

Sidenote: copying the first 512bytes of your drive may copy many partition tables, but only if they do not include extended partitions, since the main/base partition table actually uses a reference that links to another part of the drive where it stores the rest of the extended partition table data. Don't ask how I know this, other then it was a late night, and interesting things were learned using hexedit! :) ask Jaymes if one must know! :P

No comments: