Compare commits

...

2 commits

3 changed files with 131 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"
}

45
scripts/mount-btrfs Executable file
View file

@ -0,0 +1,45 @@
#!/bin/bash
restoreDir="/etc/restore"
scriptPath="$(dirname "$(readlink -f "$0")")"
source "${scriptPath}/functions.sh"
hook_before() {
local btrfsPartition
btrfsPartition="$(findmnt -oSOURCE -rnv /)"
if [[ ! -d "/.btrfs" ]]; then
mkdir /.btrfs
fi
if ! mount | grep '\.btrfs' &>/dev/null; then
mount "$btrfsPartition" -o subvolid=5 /.btrfs || exit 200
fi
}
hook_fail() {
if mount | grep '\.btrfs' &>/dev/null; then
umount /.brtfs || exit 200
fi
}
hook_after() {
:
}
hook_final() {
if mount | grep '\.btrfs' &>/dev/null; then
umount /.brtfs || exit 200
fi
}
case "$1" in
before) hook_before || exit $?;;
after) hook_after || exit $?;;
fail) hook_final || exit $?;;
finally) hook_final || exit $?;;
esac

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
} }