host-backup.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. header_info
  17. start() {
  18. 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)
  19. if [ -z "$BACKUP_PATH" ]; then
  20. BACKUP_PATH="/root/"
  21. else
  22. BACKUP_PATH="$BACKUP_PATH"
  23. fi
  24. 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 (No leading or trailing slashes):" 3>&1 1>&2 2>&3)
  25. if [ -z "$DIR" ]; then
  26. DIR="etc"
  27. else
  28. DIR="$DIR"
  29. fi
  30. DIR_DASH=$(echo "$DIR" | tr '/' '-')
  31. BACKUP_FILE="$(hostname)-${DIR_DASH}-backup"
  32. selected_directories=()
  33. while read -r dir; do
  34. DIRNAME=$(basename "$dir")
  35. OFFSET=2
  36. if [[ $((${#DIRNAME} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  37. MSG_MAX_LENGTH=$((${#DIRNAME} + $OFFSET))
  38. fi
  39. CTID_MENU+=("$DIRNAME" "$dir " "OFF")
  40. done < <(ls -d /${DIR}/*)
  41. while [ -z "${HOST_BACKUP:+x}" ]; do
  42. HOST_BACKUP=$(whiptail --backtitle "Proxmox VE Host Backup" --title "Working in the ${DIR} directory " --checklist \
  43. "\nSelect what files/directories to backup:\n" \
  44. 16 $(($MSG_MAX_LENGTH + 58)) 6 \
  45. "${CTID_MENU[@]}" 3>&1 1>&2 2>&3) || exit
  46. for selected_dir in ${HOST_BACKUP//\"/}; do
  47. selected_directories+=("/${DIR}/$selected_dir")
  48. done
  49. done
  50. selected_directories_string=$(printf "%s " "${selected_directories[@]}")
  51. header_info
  52. 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"
  53. read -p "Press ENTER to continue..."
  54. header_info
  55. echo "Working..."
  56. tar -czf "$BACKUP_PATH$BACKUP_FILE-$(date +%Y_%m_%d).tar.gz" --absolute-names ${selected_directories_string% }
  57. header_info
  58. echo -e "\nFinished"
  59. echo -e "\e[1;33m \nA backup is rendered ineffective when it remains stored on the host.\n \e[0m"
  60. }
  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. start
  63. fi