Linux backup partition table

How to backup and restore a partition table on Linux

How to see my current partition table

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

OR
# sfdisk -l
# sfdisk -l /dev/sda
Sample outputs:

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
OR
# sfdisk -f /dev/sda

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:

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.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard
Читайте также:  Обновление механизма обнаружения угроз для windows defender antivirus kb915597 не устанавливается

Join Patreon

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=
# 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=
# 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 /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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

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

Table of Contents

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 copy partition table from one disk to another or backup and restore partition table in Linux. The steps from this article to backup and restore partition table are performed on cent)S 7 and can also be executed on RHEL 7 Linux node.

Restore partition table using ‘sfdisk’ command

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

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

Content of this file

  • 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.

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

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

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.

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

  • 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.

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

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

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.

Читайте также:  Общий аккаунт для windows

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

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

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

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

“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?

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

Источник

fdisk

util-linux fdisk is a dialogue-driven command-line utility that creates and manipulates partition tables and partitions on a hard disk. Hard disks are divided into partitions and this division is described in the partition table.

This article covers fdisk(8) and its related sfdisk(8) utility.

Contents

Installation

fdisk and its associated utilities are provided by the util-linux package, which is a dependency of the base meta package.

List partitions

To list partition tables and partitions on a device, you can run the following, where device is a name like /dev/sda :

Backup and restore partition table

Before making changes to a hard disk, you may want to backup the partition table and partition scheme of the drive. You can also use a backup to copy the same partition layout to numerous drives.

For both GPT and MBR you can use sfdisk to save the partition layout of your device to a file with the -d / —dump option. Run the following command for device /dev/sda :

The file should look something like this for a single ext4 partition that is 1 GiB in size:

To later restore this layout you can run:

Create a partition table and partitions

The first step to partitioning a disk is making a partition table. After that, the actual partitions are created according to the desired partition scheme. See the partition table article to help decide whether to use MBR or GPT.

Before beginning, you may wish to backup your current partition table and scheme.

Recent versions of fdisk have abandoned the deprecated system of using cylinders as the default display unit, as well as MS-DOS compatibility by default. fdisk automatically aligns all partitions to 2048 sectors, or 1 MiB, which should work for all EBS sizes that are known to be used by SSD manufacturers. This means that the default settings will give you proper alignment.

Start fdisk against your drive as root. In this example we are using /dev/sda :

This opens the fdisk dialogue where you can type in commands.

Create new table

To create a new partition table and clear all current partition data type o at the prompt for a MBR partition table or g for a GUID Partition Table (GPT). Skip this step if the table you require has already been created.

Create partitions

This article or section needs expansion.

Create a new partition with the n command. You enter a partition type, partition number, starting sector, and an ending sector.

When prompted, specify the partition type, type p to create a primary partition or e to create an extended one. There may be up to four primary partitions.

The first sector must be specified in absolute terms using sector numbers. The last sector can be specified using the absolute position in sectors or using the + symbol to specify a position relative to the start sector measured in sectors, kibibytes ( K ), mebibytes ( M ), gibibytes ( G ), tebibytes ( T ), or pebibytes ( P ); for instance, setting +2G as the last sector will specify a point 2GiB after the start sector. Pressing the Enter key with no input specifies the default value, which is the start of the largest available block for the start sector and the end of the same block for the end sector.

Читайте также:  Продукты касперского для windows 10

This article or section needs expansion.

See the respective articles for considerations concerning the size and location of these partitions.

Repeat this procedure until you have the partitions you desire.

List partition types

Press l to list available partition types.

Change partition type

Each partition is associated with a Type. Press t to change the type of a partition. The default, Linux filesystem , should be fine for most use.

Make a partition bootable.

You can make the partition bootable by typing a .

Write changes to disk

Write the table to disk and exit via the w command.

Moving partitions

In order to move a partition, you need to have free space available where the partition will be moved. If necessary, you can make room by shrinking your partitions and the filesystems on them. See Parted#Shrinking partitions. To relocate a partition:

Where sectors is the number of sectors to move the partition (the + indicates moving it forward), device is the device that holds the partition, and number is the partition number. Note that if you add a new partition in the middle or at the beginning of your disk, you will likely want to renumber the partitions. See #Sort partitions or the «extra functionality» mode of fdisk.

Tips and tricks

Sort partitions

This applies for when a new partition is created in the space between two partitions or a partition is deleted. /dev/sda is used in this example.

After sorting the partitions if you are not using Persistent block device naming, it might be required to adjust the /etc/fstab and/or the /etc/crypttab configuration files.

Источник

Linux: How to backup hard disk partition table (MBR)

If you don’t want to take any chances with your data, it is recommended that you backup hard disk partition table. Last Friday I was discussing some issues with one of our customer and he pointed out me dd command.

Backup MBR with dd command

dd the old good command which now backup partition tables even writes CDs ;). Backing up partition is nothing but actually backing up MBR (master boot record). The command is as follows for backing up MBR stored on /dev/sdX or /dev/hdX :
# dd if=/dev/sdX of=/tmp/sda-mbr.bin bs=512 count=1

Replace X with actual device name such as /dev/sda.

Now to restore partition table to disk, all you need to do is use dd command:
# dd if= sda-mbr.bin of=/dev/sdX bs=1 count=64 skip=446 seek=446

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

dd command works with Solaris, HP-UX and all other UNIX like operating systems. Read man page of dd for more info.

Источник

Оцените статью