host-backup.sh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. function header_info {
  7. clear
  8. cat <<"EOF"
  9. __ __ __ ___ __
  10. / // /__ ___ / /_ / _ )___ _____/ /____ _____
  11. / _ / _ \(_-</ __/ / _ / _ `/ __/ '_/ // / _ \
  12. /_//_/\___/___/\__/ /____/\_,_/\__/_/\_\\_,_/ .__/
  13. /_/
  14. EOF
  15. }
  16. # Function to perform backup
  17. function perform_backup {
  18. local BACKUP_PATH
  19. local DIR
  20. local DIR_DASH
  21. local BACKUP_FILE
  22. local selected_directories=()
  23. # Get backup path from user
  24. 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
  25. # Default to /root/ if no input
  26. BACKUP_PATH="${BACKUP_PATH:-/root/}"
  27. # Get directory to work in from user
  28. 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
  29. # Default to /etc/ if no input
  30. DIR="${DIR:-/etc/}"
  31. DIR_DASH=$(echo "$DIR" | tr '/' '-')
  32. BACKUP_FILE="$(hostname)${DIR_DASH}backup"
  33. # Build a list of directories for backup
  34. local CTID_MENU=()
  35. while read -r dir; do
  36. CTID_MENU+=("$(basename "$dir")" "$dir " "OFF")
  37. done < <(ls -d "${DIR}"*)
  38. # Allow the user to select directories
  39. local HOST_BACKUP
  40. while [ -z "${HOST_BACKUP:+x}" ]; do
  41. HOST_BACKUP=$(whiptail --backtitle "Proxmox VE Host Backup" --title "Working in the ${DIR} directory " --checklist \
  42. "\nSelect what files/directories to backup:\n" 16 $(((${#DIRNAME} + 2) + 88)) 6 "${CTID_MENU[@]}" 3>&1 1>&2 2>&3) || return
  43. for selected_dir in ${HOST_BACKUP//\"/}; do
  44. selected_directories+=("${DIR}$selected_dir")
  45. done
  46. done
  47. # Perform the backup
  48. header_info
  49. 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"
  50. read -p "Press ENTER to continue..."
  51. header_info
  52. echo "Working..."
  53. tar -czf "$BACKUP_PATH$BACKUP_FILE-$(date +%Y_%m_%d).tar.gz" --absolute-names "${selected_directories[@]}"
  54. header_info
  55. echo -e "\nFinished"
  56. echo -e "\e[1;33m \nA backup is rendered ineffective when it remains stored on the host.\n \e[0m"
  57. sleep 2
  58. }
  59. # Main script execution loop
  60. while true; do
  61. 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
  62. perform_backup
  63. else
  64. break
  65. fi
  66. done