Skip to content

Instantly share code, notes, and snippets.

@sudoalx
Created March 4, 2023 20:26
Show Gist options
  • Save sudoalx/a80f02e98fbadfe67aa3e116e2b1241a to your computer and use it in GitHub Desktop.
Save sudoalx/a80f02e98fbadfe67aa3e116e2b1241a to your computer and use it in GitHub Desktop.
.Bash script to restore the UEFI boot entries from the backup file.
#!/bin/bash
# Use this script to restore boot entries from a backup made by executing the command: sudo efibootmgr -v > boot_entries.txt
# Remember to change the name of the backup file if you named it something else.
# Path to the backup file containing the UEFI boot entries
BACKUP_FILE="bootentries.txt"
# Command to restore each boot entry from the backup file
RESTORE_CMD="sudo efibootmgr -c -w -d /dev/sda -p 1 -L \"{LABEL}\" -l \"{LOADER}\" -u {OPTIONS}"
# Read the backup file line by line and restore each boot entry
while read line; do
# Skip lines that don't contain boot entries
if [[ ! "$line" =~ ^Boot[0-9a-fA-F]+\* ]]; then
continue
fi
# Extract the boot entry ID, label, loader path, and options from the line
id=$(echo "$line" | cut -d "*" -f 1 | cut -d " " -f 2)
label=$(echo "$line" | sed -n 's/^.*Boot\([0-9a-fA-F]\{4\}\)\*.*/\1/p')
loader=$(echo "$line" | sed -n 's/^.*File\\\(.*\)\)/\1/p' | sed 's/\\/\//g' | cut -d "{" -f 1)
options=$(echo "$line" | sed -n 's/^.*options:\s*\(.*\)/\1/p')
# Build the command to restore the boot entry and execute it
restore_cmd=$(echo "$RESTORE_CMD" | sed "s/{LABEL}/$label/" | sed "s/{LOADER}/$loader/" | sed "s/{OPTIONS}/$options/" | sed "s/{ID}/$id/")
eval "$restore_cmd"
done < "$BACKUP_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment