logs.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. # LGSM logs.sh function
  3. # Author: Daniel Gibbs
  4. # Contributor: UltimateByte
  5. # Website: https://gameservermanagers.com
  6. # Description: Acts as a log rotater, removing old logs.
  7. local commandname="LOGS"
  8. local commandaction="Log-Manager"
  9. # Check if logfile variable and file exist, create logfile if it doesn't exist
  10. if [ -n "${consolelog}" ]; then
  11. if [ ! -e "${consolelog}" ]; then
  12. touch "${consolelog}"
  13. fi
  14. fi
  15. # For games not displaying a console, and having logs into their game directory
  16. if [ "${function_selfname}" == "command_start.sh" ] && [ -n "${gamelogfile}" ]; then
  17. if [ -n "$(find "${systemdir}" -name "gamelog*.log")" ]; then
  18. fn_print_info "Moving game logs to ${gamelogdir}"
  19. fn_script_log_info "Moving game logs to ${gamelogdir}"
  20. echo -en "\n"
  21. sleep 1
  22. mv "${systemdir}"/gamelog*.log "${gamelogdir}"
  23. fi
  24. fi
  25. # Log manager will start the cleanup if it finds logs older than "${logdays}"
  26. if [ $(find "${scriptlogdir}"/ -type f -mtime +"${logdays}"|wc -l) -ne "0" ]; then
  27. fn_print_dots "Starting"
  28. # Set common logs directories
  29. commonlogs="${systemdir}/logs"
  30. commonsourcelogs="${systemdir}/*/logs"
  31. # Set addon logs directories
  32. sourcemodlogdir="${systemdir}/addons/sourcemod/logs"
  33. ulxlogdir="${systemdir}/data/ulx_logs"
  34. darkrplogdir="${systemdir}/data/darkrp_logs"
  35. legacyserverlogdir="${rootdir}/log/server"
  36. # Setting up counting variables
  37. scriptcount="0" ; consolecount="0" ; gamecount="0" ; srcdscount="0" ; smcount="0" ; ulxcount="0" ; darkrpcount="0" ; legacycount="0"
  38. sleep 1
  39. fn_print_ok_nl "Starting"
  40. fn_print_info_nl "Removing logs older than "${logdays}" days"
  41. fn_script_log_info "Removing logs older than "${logdays}" days"
  42. # Logging logfiles to be removed according to "${logdays}", counting and removing them
  43. # Script logfiles
  44. find "${scriptlogdir}"/ -type f -mtime +"${logdays}"| tee >> "${scriptlog}"
  45. scriptcount=$(find "${scriptlogdir}"/ -type f -mtime +"${logdays}"|wc -l)
  46. find "${scriptlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  47. # SRCDS and unreal logfiles
  48. if [ "${engine}" == "unreal2" ]||[ "${engine}" == "source" ]; then
  49. find "${gamelogdir}"/ -type f -mtime +"${logdays}"| tee >> "${scriptlog}"
  50. gamecount=$(find "${gamelogdir}"/ -type f -mtime +"${logdays}"|wc -l)
  51. find "${gamelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  52. fi
  53. # Console logfiles
  54. if [ -n "${consolelog}" ]; then
  55. find "${consolelogdir}"/ -type f -mtime +"${logdays}"| tee >> "${scriptlog}"
  56. consolecount=$(find "${consolelogdir}"/ -type f -mtime +"${logdays}"|wc -l)
  57. find "${consolelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  58. fi
  59. # Common logfiles
  60. if [ -d ${commonlogs} ]; then
  61. find "${commonlogs}"/ -type f -mtime +"${logdays}"| tee >> "${scriptlog}"
  62. smcount=$(find "${commonlogs}"/ -type f -mtime +"${logdays}"|wc -l)
  63. find "${commonlogs}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  64. fi
  65. if [ -d ${commonsourcelogs} ]; then
  66. find "${commonsourcelogs}"/* -type f -mtime +"${logdays}"| tee >> "${scriptlog}"
  67. smcount=$(find "${commonsourcelogs}"/* -type f -mtime +"${logdays}"|wc -l)
  68. find "${commonsourcelogs}"/* -mtime +"${logdays}" -type f -exec rm -f {} \;
  69. fi
  70. # Source addons logfiles
  71. if [ "${engine}" == "source" ]; then
  72. # SourceMod logfiles
  73. if [ -d "${sourcemodlogdir}" ]; then
  74. find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}"| tee >> "${scriptlog}"
  75. smcount=$(find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}"|wc -l)
  76. find "${sourcemodlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  77. fi
  78. # Garry's Mod logfiles
  79. if [ "${gamename}" == "Garry's Mod" ]; then
  80. # ULX logfiles
  81. if [ -d "${ulxlogdir}" ]; then
  82. find "${ulxlogdir}"/ -type f -mtime +"${logdays}"| tee >> "${scriptlog}"
  83. ulxcount=$(find "${ulxlogdir}"/ -type f -mtime +"${logdays}"|wc -l)
  84. find "${ulxlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  85. fi
  86. # DarkRP logfiles
  87. if [ -d "${darkrplogdir}" ]; then
  88. find "${darkrplogdir}"/ -type f -mtime +"${logdays}"| tee >> "${scriptlog}"
  89. darkrpcount=$(find "${darkrplogdir}"/ -type f -mtime +"${logdays}"|wc -l)
  90. find "${darkrplogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  91. fi
  92. fi
  93. fi
  94. # Legacy support
  95. if [ -d "${legacyserverlogdir}" ]; then
  96. find "${legacyserverlogdir}"/ -type f -mtime +"${logdays}"| tee >> "${scriptlog}"
  97. legacycount=$(find "${legacyserverlogdir}"/ -type f -mtime +"${logdays}"|wc -l)
  98. find "${legacyserverlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  99. # Remove directory if empty
  100. if [ ! "$(ls -A "${legacyserverlogdir}")" ]; then
  101. rm -rf "${legacyserverlogdir}"
  102. fi
  103. fi
  104. # Count total amount of files removed
  105. count=$((${scriptcount} + ${consolecount} + ${gamecount} + ${srcdscount} + ${smcount} + ${ulxcount} + ${darkrpcount} + ${legacycount}))
  106. # Job done
  107. fn_print_ok_nl "Removed ${count} log files"
  108. fn_script_log "Removed ${count} log files"
  109. fi