clean-lxcs.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. echo -e "\n ${RD} USE AT YOUR OWN RISK. Deleting logs/cache may result in some apps/services broken!${CL} \n"
  25. while true; do
  26. read -p "This Will Clean logs, cache and update apt lists on all LXC Containers. Proceed(y/n)?" yn
  27. case $yn in
  28. [Yy]*) break ;;
  29. [Nn]*) exit ;;
  30. *) echo "Please answer yes or no." ;;
  31. esac
  32. done
  33. clear
  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. os=$(pct config "$container" | awk '/^ostype/ {print $2}')
  43. if [ "$os" != "debian" ] && [ "$os" != "ubuntu" ]; then
  44. header_info
  45. echo -e "${BL}[Info]${GN} Skipping ${name} ${RD}$container is not Debian or Ubuntu ${CL} \n"
  46. sleep 1
  47. continue
  48. fi
  49. status=$(pct status $container)
  50. template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
  51. if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
  52. echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n"
  53. pct start $container
  54. echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n"
  55. sleep 5
  56. clean_container $container
  57. echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n"
  58. pct shutdown $container &
  59. elif [ "$status" == "status: running" ]; then
  60. clean_container $container
  61. fi
  62. done
  63. wait
  64. header_info
  65. echo -e "${GN} Finished, Containers Cleaned. ${CL} \n"