clean.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/bash
  2. #########################################################################
  3. # Script for cleaning logs #
  4. # Created by DoIT #
  5. #########################################################################
  6. #------------------------------ VERSION --------------------------------#
  7. VER="1.0"
  8. #------------------------------ HISTORY --------------------------------#
  9. chhis(){
  10. echo -e "- \e[1;36mACTUAL script version [$VER]\e[0m\e[1;35m"
  11. echo -e "- \e[1;33mCleaning\e[0m [\e[1;35mhistory\e[0m]"
  12. HIS=$(find /home/ -name .bash_history | egrep "pmacko|doit")
  13. for h in $HIS; do rm -rf $h; done
  14. rm -rf /root/.bash_history
  15. test -d /opt/log2ram && rm -rf /opt/log2ram*
  16. history -c
  17. }
  18. #------------------------------- LOGS ----------------------------------#
  19. chlog(){
  20. echo -e "- \e[1;33mCleaning\e[0m [\e[1;35mlogs\e[0m]"
  21. find /var/log/ -name "*-*" -type f -delete
  22. find /var/log/ -name "*.gz" -type f -delete
  23. find /var/log/ -name "*.log.*" -type f -delete
  24. find /var/log/ -name "*.[0-9]*" -type f -delete
  25. find /var/log/* -size +10M -exec rm -rf {} \;
  26. test -d /var/lib/clamav && rm -rf /var/lib/clamav/tmp*
  27. test -f /usr/local/bin/log2ram && systemctl restart log2ram > /dev/null 2>&1
  28. test -d /var/ossec/logs/alerts/ && find /var/ossec/logs/alerts/ -type f -mtime +30 -exec rm -f {} \;
  29. test -d /var/ossec/logs/archives/ && find /var/ossec/logs/archives/ -type f -mtime +30 -exec rm -f {} \;
  30. sleep 1
  31. systemctl restart logrotate
  32. sleep 1
  33. systemctl restart rsyslog
  34. }
  35. #----------------------------- PACKAGES --------------------------------#
  36. chpac(){
  37. echo -e "- \e[1;33mCleaning\e[0m packages [\e[1;35marchives\e[0m]"
  38. test -f /usr/bin/apt && apt autoclean > /dev/null 2>&1
  39. test -f /usr/bin/apt && apt clean > /dev/null 2>&1
  40. test -f /usr/bin/apt && apt autoremove -y > /dev/null 2>&1
  41. test -f /usr/bin/yum && yum clean all > /dev/null 2>&1
  42. test -f /usr/bin/pip && /usr/bin/pip cache purge > /dev/null 2>&1
  43. test -f /usr/bin/pip3 && /usr/bin/pip3 cache purge > /dev/null 2>&1
  44. }
  45. #----------------------------- JOURNAL ---------------------------------#
  46. chjou(){
  47. echo -e "- \e[1;33mCleaning\e[0m [\e[1;35mjournal\e[0m]"
  48. journalctl --vacuum-size=100M > /dev/null 2>&1
  49. }
  50. #------------------------------ SPACE ----------------------------------#
  51. chsiz(){
  52. echo -e "- \e[1;32mActual size\e[0m of [\e[1;35mpartition\e[0m]"
  53. OVER=$(df -h | grep -v tmpfs | sed 's/ on//g' | grep "8[0-9]%" | awk '{print $1}' | head -1)
  54. if [ ! -z "$OVER" ];
  55. then
  56. df -h | grep -v tmpfs | sed 's/ on//g' | grep -v "$OVER"
  57. HIGH=$(df -h | grep -v tmpfs | sed 's/ on//g' | grep "$OVER")
  58. echo -e "\e[1;31m$HIGH\e[0m"
  59. else
  60. df -h | grep -v tmpfs | sed 's/ on//g'
  61. fi
  62. }
  63. #------------------------------ CORE ----------------------------------#
  64. chhis
  65. chlog
  66. chpac
  67. chjou
  68. chsiz