clean-lxcs.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. set -e
  17. YW=$(echo "\033[33m")
  18. BL=$(echo "\033[36m")
  19. RD=$(echo "\033[01;31m")
  20. CM='\xE2\x9C\x94\033'
  21. GN=$(echo "\033[1;92m")
  22. CL=$(echo "\033[m")
  23. header_info
  24. while true; do
  25. read -p "This Will Clean logs, cache and update apt lists on all LXC Containers. Proceed(y/n)?" yn
  26. case $yn in
  27. [Yy]*) break ;;
  28. [Nn]*) exit ;;
  29. *) echo "Please answer yes or no." ;;
  30. esac
  31. done
  32. clear
  33. TITLE="Containers on node"
  34. while read -r line; do
  35. TAG=$(echo "$line" | awk '{print $1}')
  36. ITEM=$(echo "$line" | awk '{print substr($0,36)}')
  37. OFFSET=2
  38. if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
  39. MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
  40. fi
  41. CTID_MENU+=("$TAG" "$ITEM " "OFF")
  42. done < <(pct list | awk 'NR>1')
  43. excluded_containers=$(whiptail --title "$TITLE" --checklist \
  44. "\nSelect containers to skip from cleaning:\n" \
  45. 16 $(($MSG_MAX_LENGTH + 23)) 6 \
  46. "${CTID_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"') || exit
  47. function clean_container() {
  48. container=$1
  49. header_info
  50. name=$(pct exec "$container" hostname)
  51. echo -e "${BL}[Info]${GN} Cleaning ${name} ${CL} \n"
  52. 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"
  53. }
  54. for container in $(pct list | awk '{if(NR>1) print $1}'); do
  55. if [[ " ${excluded_containers[@]} " =~ " $container " ]]; then
  56. header_info
  57. echo -e "${BL}[Info]${GN} Skipping ${BL}$container${CL}"
  58. sleep 1
  59. else
  60. os=$(pct config "$container" | awk '/^ostype/ {print $2}')
  61. if [ "$os" != "debian" ] && [ "$os" != "ubuntu" ]; then
  62. header_info
  63. echo -e "${BL}[Info]${GN} Skipping ${name} ${RD}$container is not Debian or Ubuntu ${CL} \n"
  64. sleep 1
  65. continue
  66. fi
  67. status=$(pct status $container)
  68. template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
  69. if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
  70. echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n"
  71. pct start $container
  72. echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n"
  73. sleep 5
  74. clean_container $container
  75. echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n"
  76. pct shutdown $container &
  77. elif [ "$status" == "status: running" ]; then
  78. clean_container $container
  79. fi
  80. fi
  81. done
  82. wait
  83. header_info
  84. echo -e "${GN} Finished, Containers Cleaned. ${CL} \n"