You are not logged in.

#1 2024-04-16 04:07:41

Hairyplotter
Member
Registered: 2017-03-25
Posts: 4

My bare metal backup / restore script.

I can't stress enough the importance of having an up to date backup of all of your data.
Too many times I have done "rm -rf" -> realize I was in the wrong directory -> cried for days because I didn't have a backup of the data I nuked.

This script is a full backup using rsync. I might add an option for incremental or differential backups in the future.
This script saves my source device partition table to a file in the root of the backup directory.
This script creates a full restore script in the root of the backup directory.

For a little context. My laptop has 2 internal SSD drives that I link with LVM. I have a docking station that I use with a combination of SSD and SATA drives. (I NEVER dispose of a working drive, I retire them when they die) I also have a 4TB USB drive. I use a combination of all of these for backing up my data which explains the long list of potential backup destinations in my script as well as the source devices.

My entire backup strategy is built around this setup, but my script can easily be edited to conform to virtually any configuration.

ALL of my external drives are mounted in my /home under a directory called "MountPoints", each drive having a listing in my /etc/fstab:

#################################################################################
#                   Custom Mount Points
#################################################################################
UUID=7a9e17da-0aa9-4167-b841-acadf998b8bb   /home/robert/MountPoints/ExternalSSD     ext4    noauto,users                                                                                     0 0
UUID=6c47e18f-a401-4d2a-9702-ba41c254619a   /home/robert/MountPoints/Ext_SATA232GB   ext4    noauto,users                                                                                     0 0
UUID=2a45f214-1d90-4cd1-95f1-2d5c5abaaedc   /home/robert/MountPoints/Ext_SATA465GB   ext4    noauto,users                                                                                     0 0
UUID=3b0fea19-a81d-470f-89d5-ad6d938887d6   /home/robert/MountPoints/USB4T-PART1     btrfs   noauto,users                                                                                     0 0
UUID=0b9de2b8-a9f5-45d1-96bb-99ed5e445a7f   /home/robert/MountPoints/USB4T-PART2     btrfs   noauto,users                                                                                     0 0
UUID=09c5f8df-7a4e-48df-b98c-606a831213d4   /home/robert/MountPoints/USB4T-PART3     btrfs   noauto,users                                                                                     0 0

The documentation isn't finished, that is on my [TODO] list.
Also, since all of my UUIDs are fixed, there is no need to do genfstab after the restore, so activating the swap partition in my restore script is kinda pointless.
since leaving it in is harmless, I haven't gotten around to removing it, I'll do it in a later revision.

#!/bin/bash

### [ Test for root ] ###################################################################
if [[ $(id -u) != 0 ]]; then
  printf 'This script must be run as root\n' 1>&2
  exit 1
fi
#########################################################################################

### [ Define variables ] ################################################################
BACKUP_LOCATIONS=(
"/home/robert/MountPoints/ExternalSSD/"
"/home/robert/MountPoints/Ext_SATA232GB/"
"/home/robert/MountPoints/Ext_SATA465GB/"
"/home/robert/MountPoints/USB4T-PART1/"
"/home/robert/MountPoints/USB4T-PART2/"
"/home/robert/MountPoints/USB4T-PART3/"
)
SOURCE_DEVICE=(
"/dev/nvme0n1"
"/dev/nvme1n1"
)
BACKUP_SOURCE='/'
CURRENT_DATE=$(date +'%m%d%Y')
BACKUP_RETENTION=4
PARTITION_TABLE_BACKUP='-partition-table-'${CURRENT_DATE}
BASE_RESTORE_SCRIPT='/base-system-restore.sh'
#########################################################################################

### [ Find mounted backup targets ] #####################################################
# 1) Use lsblk -n -r -e 11,259,254 to populate an array [BLOCK_DEVICES] with a list of all valid block devices
#    excluding any CD-ROM drives (11) and the primary SSD drives (259 and 254)
readarray -t BLOCK_DEVICES < <(lsblk -n -r -e 11,259,254)
# 2) Step through each element of the array BLOCK_DEVICES
for loopOne in "${BLOCK_DEVICES[@]}"
do
# 3) the 7th value in each element of BLOCK_DEVICES will be the block devices mount point (if the device is mounted)
#    assign that value to a variable MOUNT_POINT
  MOUNT_POINT=$(cut -d ' ' -f 7 <<< $loopOne)/
