clean-lxcs.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. while true; do
  21. read -p "This Will Clean logs, cache and update apt lists on all LXC Containers. Proceed(y/n)?" yn
  22. case $yn in
  23. [Yy]*) break ;;
  24. [Nn]*) exit ;;
  25. *) echo "Please answer yes or no." ;;
  26. esac
  27. done
  28. clear
  29. containers=$(pct list | tail -n +2 | cut -f1 -d' ')
  30. function clean_container() {
  31. container=$1
  32. clear
  33. header_info
  34. echo -e "${BL}[Info]${GN} Cleaning${BL} $container${CL} \n"
  35. pct exec $container -- bash -c "apt-get -y autoremove && apt-get -y autoclean && rm -rf /var/{cache,log}/* /var/lib/apt/lists/* && apt-get update"
  36. }
  37. read -p "Skip stopped containers? " -n 1 -r
  38. echo
  39. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  40. skip=no
  41. else
  42. skip=yes
  43. fi
  44. for container in $containers; do
  45. status=$(pct status $container)
  46. if [ "$skip" == "no" ]; then
  47. if [ "$status" == "status: stopped" ]; then
  48. echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL} \n"
  49. pct start $container
  50. echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL} \n"
  51. sleep 5
  52. clean_container $container
  53. echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL} \n"
  54. pct shutdown $container &
  55. elif [ "$status" == "status: running" ]; then
  56. clean_container $container
  57. fi
  58. fi
  59. if [ "$skip" == "yes" ]; then
  60. if [ "$status" == "status: running" ]; then
  61. clean_container $container
  62. fi
  63. fi
  64. done
  65. wait
  66. echo -e "${GN} Finished, All Containers Cleaned. ${CL} \n"