Skip to content

Instantly share code, notes, and snippets.

@prauscher
Created April 17, 2020 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prauscher/eca17c195f8e0e4d2d263ff09b9a7b7e to your computer and use it in GitHub Desktop.
Save prauscher/eca17c195f8e0e4d2d263ff09b9a7b7e to your computer and use it in GitHub Desktop.
Script to create a bootable system-stick out of a borg backup. <3 EFI
#!/bin/bash
if [ -z "$1" ]; then
echo "Usage: $0 <device> [borg repository]" >&2
echo "WARNING: <device> will receive a new partition table and be erased completely" >&2
exit 2
fi
if [ -e /dev/disk/by-partlabel/howard-esp -o -e /dev/disk/by-partlabel/howard-root ]; then
echo "Failure: Partlabels howard-esp and/or howard-root already taken." >&2
exit 3
fi
REPO="borg@backup.prauscher.de:howard"
[ -n "$2" ] && REPO="$2"
export BORG_PASSPHRASE="ARE-YOU-SERIOUS?!"
export BORG_RSH="ssh -i ~prauscher/.ssh/id_ed25519"
# Create Partition layout
parted --script "$1" -a optimal \
mklabel gpt \
mkpart howard-esp fat32 1MiB 538MiB \
mkpart howard-root ext4 538MiB 100% \
set 1 esp on
# wait for the devices to be ready
sleep 2
# Create partitions
# specify uuids for grub (mainly), fstab could use PARTLABEL=
mkfs.vfat -i 3E9CF97B /dev/disk/by-partlabel/howard-esp
mkfs.ext4 -U a9b3c40f-69a5-471d-a2cc-2055435e4f07 /dev/disk/by-partlabel/howard-root
trap "umount -R /mnt" EXIT
# Mount partitions
mount /dev/disk/by-partlabel/howard-root /mnt
mkdir -p /mnt/boot/efi
mount /dev/disk/by-partlabel/howard-esp /mnt/boot/efi
# create temporary folders not contained in the backup (excludes)
mkdir -p /mnt/tmp /mnt/var/tmp /mnt/dev /mnt/proc /mnt/sys /mnt/run /mnt/mnt /mnt/media /mnt/host
chmod +t /tmp
chmod +t /var/tmp
# get latest
ARCHIVE=$(borg list --short --last 1 "$REPO")
echo "Restoring from archive $REPO::$ARCHIVE"
# restore files
cd /mnt
borg extract --list --numeric-owner --exclude pp:host "$REPO::$ARCHIVE"
# copy bootloader
[ -d /mnt/boot/efi/EFI/boot ] || mkdir -p /mnt/boot/efi/EFI/boot
cp /mnt/boot/efi/EFI/debian/grubx64.efi /mnt/boot/efi/EFI/boot/grubx64.efi
cp /mnt/boot/efi/EFI/debian/shimx64.efi /mnt/boot/efi/EFI/boot/bootx64.efi
# wait for copy process to be done
sync
sleep 2
# unmount partitions is done in trap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment