clean-lxcs.sh 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. function clean_container() {
  34. container=$1
  35. header_info
  36. name=$(pct exec "$container" hostname)
  37. echo -e "${BL}[Info]${GN} Cleaning ${name} ${CL} \n"
  38. 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"
  39. }
  40. for container in $(pct list | awk '{if(NR>1) print $1}'); do
  41. os=$(pct config "$container" | awk '/^ostype/ {print $2}')
  42. if [ "$os" != "debian" ] && [ "$os" != "ubuntu" ]; then
  43. header_info
  44. echo -e "${BL}[Info]${GN} Skipping ${name} ${RD}$container is not Debian or Ubuntu ${CL} \n"
  45. sleep 1
  46. continue
  47. fi
  48. status=$(pct status $container)
  49. template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
  50. if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
  51. echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n"
  52. pct start $container
  53. echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n"
  54. sleep 5
  55. clean_container $container
  56. echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n"
  57. pct shutdown $container &
  58. elif [ "$status" == "status: running" ]; then
  59. clean_container $container
  60. fi
  61. done
  62. wait
  63. header_info
  64. echo -e "${GN} Finished, Containers Cleaned. ${CL} \n"