From a3310d290cf6ca183cd6855ebae7a4a47adfeec2 Mon Sep 17 00:00:00 2001 From: Gaston Gonzalez Date: Tue, 14 Jan 2025 05:14:21 -0700 Subject: [PATCH 1/4] Backup - Added initial script to backup select directories in user's home directory --- overlay/opt/emcomm-tools/bin/et-user-backup | 45 +++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 overlay/opt/emcomm-tools/bin/et-user-backup diff --git a/overlay/opt/emcomm-tools/bin/et-user-backup b/overlay/opt/emcomm-tools/bin/et-user-backup new file mode 100755 index 0000000..8a94ba5 --- /dev/null +++ b/overlay/opt/emcomm-tools/bin/et-user-backup @@ -0,0 +1,45 @@ +#!/bin/bash +# Author : Gaston Gonzalez +# Date : 14 January 2024 +# 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 + +TODAY=$(date +"%Y%m%d") +BACKUP_FILE="${HOME}/etc-user-backup-${HOSTNAME}-${TODAY}.tar.gz" + +# All paths are relative to $HOME. +BACKUP_DIRS=( + ".config/emcomm-tools" + ".local/share/emcomm-tools" + ".local/share/pat" +) + +ESC="\033" +CYAN="$ESC[36m" +GREEN="$ESC[0;32m" +RED="$ESC[0;31m" +WHITE="$ESC[1;37m" +YELLOW="$ESC[1;33m" +NC="$ESC[0m" + +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 "\n${YELLOW}If you need files outside of these directories, please back them up manually.${NC}" From d0a3152501e907ff68defcd1b3125ce781cb9d48 Mon Sep 17 00:00:00 2001 From: Gaston Gonzalez Date: Thu, 16 Jan 2025 02:51:18 -0700 Subject: [PATCH 2/4] Backup - Updated backup script to check for existing backup file. --- overlay/opt/emcomm-tools/bin/et-user-backup | 23 ++++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/overlay/opt/emcomm-tools/bin/et-user-backup b/overlay/opt/emcomm-tools/bin/et-user-backup index 8a94ba5..6eacfbe 100755 --- a/overlay/opt/emcomm-tools/bin/et-user-backup +++ b/overlay/opt/emcomm-tools/bin/et-user-backup @@ -1,6 +1,7 @@ #!/bin/bash # Author : Gaston Gonzalez -# Date : 14 January 2024 +# 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: @@ -10,13 +11,6 @@ TODAY=$(date +"%Y%m%d") BACKUP_FILE="${HOME}/etc-user-backup-${HOSTNAME}-${TODAY}.tar.gz" -# All paths are relative to $HOME. -BACKUP_DIRS=( - ".config/emcomm-tools" - ".local/share/emcomm-tools" - ".local/share/pat" -) - ESC="\033" CYAN="$ESC[36m" GREEN="$ESC[0;32m" @@ -25,6 +19,19 @@ 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" +) + tar -czf ${BACKUP_FILE} \ --transform 's|^.*\.local|.local|' \ -C "${HOME}" "${BACKUP_DIRS[@]}" From 9804b19db199908e1f6c62e0d733410fb9c7391e Mon Sep 17 00:00:00 2001 From: Gaston Gonzalez Date: Thu, 16 Jan 2025 03:07:41 -0700 Subject: [PATCH 3/4] Backup - Added SSH keys to the list. Add the ability to preview the list of files backed up --- overlay/opt/emcomm-tools/bin/et-user-backup | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/overlay/opt/emcomm-tools/bin/et-user-backup b/overlay/opt/emcomm-tools/bin/et-user-backup index 6eacfbe..f086a60 100755 --- a/overlay/opt/emcomm-tools/bin/et-user-backup +++ b/overlay/opt/emcomm-tools/bin/et-user-backup @@ -7,6 +7,7 @@ # 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" @@ -30,6 +31,7 @@ BACKUP_DIRS=( ".config/emcomm-tools" ".local/share/emcomm-tools" ".local/share/pat" + ".ssh" ) tar -czf ${BACKUP_FILE} \ @@ -49,4 +51,15 @@ for dir in "${BACKUP_DIRS[@]}"; do echo "* $dir" done -echo -e "\n${YELLOW}If you need files outside of these directories, please back them up manually.${NC}" +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 + From b9539999d8c0fcadcfabe9a8e878f0ff9912366d Mon Sep 17 00:00:00 2001 From: Gaston Gonzalez Date: Fri, 17 Jan 2025 06:53:49 -0700 Subject: [PATCH 4/4] Backup - Added et-user-restore command --- overlay/opt/emcomm-tools/bin/et-user-restore | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100755 overlay/opt/emcomm-tools/bin/et-user-restore diff --git a/overlay/opt/emcomm-tools/bin/et-user-restore b/overlay/opt/emcomm-tools/bin/et-user-restore new file mode 100755 index 0000000..4e83a0b --- /dev/null +++ b/overlay/opt/emcomm-tools/bin/et-user-restore @@ -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}