Properly backup and restore partition table in Linux [100% Working]


How To, Linux

In my last article I gave you an overview on Public key Infrastructure (PKI) and shared the steps to generate a self signed certificate using openssl in Linux. Next let me show you the steps to clone partition table from one disk to another to repair or backup and restore partition table in Linux. The steps from this article to backup and restore partition table are performed on centOS 7 and can also be executed on RHEL 7 Linux node.

Before going for any sort of backup or restore procedure for partition table, you must be aware of different types of partition scheme i.e. GPT vs MBR.

NOTE:
On GPT partitions you cannot perform such backup and restore of partition table.

 2 ways to backup and restore partition table in Linux (CentOS / RHEL 7)

 

Clone and Restore partition table using 'sfdisk' command

Backup Partition Table

Run the command below to backup the partition table on device /dev/sda to /root/partition-sda.img:

[root@node2 ~]# sfdisk -d /dev/sda > /root/partition-sda.img
[root@node2 ~]# ls -l /root/partition-sda.img
-rw-r--r-- 1 root root 259 Apr 18 14:52 /root/partition-sda.img

As you can see the output is a ASCII type text file.

[root@node2 ~]# file /root/partition-sda.img
/root/partition-sda.img: ASCII text

Content of this file

[root@node2 ~]# cat /root/partition-sda.img
# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=     2048, size=  1048576, Id=83, bootable
/dev/sda2 : start=  1050624, size= 57688064, Id=8e
/dev/sda3 : start=        0, size=        0, Id= 0
/dev/sda4 : start=        0, size=        0, Id= 0

 

Restore Partition Table

You should then copy /root/partition-sda.img to some other storage, for example a portable USB disk.

If the partition is damaged and needs to be restored from backup, please connect your USB disk to the server and boot the server with installation media and enter rescue mode.

Do not mount the root partition at this time - select "Skip" when the system asks if you want to mount the root partition on /mnt/sysimage.

Create a temporary directory, for example /mnt/temp and mount the filesystem of your USB device which contains your backup.

For example:

# mkdir /mnt/temp
# mount /dev/sdb1 /mnt/temp/
# cd /mnt/temp/

where /dev/sdb1 is the file system of the USB disk. After that, run:

# sfdisk /dev/sda < /mnt/temp/partition-sda.img

To verify that the partition table has been restored, run:

# fdisk -l /dev/sda

 

Clone and Restore partition table using "dd" command

The MBR (Master Boot Record) occupies the first 446 bytes of the disk while the partition table occupies the next 64 bytes. We can use "dd" to dump the range from 447 - 510 bytes in the first sector.

 

Backup Partition Table

For example, if the harddisk is /dev/sda, then run the command below:

# dd if=/dev/sda of=/root/partition-sda.img bs=1 count=64 skip=446

 

Restore Partition Table

You should then copy /root/partition-sda.img to some other storage, for example a portable USB disk.

If the partition is damaged and needs to be restored from backup, please connect your USB disk to the server and boot the server with installation media and enter rescue mode.

Do not mount the root partition at this time - select "Skip" when the system asks if you want to mount the root partition on /mnt/sysimage.

Create a temporary directory, for example /mnt/temp and mount the filesystem of your USB device which contains your backup.

For example:

# mkdir /mnt/temp
# mount /dev/sdb1 /mnt/temp/
# cd /mnt/temp/

where /dev/sdb1 is the file system of the USB disk. After that, run:

# dd if=/mnt/temp/partition-sda.img of=/dev/sda bs=1 count=64 seek=446

To verify that the partition table has been restored, run:

# fdisk -l /dev/sda
NOTE:
The methods above assume that the hard drive has no bad blocks or any type of physical problems. The two methods only support MS-DOS partition tables, not GPT partition tables.

 

Lastly I hope the steps from the article to backup and restore partition table using sfdisk and dd command on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

 

References:
Red Hat Knowledgebase

 

Related keywords: linux clone partition table, linux repair partition table, sfdisk

Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

2 thoughts on “Properly backup and restore partition table in Linux [100% Working]”

  1. “On GPT partitions you cannot perform such backup and restore of partition table.”

    My understanding is the gdisk does precisely this. And that sfdisk, the current version, also supports this. So why use fdisk?

    Reply
    • Thank you for highlighting this, let me verify and may be I can either update this article or add another one for GPT partitions as well

      Reply

Leave a Comment