#!/bin/sh
# The number of partitions in the partition table could be varied and uncertain.
# So is the length of backup file.
# Only works in limited situations
# Won't work if any new partition table is added to the partition table later
# Require sudo privilege
# Use the code only after you know what you're doing. Use at your own risk.
# Change the variables in the script if necessary
# Erase the disk with disk manager and reinstall if the restoration of GPT file doesn't work.
# But first back up the data.
# https://askubuntu.com/questions/57908/how-can-i-quickly-copy-a-gpt-partition-scheme-from-one-hard-drive-to-another
# Get the number of partitions in the partition table and save it in a file.
parted -ms /dev/sda print |tail -1|cut -b1 > ./partition_count.txt
# parted -ms sed '$s/:.*//p;d' > ./partition_count.txt
read -r partition_count < ./partition_count.txt
echo "partition_count: " $partition_count
byte_size=$(((128 * $partition_count) + 1024))
echo "byte size: " $byte_size
dd if=/dev/sda of=./GPT_TABLE_BACKUP bs=1 count=$byte_size
echo "Done."