clean-lxcs.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash
  2. function header_info {
  3. cat <<"EOF"
  4. ________ __ _ ________
  5. / ____/ /__ ____ _____ / / | |/ / ____/
  6. / / / / _ \/ __ `/ __ \ / / | / /
  7. / /___/ / __/ /_/ / / / / / /___/ / /___
  8. \____/_/\___/\__,_/_/ /_/ /_____/_/|_\____/
  9. EOF
  10. }
  11. set -e
  12. YW=$(echo "\033[33m")
  13. BL=$(echo "\033[36m")
  14. RD=$(echo "\033[01;31m")
  15. CM='\xE2\x9C\x94\033'
  16. GN=$(echo "\033[1;92m")
  17. CL=$(echo "\033[m")
  18. clear
  19. header_info
  20. echo -e "\n ${RD} USE AT YOUR OWN RISK. Deleting logs/cache may result in some apps/services broken!${CL} \n"
  21. while true; do
  22. read -p "This Will Clean logs, cache and update apt lists on all LXC Containers. Proceed(y/n)?" yn
  23. case $yn in
  24. [Yy]*) break ;;
  25. [Nn]*) exit ;;
  26. *) echo "Please answer yes or no." ;;
  27. esac
  28. done
  29. clear
  30. containers=$(pct list | tail -n +2 | cut -f1 -d' ')
  31. function clean_container() {
  32. container=$1
  33. clear
  34. header_info
  35. name=`pct exec $container hostname`
  36. echo -e "${BL}[Info]${GN} Cleaning ${name} ${CL} \n"
  37. pct exec $container -- bash -c "apt-get -y --purge autoremove && apt-get -y autoclean && bash <(curl -fsSL https://github.com/tteck/Proxmox/raw/dev/misc/clean.sh) && rm -rf /var/lib/apt/lists/* && apt-get update"
  38. }
  39. read -p "Skip stopped containers? [y/N]" -n 1 -r
  40. echo
  41. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  42. skip=no
  43. else
  44. skip=yes
  45. fi
  46. for container in $containers; do
  47. status=$(pct status $container)
  48. if [ "$skip" == "no" ]; then
  49. if [ "$status" == "status: stopped" ]; then
  50. echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n"
  51. pct start $container
  52. echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n"
  53. sleep 5
  54. clean_container $container
  55. echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n"
  56. pct shutdown $container &
  57. elif [ "$status" == "status: running" ]; then
  58. clean_container $container
  59. fi
  60. fi
  61. if [ "$skip" == "yes" ]; then
  62. if [ "$status" == "status: running" ]; then
  63. clean_container $container
  64. fi
  65. fi
  66. done
  67. wait
  68. clear
  69. header_info
  70. echo -e "${GN} Finished, Containers Cleaned. ${CL} \n"