Updated: April 10, 2017
Recently, I scared you some with my story about a wonked Windows 8.1 + UEFI + ATI setup, where I was forced to restore an old image of the operating system to get the box working again. This went well, but we did have to overcome some big snags. And then I got me thinking, how does one go about manually backing up and restoring GPT disks should they ever need to replicate the partition table to another device?
In this short tutorial, I will show you how you can use Linux (like duh) to examine, back up or restore GPT disks, so that if you ever need to restore individual partitions, you do not need to go about recreating everything from scratch. It might be tedious, or worse, you might not fully remember the setup, and what it looks like. So let's take a look, shall we?
Not like the ole setup
We've already covered this topic in the past, but we discussed the classic setup, or shall we say, classique, with ms-dos partition type slash MBR, and in that scenario, it was a simple procedure of using dd to copy a bunch of bytes. Technically, you can do the same with GPT disks, but then you need a little of extra knowledge on how to achieve the same result.
What we will do here is a slightly different approach. We will use gdisk, a tool designed to create and manipulate partition tables of the GPT kind. It works like fdisk, only better. We did see this utility in action in my Ubuntu 14.04 Trusty review and then again in my detailed tutorial on how to setup Windows 8.X and the aforementioned Ubuntu side by side. Tested and tried on another UEFI-powered box, an active production system, my sweet and useful Ultrabook - Asus Vivobook. So this isn't just a theoretical game.
Let me carefully rephrase that. We will actually use sgdisk, which is a version of gdisk designed for non-interactive use. This makes is quite useful, but also dangerous, as you may make critical changes to your system that will haunt you for the rest of your life. Be warned!
Sgdisk in action (backup and restore)
I will demonstrate the procedure with the Lenovo G50 laptop, which currently runs Windows 10 and some six-seven different Linux distros in a complex multi-boot setup. As I'm often making big changes on this system, it makes sense to have a backup of the partition table. But first, let's see what we have.
There you go. Now with this info, you can actually perform a manual backup using dd, but you do need a little bit of calculation to get it right. But that's a separate topic. We will now use sgdisk. If it's not installed, just install the program from the official repo. Then, let's run the tool:
sgdisk --backup=<file> <device>
What do we have here? The --backup flag specifies that we want to backup the partition table. You will need to provide a path for the backup. The <device> field specifies the actual device you wish to backup. Be very careful how you do this. Triple check your commands. Requires root or sudo. For instance:
sgdisk --backup=/mnt/sdc.gpt.header.backup /dev/sdc
And believe it or not, that is the whole magic. Now, the restore part is slightly more complex. There are several options here. Simplest and safest, you can load a file from backup (-l or --load-backup), replicate the partition table to another disk directly (-R), and even randomize GUID (-G). You can always try changes with the pretend option (-P) first.
sgdisk --load-backup=<backup file> <target device>
That's all. For tech-savvy people, this tutorial is a quick one-two-liner. But then, you should respect the process. It is very easy to make mistakes or change the order of your source and target. Tears normally ensue at this point. My warmest recommendation is to have a full disk image, just in case, or at the very least, proper system imaging and data backups. Then, you can slowly work on getting comfortable with sgdisk. Be comfortable making mistakes, if you have to.
Having partition table backups can be useful if you need to change your entire disk, or maybe repair a damaged partition table. This won't give you your data back, but if you have an imaging program, which expects some kind of data structure in place, you can make things simpler and faster to restore this way. It's a nice little extra to make your doomsday activities more pleasant. Well, I guess that's all. Hopefully you've learned something new. Be careful and use sgdisk for GPT backups. We're done.