# 4) Step through each element of the array BACKUP_LOCATIONS
    for loopTwo in "${BACKUP_LOCATIONS[@]}"
    do
# 5) Compare the value of MOUNT_POINT to each element of BACKUP_LOCATIONS. If there is a match, stop checking
#    Concatenate the values of MOUNT_POINT and CURRENT_DATE to create BACKUP_DESTINATION
#    Create BASE_DIRECTORY for later use in removing stale backups.
      if [[ $loopTwo = $MOUNT_POINT ]]
        then
          BASE_DIRECTORY=${MOUNT_POINT}
          BACKUP_DESTINATION=${MOUNT_POINT}/${CURRENT_DATE}
      fi
    done
done
# 6) If BACKUP_DESTINATION is empty, then there are no drives mounted to back up to and the script should terminate.
if [[ -z $BACKUP_DESTINATION ]] ; then
  echo 'No Backup destination found'
  exit 1
fi

EXCLUDE_DIR=(
"--exclude=*.[Ii][Ss][Oo]"
"--exclude=/dev/*"
"--exclude=/proc/*"
"--exclude=/sys/*"
"--exclude=/tmp/*"
"--exclude=/run/*"
"--exclude=/mnt/*"
"--exclude=/media/*"
"--exclude=/lost+found"
"--exclude=/var/cache/*"
"--exclude=/home/robert/.local/share/Steam/steamapps/common/Baldurs*Gate*3/*"
"--exclude=/home/robert/.local/share/Trash/*"
"--exclude=/home/robert/MountPoints/*"
)

rsync -aAXv --delete ${BACKUP_SOURCE} ${EXCLUDE_DIR[@]} ${BACKUP_DESTINATION}

readarray -t TOTAL_BACKUPS < <(find "${BASE_DIRECTORY}" -maxdepth 1 -name *202* | sort )

