clean-lxcs.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. BL=$(echo "\033[36m")
  17. RD=$(echo "\033[01;31m")
  18. CM='\xE2\x9C\x94\033'
  19. GN=$(echo "\033[1;92m")
  20. CL=$(echo "\033[m")
  21. header_info
  22. echo "Loading..."
  23. whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC Updater" --yesno "This Will Clean logs, cache and update apt lists on selected LXC Containers. Proceed?" 10 58 || exit
  24. NODE=$(hostname)
  25. EXCLUDE_MENU=()
  26. MSG_MAX_LENGTH=0
  27. while read -r TAG ITEM; do
  28. OFFSET=2
  29. ((${#ITEM} + OFFSET > MSG_MAX_LENGTH)) && MSG_MAX_LENGTH=${#ITEM}+OFFSET
  30. EXCLUDE_MENU+=("$TAG" "$ITEM " "OFF")
  31. done < <(pct list | awk 'NR>1')
  32. excluded_containers=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Containers on $NODE" --checklist "\nSelect containers to skip from cleaning:\n" \
  33. 16 $((MSG_MAX_LENGTH + 23)) 6 "${EXCLUDE_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"') || exit
  34. function clean_container() {
  35. container=$1
  36. header_info
  37. name=$(pct exec "$container" hostname)
  38. echo -e "${BL}[Info]${GN} Cleaning ${name} ${CL} \n"
  39. pct exec $container -- bash -c "apt-get -y --purge autoremove && apt-get -y autoclean && bash <(curl -fsSL https://github.com/tteck/Proxmox/raw/main/misc/clean.sh) && rm -rf /var/lib/apt/lists/* && apt-get update"
  40. }
  41. for container in $(pct list | awk '{if(NR>1) print $1}'); do
  42. if [[ " ${excluded_containers[@]} " =~ " $container " ]]; then
  43. header_info
  44. echo -e "${BL}[Info]${GN} Skipping ${BL}$container${CL}"
  45. sleep 1
  46. else
  47. os=$(pct config "$container" | awk '/^ostype/ {print $2}')
  48. if [ "$os" != "debian" ] && [ "$os" != "ubuntu" ]; then
  49. header_info
  50. echo -e "${BL}[Info]${GN} Skipping ${name} ${RD}$container is not Debian or Ubuntu ${CL} \n"
  51. sleep 1
  52. continue
  53. fi
  54. status=$(pct status $container)
  55. template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
  56. if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
  57. echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n"
  58. pct start $container
  59. echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n"
  60. sleep 5
  61. clean_container $container
  62. echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n"
  63. pct shutdown $container &
  64. elif [ "$status" == "status: running" ]; then
  65. clean_container $container
  66. fi
  67. fi
  68. done
  69. wait
  70. header_info
  71. echo -e "${GN} Finished, Selected Containers Cleaned. ${CL} \n"