Maintaining reliable backups of your storage layout is a vital responsibility in Linux system administration. When deploying high-capacity solid-state drives, configuring RAID arrays, or provisioning identical bare-metal server nodes, reconstructing complex GUID Partition Table (GPT) layouts manually using interactive utilities like gdisk or fdisk introduces human error and substantial downtime. The command-line utility sgdisk—the script-friendly brother of gdisk—provides rapid, automated manipulation, exact binary extraction, and cloning of GPT disk structures.
Because GPT stores partition data across both a primary header at the start of the drive and a secondary backup header at the end of the disk, executing a low-level binary backup safeguards your systems against corrupted partition records, unaligned sector configurations, and accidental partition table wipes. This tutorial guides you through installing sgdisk, creating binary and textual partition table backups, restoring partitions after disk failure, and cloning identical partition schemes to fresh secondary drives.
Understanding the Advantages of sgdisk over Interactive Partition Tools
While classic disk partitioning utilities require navigating terminal menus and confirming prompts interactively, sgdisk is engineered specifically for non-interactive automation, shell scripts, and rapid command execution:
- Non-Interactive Automation: You can script complete disk partition layouts, GUID assignments, and filesystem type tags without pipe redirection workarounds or manual keyboard input.
- Exact Binary Snapshots: Using the
-bparameter,sgdisksaves the primary GPT header, partition array, and protective MBR into a compact binary file (typically around 34 sectors or roughly 17KB in size), making it ideal to bundle with filesystem backups or split archives. For tips on managing backup payload sizes, see our guide on how to use the split command in Linux to divide large files. - Cross-Drive Replication: When building multi-drive software RAID setups (such as
mdadm) or configuring ZFS pools,sgdiskallows you to duplicate partition boundaries across identically sized disks within seconds. - Unique GUID Randomisation: When cloning partition tables across multiple physical drives, simple bit-for-bit duplication produces duplicate disk and partition UUIDs, which causes catastrophic mounting collisions in
systemd. Thesgdiskutility includes built-in commands to randomise all identifiers instantly.
Installing sgdisk on Major Linux Distributions
The sgdisk utility is distributed as part of the gdisk (GPT fdisk) package across all primary package managers. Install it using the command appropriate for your system:
On Ubuntu, Debian, and Debian-derived platforms:
sudo apt update && sudo apt install -y gdisk
On RHEL, Rocky Linux, AlmaLinux, and Fedora:
sudo dnf install -y gdisk
On Arch Linux and derivatives:
sudo pacman -S --noconfirm gptfdisk
Verify that the tool is operational and inspect your current block devices using lsblk to identify your target storage drive:
lsblk -e7
Backing Up a GPT Partition Table to a Binary File
To capture a comprehensive binary snapshot of your GPT structure, invoke sgdisk with the -b (backup) flag, followed by your target backup file destination and the source disk device identifier (such as /dev/nvme0n1 or /dev/sda):
sudo sgdisk -b=nvme0n1_gpt_backup.bin /dev/nvme0n1
This command extracts:
- The Protective Master Boot Record (PMBR) stored on sector 0.
- The primary GPT header stored on sector 1.
- The primary partition entry table (sectors 2 through 33).
You can verify that the backup completed successfully by checking its file size and metadata with ls -lh:
ls -lh nvme0n1_gpt_backup.bin
Store this backup file on an external USB volume, a secure remote server, or an isolated backup repository. Maintaining clean file deletion hygiene around temporary administrative scripts is equally valuable; see how to safely delete and restore files in Linux terminal using trash-cli.
Creating a Human-Readable Text Backup
In addition to binary dumps, capturing a human-readable text printout of your partition boundaries, starting/ending sectors, and partition type GUIDs is strongly recommended for administrative auditing:
sudo sgdisk -p /dev/nvme0n1 > nvme0n1_partition_layout.txt
To inspect the saved layout at any time, run cat nvme0n1_partition_layout.txt. The output outlines the total drive capacity, sector size (typically 512 bytes or 4096 bytes), partition numbers, flags, and filesystem category codes (such as EF00 for EFI System Partition or 8300 for Linux filesystem).
Restoring a GPT Partition Table from Backup
If a storage drive experiences accidental partition table loss or sector corruption, you can restore its original layout seamlessly using the -l (load) flag. Note that restoring a partition table overwrites any existing partition map on the target drive:
sudo sgdisk -l=nvme0n1_gpt_backup.bin /dev/nvme0n1
Once the restore operation finishes, notify the Linux kernel of the partition changes so that block device files (like /dev/nvme0n1p1, /dev/nvme0n1p2) are populated without requiring an immediate system reboot:
sudo partprobe /dev/nvme0n1
Cloning a Partition Table to a Secondary Disk
When provisioning a second drive (such as /dev/sdb) with the exact partition geometry of an existing drive (such as /dev/sda), execute a direct transfer using the -R (replicate) switch:
sudo sgdisk -R=/dev/sdb /dev/sda
This command replicates every partition boundary, alignment offset, and partition type from /dev/sda directly onto /dev/sdb.
Crucial Step: Randomising GUIDs
After replicating a partition table, the new drive shares identical disk GUIDs and partition unique GUIDs with the original drive. If both drives remain connected to the system, Linux mount processes using UUID designations will face ambiguity. Immediately randomise the identifiers on the new drive with the -G (randomise GUIDs) parameter:
sudo sgdisk -G /dev/sdb
Finally, inform the kernel of the new layout:
sudo partprobe /dev/sdb
Verify the new partition boundaries and distinct UUIDs across both drives by running:
sudo blkid /dev/sda* /dev/sdb*