if [[ ${#TOTAL_BACKUPS[@]} -gt $BACKUP_RETENTION ]]
  then
    EXCESS_BACKUPS=$(( (${#TOTAL_BACKUPS[@]} - $BACKUP_RETENTION) - 1 ))
      until [ $EXCESS_BACKUPS -lt 0 ]
      do
        echo 'Removing stale backup --> '${TOTAL_BACKUPS[$EXCESS_BACKUPS]}
        rm -rfd ${TOTAL_BACKUPS[$EXCESS_BACKUPS]}
        EXCESS_BACKUPS=$(($EXCESS_BACKUPS-1))
      done
fi

for loopThree in "${SOURCE_DEVICE[@]}"
do
  sfdisk -d ${loopThree} > ${BACKUP_DESTINATION}/$(basename ${loopThree})${PARTITION_TABLE_BACKUP}
done

#####################################################################################################
#         Write a full resore script to the backup device
#
#####################################################################################################
echo "set -e" > ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Lists devices that are the target of the restore
#
echo "RED='\033[0;31m'" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "NOCOLOR='\033[0m'" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "TARGET_DEVICES=(/dev/nvme0n1 /dev/nvme1n1)" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Wipe the partition information from all restore devices
#
echo 'for DEVICE_LOOP in "${TARGET_DEVICES[@]}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "do" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo '  echo -e "${RED}Erasing existing partition table on device "${DEVICE_LOOP}${NOCOLOR} ' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo '  dd if=/dev/zero of="${DEVICE_LOOP}" bs=512 count=1' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "  sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Write partition table files to their respective drives
#
echo "  for FILE in *" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "  do" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo '    if [[ $(basename $DEVICE_LOOP) = ${FILE:0:7} ]]' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "      then" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo '        echo -e "${RED}Writting backup partition table to device "${DEVICE_LOOP}${NOCOLOR} ' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo '        sfdisk "${DEVICE_LOOP}" < "$FILE"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "        sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "    fi" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "  done" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "done" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Create physical volume from /dev/nvme0n1p1 (Entire drive)
#
echo "pvcreate /dev/nvme0n1p1" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Create physical volume from /dev/nvme1n1p3 (Thrid Partition)
#
echo "pvcreate /dev/nvme1n1p3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Create a volume group containing both physical volumes and name it ArchVolumeGroup
#
echo "vgcreate ArchVolumeGroup /dev/nvme0n1p1 /dev/nvme1n1p3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Create a 32G logical volume for Swap from ArchVolumeGroup
#
echo "lvcreate -L 32G ArchVolumeGroup -n SwapVolume" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Create a 60G logical volume for Root from ArchVolumeGroup
#
echo "lvcreate -L 60G ArchVolumeGroup -n RootVolume" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Create a logical volume for Home from ArchVolumeGroup using ALL remaining space
#
echo "lvcreate -l 100%FREE ArchVolumeGroup -n HomeVolume" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Shrink ArchVolumeGroup-HomeVolume by 256M for housekeeping
#
echo "lvreduce -L -256M ArchVolumeGroup/HomeVolume" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Format the /boot partition (ext4) and set its UUID
#
echo 'echo -e "${RED}Formatting /boot filesystem and setting UUID${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mkfs.ext4 -q -F -U 9be97e46-7359-4912-9f21-8b80b6ef66ac /dev/nvme1n1p1" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Format the /boot/efi partition (fat32) and set its UUID
#
echo 'echo -e "${RED}Formatting /boot/efi filesystem and setting UUID${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mkfs.fat -F 32 -i 7CA6F932 /dev/nvme1n1p2" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Format the /root partition (ext4) and set its UUID
#
echo 'echo -e "${RED}Formatting /root filesystem and setting UUID${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mkfs.ext4 -q -F -U 6016ed2d-c304-4da9-aef3-3bcb898a4d04 /dev/ArchVolumeGroup-RootVolume" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Format the /home partition (ext4) and set its UUID
#
echo 'echo -e "${RED}Formatting /home filesystem and setting UUID${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mkfs.ext4 -q -F -U 4ab6c77b-5fd8-4051-82f4-c6867ab8f392 /dev/ArchVolumeGroup-HomeVolume" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Format the /swap partition and set its UUID
#
echo 'echo -e "${RED}Formatting swap and setting UUID${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mkswap -q -f -U 71512d9e-01df-49d6-8b2d-5d29642e1abc /dev/ArchVolumeGroup-SwapVolume" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Mount ArchVolumeGroup-RootVolume to /mnt
#
echo 'echo -e "${RED}Mounting root filesysten to /mnt${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mount /dev/ArchVolumeGroup-RootVolume /mnt" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Create the boot directory under /mnt/
#
echo 'echo -e "${RED}Creating /boot in /mnt${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mkdir /mnt/boot" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 1" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Create the home directory under /mnt/
#
echo 'echo -e "${RED}Creating /home in /mnt${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mkdir /mnt/home" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 1" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Mount /dev/nvme1n1p1 to /mnt/boot
#
echo 'echo -e "${RED}Mounting /dev/nvme1n1p1 [/boot] to /mnt/boot${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mount /dev/nvme1n1p1 /mnt/boot" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 1" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Create the efi directory under /mnt/boot/
#
echo 'echo -e "${RED}Creating /boot/efi in /mnt${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mkdir /mnt/boot/efi" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 1" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Mount /dev/nvme0n1p2 to /mnt/boot/efi
#
echo 'echo -e "${RED}Mounting /dev/nvme1n1p2 [/boot/efi] to /mnt/boot${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mount /dev/nvme1n1p2 /mnt/boot/efi" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 1" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Mount ArchVolumeGroup-HomeVolume to /mnt/home
#
echo 'echo -e "${RED}Mounting home to /mnt/home${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "mount /dev/ArchVolumeGroup-HomeVolume /mnt/home" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 1" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Activate the swap partition
#
echo 'echo -e "${RED}Activating swap partition${NOCOLOR}"' >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "swapon /dev/ArchVolumeGroup-SwapVolume" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#         Restore the backup and install the boot loader
#
echo "rsync -aAX . /mnt" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "arch-chroot /mnt grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id='Arch Linux'" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#
#
echo "rm /mnt/*"${PARTITION_TABLE_BACKUP} >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#
#
echo "rm /mnt"${BASE_RESTORE_SCRIPT} >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "sleep 3" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
echo "" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#
#
echo "reboot" >> ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}
#####################################################################################################
#
#
chmod 755 ${BACKUP_DESTINATION}${BASE_RESTORE_SCRIPT}

The resulting restore script looks like this:

set -e

RED='\033[0;31m'
NOCOLOR='\033[0m'
TARGET_DEVICES=(/dev/nvme0n1 /dev/nvme1n1)

for DEVICE_LOOP in "${TARGET_DEVICES[@]}"
do
  echo -e "${RED}Erasing existing partition table on device "${DEVICE_LOOP}${NOCOLOR} 
  dd if=/dev/zero of="${DEVICE_LOOP}" bs=512 count=1
  sleep 3

  for FILE in *
  do
    if [[ $(basename $DEVICE_LOOP) = ${FILE:0:7} ]]
      then
        echo -e "${RED}Writting backup partition table to device "${DEVICE_LOOP}${NOCOLOR} 
        sfdisk "${DEVICE_LOOP}" < "$FILE"
        sleep 3
    fi
  done
done
sleep 3

pvcreate /dev/nvme0n1p1
sleep 3

pvcreate /dev/nvme1n1p3
sleep 3

vgcreate ArchVolumeGroup /dev/nvme0n1p1 /dev/nvme1n1p3
sleep 3

lvcreate -L 32G ArchVolumeGroup -n SwapVolume
sleep 3

lvcreate -L 60G ArchVolumeGroup -n RootVolume
sleep 3

lvcreate -l 100%FREE ArchVolumeGroup -n HomeVolume
sleep 3

lvreduce -L -256M ArchVolumeGroup/HomeVolume
leep 3

echo -e "${RED}Formatting /boot filesystem and setting UUID${NOCOLOR}"
mkfs.ext4 -q -F -U 9be97e46-7359-4912-9f21-8b80b6ef66ac /dev/nvme1n1p1
sleep 3

echo -e "${RED}Formatting /boot/efi filesystem and setting UUID${NOCOLOR}"
mkfs.fat -F 32 -i 7CA6F932 /dev/nvme1n1p2
sleep 3

echo -e "${RED}Formatting /root filesystem and setting UUID${NOCOLOR}"
mkfs.ext4 -q -F -U 6016ed2d-c304-4da9-aef3-3bcb898a4d04 /dev/ArchVolumeGroup-RootVolume
sleep 3

echo -e "${RED}Formatting /home filesystem and setting UUID${NOCOLOR}"
mkfs.ext4 -q -F -U 4ab6c77b-5fd8-4051-82f4-c6867ab8f392 /dev/ArchVolumeGroup-HomeVolume
sleep 3

echo -e "${RED}Formatting swap and setting UUID${NOCOLOR}"
mkswap -q -f -U 71512d9e-01df-49d6-8b2d-5d29642e1abc /dev/ArchVolumeGroup-SwapVolume
sleep 3

echo -e "${RED}Mounting root filesysten to /mnt${NOCOLOR}"
mount /dev/ArchVolumeGroup-RootVolume /mnt
sleep 3

echo -e "${RED}Creating /boot in /mnt${NOCOLOR}"
mkdir /mnt/boot
sleep 1

echo -e "${RED}Creating /home in /mnt${NOCOLOR}"
mkdir /mnt/home
sleep 1

echo -e "${RED}Mounting /dev/nvme1n1p1 [/boot] to /mnt/boot${NOCOLOR}"
mount /dev/nvme1n1p1 /mnt/boot
sleep 1

echo -e "${RED}Creating /boot/efi in /mnt${NOCOLOR}"
mkdir /mnt/boot/efi
sleep 1

echo -e "${RED}Mounting /dev/nvme1n1p2 [/boot/efi] to /mnt/boot${NOCOLOR}"
mount /dev/nvme1n1p2 /mnt/boot/efi
sleep 1

echo -e "${RED}Mounting home to /mnt/home${NOCOLOR}"
mount /dev/ArchVolumeGroup-HomeVolume /mnt/home
sleep 1

echo -e "${RED}Activating swap partition${NOCOLOR}"
swapon /dev/ArchVolumeGroup-SwapVolume
sleep 3

rsync -aAX . /mnt

arch-chroot /mnt grub-install --target=x86_64-efi --efi-directory=/boot/efi --bootloader-id='Arch Linux'
sleep 3

rm /mnt/*-partition-table-04152024
sleep 3

rm /mnt/base-system-restore.sh
sleep 3

reboot

The 'sleep' is liberally used so I can see any warnings or errors before they scroll off the screen.

Hopefully somebody finds this useful.

Offline

#2 2024-04-24 11:39:13

256
Member
Registered: 2023-12-17
Posts: 7

Re: My bare metal backup / restore script.

The use of BLOCK_DEVICES is overcomplicated. 'lsblk -n -o MOUNTPOINT' would obviate the need for cut.


"Don't comment bad code - rewrite it." - The Elements of Programming Style (1978), Brian W. Kernighan & P. J. Plauger, p. 144.

Offline

Board footer

Powered by FluxBB