Merge pull request #22 from thetechprepper/feature/backup-and-restore

Merged branch with et-user-backup|restore commands
This commit is contained in:
thetechprepper
2025-01-17 06:55:20 -07:00
committed by GitHub
2 changed files with 111 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
#!/bin/bash
# Author : Gaston Gonzalez
# Date : 14 January 2025
# Updated : 16 January 2025
# Purpose : Back up select files from the user's home directory.
#
# The backups are limited to the following:
# 1. EmComm Tools configuration
# 2. Winlink email
# 3. SSH keys
TODAY=$(date +"%Y%m%d")
BACKUP_FILE="${HOME}/etc-user-backup-${HOSTNAME}-${TODAY}.tar.gz"
ESC="\033"
CYAN="$ESC[36m"
GREEN="$ESC[0;32m"
RED="$ESC[0;31m"
WHITE="$ESC[1;37m"
YELLOW="$ESC[1;33m"
NC="$ESC[0m"
if [ -e "${BACKUP_FILE}" ]; then
echo -e -n "${RED}A backup file for today exists. Can ${WHITE}${BACKUP_FILE} ${RED}be overwritten? (y/n) ${NC}"
read response
[[ $response != [yY] ]] && echo "Skipping backup and exiting." && exit 1
fi
# All paths are relative to $HOME.
BACKUP_DIRS=(
".config/emcomm-tools"
".local/share/emcomm-tools"
".local/share/pat"
".ssh"
)
tar -czf ${BACKUP_FILE} \
--transform 's|^.*\.local|.local|' \
-C "${HOME}" "${BACKUP_DIRS[@]}"
[ $? -ne 0 ] && et-log "Error creating backup" && exit 1
# Test that we can read if after we create it.
TAR_OUT=$(tar -tzf ${BACKUP_FILE})
[ $? -ne 0 ] && et-log "Can't read backup file: ${BACKUP_FILE}. Error creating backup." && exit 1
echo -e "${GREEN}Backup created: ${WHITE}${BACKUP_FILE}\n"
echo -e "${CYAN}Note: Only the following directories were backed up:"
for dir in "${BACKUP_DIRS[@]}"; do
echo "* $dir"
done
echo -e "\nIf you need files outside of these directories, please back them up manually.\n${NC}"
read -p "Do you want see the full list of files backed up? (y/n) " response
[[ $response != [yY] ]] && exit 0
echo -e "1. ${YELLOW}Press ${WHITE}[SPACE]${YELLOW} to page through the list of files.${NC}"
echo -e "2. ${YELLOW}Press ${WHITE}[UP/DOWN]${YELLOW} to scroll through the list of files.${NC}"
echo -e "3. ${YELLOW}Press ${WHITE}q${YELLOW} when done viewing the list of files.\n${NC}"
read -p "Press any key to view the backup."
tar -tzf ${BACKUP_FILE} | less
+46
View File
@@ -0,0 +1,46 @@
#!/bin/bash
# Author : Gaston Gonzalez
# Date : 15 January 2025
# Updated : 17 January 2025
# Purpose : Restore backup to user's home directory.
BACKUP_FILE_PATTERN="etc-user-backup-*.tar.gz"
USB_MEDIA_MOUNT=/media
options=()
# 1. Search home directory
echo "Searching ${HOME} for backup files...."
while IFS= read -r backup; do
options+=("${backup}" "")
done < <(find ~ -type f -name ${BACKUP_FILE_PATTERN} 2> /dev/null)
# 2. Search attached USB drive
DF_OUT=$(df -h | grep ${USB_MEDIA_MOUNT})
if [ $? -eq 0 ]; then
echo "Searching ${USB_MEDIA_MOUNT} for backup files...."
while IFS= read -r backup; do
options+=("${backup}" "")
done < <(find ${USB_MEDIA_MOUNT} -type f -name ${BACKUP_FILE_PATTERN} 2> /dev/null)
fi
selected_file=$(dialog --clear --menu "Select a backup to restore" 20 80 10 "${options[@]}" 3>&1 1>&2 2>&3)
exit_status=$?
tput sgr 0 && clear
[ ${exit_status} -ne 0 ] && echo "No backup selected. Exiting." && exit 1
[ ! -e "${selected_file}" ] && echo "File ${selected_file} does not exist. Exiting."
CWD_DIR=$(pwd)
cd ~ && tar -xzf "${selected_file}"
if [ $? -eq 0 ]; then
echo "${selected_file} restored to ${HOME}."
else
echo "An error occured while attempting to restore ${selected_file}"
fi
cd ${CWD}