Added backup of partition tables for use in restore

This commit is contained in:
Eric Renfro 2024-11-11 11:40:00 -05:00
parent a967445005
commit 4a9c6f0894
Signed by: psi-jack
SSH key fingerprint: SHA256:1TKB8Z257L8EHK8GWNxKgMhD8a+FAR+f+j3nnlcuNVM
2 changed files with 86 additions and 0 deletions

84
scripts/functions.sh Normal file
View file

@ -0,0 +1,84 @@
#!/bin/bash
###########################################################
### Utility & Common Functions
###########################################################
function is_bin_in_path {
builtin type -P "$1" &>/dev/null
}
###########################################################
### BACKUP and RESTORE FUNCTIONS
###########################################################
#dumpPartitions() {
backup_partitions() {
local rootPart rootDisk
rootPart="$(findmnt -no SOURCE / | sed -E 's/\[.*\]$//')"
rootDisk="/dev/$(lsblk -no pkname "$rootPart" | head -n1)"
sfdisk --dump "$rootDisk" > "${restoreDir}/sfdisk.dump"
blkid -o export "$rootPart" > "${restoreDir}/blkid.dump"
btrfs subvolume list -p / > "${restoreDir}/btrfs.dump"
mount | grep "$rootPart" > "${restoreDir}/mounts.dump"
}
#restorePartitions() {
restore_partitions() {
local rootPart rootDisk
rootPart="$1"
rootDisk="$2"
if [[ -z "$rootDisk" || -z "$rootPart" ]]; then
echo "ERROR, restorePartitions not supplied with rootDisk and/or rootPartition"
exit 200
fi
# Restore Partition Table (without modification)
sfdisk "$rootDisk" < "${restoreDir}/sfdisk.dump"
# Restore UUIDs
while read -r line; do
if [[ $line == UUID=* ]]; then
eval "$line"
tune2fs "$rootPart" -U "$UUID"
fi
done < "${restoreDir}/blkid.dump"
echo "Partition table and UUID information have been restored."
}
#restoreBtrFSSubvolumes() {
restore_btrfs_subvolumes() {
local rootBase subvolID subvolPath
rootBase="$1"
while read -r line; do
# Extract the subvolume ID and Path
subvolID="$(echo "$line" | awk '{print $2}')"
subvolPath="$(echo "$line" | awk '{print $NF}')"
# Restore the subvolume
btrfs subvolume create "${rootBase}/${subvolPath}"
done < "${restoreDir}/btrfs.dump"
}
#restoreBtrFSMounts() {
restore_btfs_mounts() {
local rootBase mountSource mountDest
rootBase="$1"
while read -r line; do
# Extract mount source and destination
mountSource="$(echo "$line" | awk '{print $1}')"
mountDest="$(echo "$line" | awk '{print $3}')"
# Mount the subvolume
mount "$mountSource" "${rootBase}/${mountDest}"
done < "$restoreDir/mounts.dump"
}

View file

@ -5,6 +5,7 @@ restoreDir="/etc/restore"
kernel="$(uname -s)" kernel="$(uname -s)"
scriptPath="$(dirname "$(readlink -f "$0")")" scriptPath="$(dirname "$(readlink -f "$0")")"
source "${scriptPath}/functions.sh"
if [[ "$kernel" == "Darwin" ]]; then if [[ "$kernel" == "Darwin" ]]; then
restoreDir="${HOME}/restore" restoreDir="${HOME}/restore"
@ -78,6 +79,7 @@ hook_fail() {
hook_after() { hook_after() {
checkOS || exit 200 checkOS || exit 200
backup_partitions || exit 200
runOsHook after runOsHook after
} }