Update host-backup.sh

- code refactoring
- add comments
- loop the main script execution until "No"
This commit is contained in:
tteckster 2023-09-09 20:50:26 -04:00 committed by GitHub
parent 64b3958685
commit e098af8ed9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 32 deletions

View File

@ -15,61 +15,65 @@ function header_info {
/_/ /_/
EOF EOF
} }
header_info
start() { # Function to perform backup
BACKUP_PATH=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "\nDefaults to /root/\ne.g. /mnt/backups/" 11 68 --title "Directory to backup to:" 3>&1 1>&2 2>&3) function perform_backup {
local BACKUP_PATH
local DIR
local DIR_DASH
local BACKUP_FILE
local selected_directories=()
if [ -z "$BACKUP_PATH" ]; then # Get backup path from user
BACKUP_PATH="/root/" BACKUP_PATH=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "\nDefaults to /root/\ne.g. /mnt/backups/" 11 68 --title "Directory to backup to:" 3>&1 1>&2 2>&3) || return
else
BACKUP_PATH="$BACKUP_PATH"
fi
DIR=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "\nDefaults to /etc/\ne.g. /root/, /var/lib/pve-cluster/ etc." 11 68 --title "Directory to work in:" 3>&1 1>&2 2>&3) # Default to /root/ if no input
BACKUP_PATH="${BACKUP_PATH:-/root/}"
if [ -z "$DIR" ]; then # Get directory to work in from user
DIR="/etc/" DIR=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "\nDefaults to /etc/\ne.g. /root/, /var/lib/pve-cluster/ etc." 11 68 --title "Directory to work in:" 3>&1 1>&2 2>&3) || return
else
DIR="$DIR" # Default to /etc/ if no input
fi DIR="${DIR:-/etc/}"
DIR_DASH=$(echo "$DIR" | tr '/' '-') DIR_DASH=$(echo "$DIR" | tr '/' '-')
BACKUP_FILE="$(hostname)${DIR_DASH}backup" BACKUP_FILE="$(hostname)${DIR_DASH}backup"
selected_directories=()
# Build a list of directories for backup
local CTID_MENU=()
while read -r dir; do while read -r dir; do
DIRNAME=$(basename "$dir") CTID_MENU+=("$(basename "$dir")" "$dir " "OFF")
OFFSET=2 done < <(ls -d "${DIR}"*)
if [[ $((${#DIRNAME} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
MSG_MAX_LENGTH=$((${#DIRNAME} + $OFFSET))
fi
CTID_MENU+=("$DIRNAME" "$dir " "OFF")
done < <(ls -d ${DIR}*)
# Allow the user to select directories
local HOST_BACKUP
while [ -z "${HOST_BACKUP:+x}" ]; do while [ -z "${HOST_BACKUP:+x}" ]; do
HOST_BACKUP=$(whiptail --backtitle "Proxmox VE Host Backup" --title "Working in the ${DIR} directory " --checklist \ HOST_BACKUP=$(whiptail --backtitle "Proxmox VE Host Backup" --title "Working in the ${DIR} directory " --checklist \
"\nSelect what files/directories to backup:\n" \ "\nSelect what files/directories to backup:\n" 16 $(((${#DIRNAME} + 2) + 88)) 6 "${CTID_MENU[@]}" 3>&1 1>&2 2>&3) || return
16 $(($MSG_MAX_LENGTH + 58)) 6 \
"${CTID_MENU[@]}" 3>&1 1>&2 2>&3) || exit
for selected_dir in ${HOST_BACKUP//\"/}; do for selected_dir in ${HOST_BACKUP//\"/}; do
selected_directories+=("${DIR}${selected_dir}") selected_directories+=("${DIR}$selected_dir")
done done
done done
selected_directories_string=$(printf "%s " "${selected_directories[@]}") # Perform the backup
header_info header_info
echo -e "This will create a backup in\e[1;33m ${BACKUP_PATH} \e[0mfor these files and directories\e[1;33m ${selected_directories_string% } \e[0m" echo -e "This will create a backup in\e[1;33m $BACKUP_PATH \e[0mfor these files and directories\e[1;33m ${selected_directories[*]} \e[0m"
read -p "Press ENTER to continue..." read -p "Press ENTER to continue..."
header_info header_info
echo "Working..." echo "Working..."
tar -czf "${BACKUP_PATH}${BACKUP_FILE}-$(date +%Y_%m_%d).tar.gz" --absolute-names ${selected_directories_string% } tar -czf "$BACKUP_PATH$BACKUP_FILE-$(date +%Y_%m_%d).tar.gz" --absolute-names "${selected_directories[@]}"
header_info header_info
echo -e "\nFinished" echo -e "\nFinished"
echo -e "\e[1;33m \nA backup is rendered ineffective when it remains stored on the host.\n \e[0m" echo -e "\e[1;33m \nA backup is rendered ineffective when it remains stored on the host.\n \e[0m"
sleep 2
} }
# Main script execution loop
while true; do
if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE Host Backup" --yesno "This will create backups for particular files and directories located within a designated directory. Proceed?" 10 88); then if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE Host Backup" --yesno "This will create backups for particular files and directories located within a designated directory. Proceed?" 10 88); then
start perform_backup
else
break
fi fi
done