From b513be8d4d0d7a9f7beb8b0d03abc50c4be642d5 Mon Sep 17 00:00:00 2001 From: phanes Date: Sun, 26 Feb 2023 17:19:40 -0500 Subject: [PATCH] incremental improvement in backup/restore facility --- Makefile | 10 +- ...build_stage4backup.sh => backup_create.sh} | 0 make.project/backup_restore.sh | 107 ++++++++++++++++++ .../stage4backup/restore_backup.bash | 95 ++++++++++++++++ rex.project/x86_64/plans/backup_restore.plan | 10 ++ rex.project/x86_64/units/backup_restore.units | 22 ++++ 6 files changed, 241 insertions(+), 3 deletions(-) rename make.project/{build_stage4backup.sh => backup_create.sh} (100%) create mode 100755 make.project/backup_restore.sh create mode 100755 rex.project/x86_64/components/stage4backup/restore_backup.bash create mode 100644 rex.project/x86_64/plans/backup_restore.plan create mode 100644 rex.project/x86_64/units/backup_restore.units diff --git a/Makefile b/Makefile index 35f2135..de0e82a 100644 --- a/Makefile +++ b/Makefile @@ -77,9 +77,13 @@ build_stage3: sudo /usr/bin/env -i bash -c ". ./project_config.sh && ${dir_make}/build_stage3.sh" # offers to back up -build_stage4backup: +backup: set -e - sudo /usr/bin/env -i bash -c ". ./project_config.sh && ${dir_make}/build_stage4backup.sh" + sudo /usr/bin/env -i bash -c ". ./project_config.sh && ${dir_make}/backup_create.sh" + +restore_backup: + set -e + sudo bash -c ". ./project_config.sh && ${dir_make}/backup_restore.sh" build_stage4: set -e @@ -110,7 +114,7 @@ all: make build_stage1 && \ make build_stage2 && \ make build_stage3 && \ - make build_stage4backup && \ + make backup && \ make build_stage4 diff --git a/make.project/build_stage4backup.sh b/make.project/backup_create.sh similarity index 100% rename from make.project/build_stage4backup.sh rename to make.project/backup_create.sh diff --git a/make.project/backup_restore.sh b/make.project/backup_restore.sh new file mode 100755 index 0000000..bf3165b --- /dev/null +++ b/make.project/backup_restore.sh @@ -0,0 +1,107 @@ +#!/usr/bin/env bash + + +# fix an issue with open files limit on some hosts +ulimit -l unlimited + +#ulimit -n 10240 +ulimit -c unlimited + +T_SYSROOT=${dir_sysroot} + +#!/bin/bash +# move this to the makefile as a dedicated target + +APPNAME="Offer Backup" + +# ISO 8601 variation +TIMESTAMP="$(date +%Y-%m-%d_%H:%M:%S)" + +LOG_DIR="${LOGS_ROOT}/${APPNAME}-${TIMESTAMP}" + +# the file to log to +LOGFILE="${APPNAME}.log" + +logprint() { + mkdir -p "${LOG_DIR}" + echo "[$(date +%Y-%m-%d_%H:%M:%S)] [${APPNAME}] $1" \ + | tee -a "${LOG_DIR}/${LOGFILE}" +} + +logprint "Giving the user the option of backing up before proceeding." + +is_mounted() { + findmnt $1 &> /dev/null + if [ $? != 0 ]; then + logprint "Not mounted...skipping." + /usr/bin/false + else + logprint "Mounted..." + /usr/bin/true + fi +} + +function disarm_chroot() { + logprint "Unmounting CHROOT VFKS mounts" + + logprint "Unmounting ${T_SYSROOT}/dev" + is_mounted ${T_SYSROOT}/dev && umount -l ${T_SYSROOT}/dev + + logprint "Unmounting ${T_SYSROOT}/dev/pts" + is_mounted ${T_SYSROOT}/dev/pts && umount -l ${T_SYSROOT}/dev/pts + + logprint "Unmounting ${T_SYSROOT}/proc" + is_mounted ${T_SYSROOT}/proc && umount -l ${T_SYSROOT}/proc + + # not a symlink on ubuntu + logprint "Unmounting ${T_SYSROOT}/dev/shm" + is_mounted ${T_SYSROOT}/dev/shm && umount -l ${T_SYSROOT}/dev/shm + + logprint "Unmounting pyrois inside of chroot" + is_mounted ${T_SYSROOT}/rex_embedded && umount -l ${T_SYSROOT}/rex_embedded +} + +function clear_stage() { + pushd ${project_root} + make clean +} + + +function restore() { + # select a backup file + FILE=$(dialog --title "Choose a restore point." --stdout --title "Please choose an archive to restore from." --fselect ${project_root} 14 48) + + [ ! -z $FILE ] || logprint "User canceled restore." && exit 0 + + logprint "Entering backup routine. Clearing stage." + clear_stage + + logprint "Restoring backup...This will take a long time..." + tar xphf $FILE + assert_zero $? + + logprint "Backup restored successfully. Arming chroot." + pushd ${project_root} + assert_zero $? + make arm_chroot + echo + logprint "You may now proceed to run 'make build_stage4' or higher." + echo +} + + +read -r -d '' yn_msg <<-'EOF' +Restore from backup? +EOF + +# Use the dialog utility to prompt the user with a yes/no question +dialog --backtitle "Dark Horse Linux: Pyrois" --title "Restore From Backup" --yesno "$yn_msg" 10 60 +response=$? + +if [ $response -eq 0 ]; then + logprint "User selected to perform backups." + restore +else + logprint "User canceled. Moving on." + exit 0 +fi diff --git a/rex.project/x86_64/components/stage4backup/restore_backup.bash b/rex.project/x86_64/components/stage4backup/restore_backup.bash new file mode 100755 index 0000000..d6d5638 --- /dev/null +++ b/rex.project/x86_64/components/stage4backup/restore_backup.bash @@ -0,0 +1,95 @@ +#!/bin/bash +# move this to the makefile as a dedicated target + +APPNAME="Offer Backup" + +# ISO 8601 variation +TIMESTAMP="$(date +%Y-%m-%d_%H:%M:%S)" + +LOG_DIR="${LOGS_ROOT}/${APPNAME}-${TIMESTAMP}" + +# the file to log to +LOGFILE="${APPNAME}.log" + +logprint() { + mkdir -p "${LOG_DIR}" + echo "[$(date +%Y-%m-%d_%H:%M:%S)] [${APPNAME}] $1" \ + | tee -a "${LOG_DIR}/${LOGFILE}" +} + +logprint "Giving the user the option of backing up before proceeding." + +is_mounted() { + findmnt $1 &> /dev/null + if [ $? != 0 ]; then + logprint "Not mounted...skipping." + /usr/bin/false + else + logprint "Mounted..." + /usr/bin/true + fi +} + +function disarm_chroot() { + logprint "Unmounting CHROOT VFKS mounts" + + logprint "Unmounting ${T_SYSROOT}/dev" + is_mounted ${T_SYSROOT}/dev && umount -l ${T_SYSROOT}/dev + + logprint "Unmounting ${T_SYSROOT}/dev/pts" + is_mounted ${T_SYSROOT}/dev/pts && umount -l ${T_SYSROOT}/dev/pts + + logprint "Unmounting ${T_SYSROOT}/proc" + is_mounted ${T_SYSROOT}/proc && umount -l ${T_SYSROOT}/proc + + # not a symlink on ubuntu + logprint "Unmounting ${T_SYSROOT}/dev/shm" + is_mounted ${T_SYSROOT}/dev/shm && umount -l ${T_SYSROOT}/dev/shm + + logprint "Unmounting pyrois inside of chroot" + is_mounted ${T_SYSROOT}/rex_embedded && umount -l ${T_SYSROOT}/rex_embedded +} + +function clear_stage() { + pushd ${project_root} + make clean +} + + +function restore() { + # select a backup file + FILE=$(dialog --title "Choose a restore point." --stdout --title "Please choose an archive to restore from." --fselect ${project_root} 14 48) + + + logprint "Entering backup routine." + disarm_chroot + disarm_chroot + logprint "Restoring backup...This will take a long time..." + tar xpf $FILE + assert_zero $? + + logprint "Backup restored successfully. Arming chroot." + pushd ${project_root} + assert_zero $? + make arm_chroot + echo + logprint "You may now proceed to run 'make build_stage4' or higher." + echo +} + + +read -r -d '' yn_msg <<-'EOF' +Restore from backup? +EOF + +# Use the dialog utility to prompt the user with a yes/no question +dialog --backtitle "Dark Horse Linux: Pyrois" --title "Restore From Backup" --yesno "$yn_msg" 10 60 +response=$? + +if [ $response -eq 0 ]; then + logprint "User selected to perform backups." + restore +else + logprint "User canceled. Moving on." + exit 0 +fi diff --git a/rex.project/x86_64/plans/backup_restore.plan b/rex.project/x86_64/plans/backup_restore.plan new file mode 100644 index 0000000..d22ed43 --- /dev/null +++ b/rex.project/x86_64/plans/backup_restore.plan @@ -0,0 +1,10 @@ +{ + "plan": + [ + { + "name": "restore backup", + "dependencies": [ null ], + "comment": "LFS 11.3-systemd-rc1 Ch. 7.13.2" + } + ] +} diff --git a/rex.project/x86_64/units/backup_restore.units b/rex.project/x86_64/units/backup_restore.units new file mode 100644 index 0000000..265650e --- /dev/null +++ b/rex.project/x86_64/units/backup_restore.units @@ -0,0 +1,22 @@ +{ + "units": [ + { + "name": "restore backup", + "target": "components/stage4backup/restore_backup.bash", + "is_shell_command": true, + "shell_definition": "bash", + "force_pty": true, + "set_working_directory": false, + "working_directory": "", + "rectify": false, + "rectifier": "", + "active": true, + "required": true, + "set_user_context": true, + "user": "root", + "group": "root", + "supply_environment": true, + "environment": "environments/stage2.env.bash" + } + ] +}