≡ Menu

How to backup and restore a partition table on Linux

Is it possible to just backup my Linux partition table and restore it when required? How do I restore partition table if it get deleted by mistake on Linux operating system? How do I backup and restore a GPT partition table on Linux?

Yes, you need to backup both data and disk’s partition table. This is useful for:

  1. Replicating disk’s partition for Linux software RAID in case of disk failure.
  2. Dealing with corrupted partition table
  3. Dealing with deleted partition table by mistake as data may still exists on the disk and can be accessed again with a correct partition table.

How to see my current partition table

Type the following command to list partitions of each device:
# fdisk -l
# fdisk -l /dev/sda

# sfdisk -l
# sfdisk -l /dev/sda

Sample outputs:

Fig.01: Linux List Disk Partitions Command

Fig.01: Linux List Disk Partitions Command

How do I back-up Linux partition with sfdisk command?

To backup /dev/sda partition table, enter:
# sfdisk -d /dev/sda > sda.partition.table.12-30-2015.txt
Copy sda.partition.table.12-30-2015.txt to NFS mounted share or a USB pen drive.

How do I restore the Linux partition with sfdisk command to the disk?

# sfdisk /dev/sda < sda.partition.table.12-30-2015.txt # sfdisk /dev/sda < /path/to/usb/pen/sda.partition.table.12-30-2015.txt
# sfdisk -f /dev/sda < /media/usb/sda.partition.table.12-30-2015.txt

How do I replicate a disk partition table from /dev/sda to /dev/sdd with the same geometry?

This is useful for Linux based software RAID device. Say you replaced /dev/sdd and you want to rebuild the Linux software RAID array. The first step is to replicate a partition table from a disk to another disk:
# sfdisk -d /dev/sda | sfdisk -f /dev/sdd
Now, run the following to verify that both hard drives have the same partitioning:
# fdisk -l /dev/sda
# fdisk -l /dev/sdd

Finally, use mdadm to manage and rebuild your RAID device:
# mdadm --manage /dev/mdX --add /dev/sdd1
# mdadm --manage /dev/mdX --add /dev/sdd2
# mdadm --manage /dev/mdX --add /dev/sddN

Use the following command to see raid array sync progress:
# watch cat /proc/mdstat

A note about disks with GPT (GUID Partition Table)

From the man page:

GPT fdisk (aka gdisk) is a text-mode menu-driven program for creation and manipulation of partition tables. It will automatically convert an old-style Master Boot Record (MBR) partition table or BSD disklabel stored without an MBR carrier partition to the newer Globally Unique Identifier (GUID) Partition Table (GPT) format, or will load a GUID partition table. When used with the -l command-line option, the program displays the current partition table and then exits.

Let us install gdisk on a Debian or Ubuntu Linux using apt-get command:
$ sudo apt-get install gdisk
Sample outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
The following NEW packages will be installed:
  gdisk libicu52
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,937 kB of archives.
After this operation, 28.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu/ trusty-updates/main gdisk amd64 0.8.8-1ubuntu0.1 [185 kB]
Get:2 http://security.ubuntu.com/ubuntu/ trusty-security/main libicu52 amd64 52.1-3ubuntu0.4 [6,752 kB]
Fetched 6,937 kB in 24s (284 kB/s)                                             
Selecting previously unselected package libicu52:amd64.
(Reading database ... 65753 files and directories currently installed.)
Preparing to unpack .../libicu52_52.1-3ubuntu0.4_amd64.deb ...
Unpacking libicu52:amd64 (52.1-3ubuntu0.4) ...
Selecting previously unselected package gdisk.
Preparing to unpack .../gdisk_0.8.8-1ubuntu0.1_amd64.deb ...
Unpacking gdisk (0.8.8-1ubuntu0.1) ...
Processing triggers for man-db ( ...
Setting up libicu52:amd64 (52.1-3ubuntu0.4) ...
Setting up gdisk (0.8.8-1ubuntu0.1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...

RHEL/CentOS user use the yum command to install the same:
# yum install gdisk
Fedora Linux user use the dnf command to install the same:
$ sudo dnf install gdisk
OpenSUSE Linux user use the yast command to install the same:
# yast install gdisk
Please note that sgdisk command works with both Linux and Unix-like system.

How do I backup /dev/sda GPT partition table to a file?

To save partition data to a backup file called /root/sda_partition_table_12_30_2015, run:
# sgdisk --backup={/path/to/file} {/dev/device/here}
# sgdisk --backup=/root/sda_partition_table_12_30_2015 /dev/sda

How do I restore GPT partition table from a file?

To restore the backup use:
# sgdisk --load-backup={/path/to/file} {/dev/device/here}
# sgdisk --load-backup=/media/usb/sda_partition_table_12_30_2015 /dev/sda

How do I replicate GPT partition scheme from /dev/sda to /dev/sdd (RAID array rebuild)?

The syntax is:
# sgdisk -R {SECOND-DEVICE-NAME-HERE} /dev/sda
# sgdisk -R /dev/sdd /dev/sda

To randomize the GUID on the /dev/sdd, enter:
# sgdisk -G /dev/sdd
Finally, verify that both hard drives have the same partitioning schema:
# sgdisk -p /dev/sda
# sgdisk -p /dev/sdd

Finally, use mdadm command as explained earlier to rebuild the RAID array on Linux.

Share this tutorial on:
{ 1 comment… add one }
Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , , ,