Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. filePath="/var/log/nginx/"
  4. fileCountRetention=4
  5. fileSizeLimit=5M
  6.  
  7. vFound=0
  8. for fsName in $(find ${filePath} -type f -and -name "*.log" -and -size +${fileSizeLimit}); do
  9. vFound=1
  10. echo "[$(date +'%F %H:%M:%S')] - Found ${fsName} with size over ${fileSizeLimit}"
  11. echo "[$(date +'%F %H:%M:%S')] - Renaming ${fsName} to ${fsName}.1"
  12. mv ${fsName} ${fsName}.1
  13. for (( cur=${fileCountRetention}; cur>0; cur-- )); do
  14. if [[ ${cur} -gt 1 ]]; then
  15. let prev=${cur}-1
  16. prevExt=".${prev}.gz"
  17. curExt=".${cur}.gz"
  18.  
  19. if [[ ${cur} -eq ${fileCountRetention} ]] && [[ -f "${fsName}${curExt}" ]]; then
  20. echo "[$(date +'%F %H:%M:%S')] - Removing ${fsName}${curExt}"
  21. rm -f "${fsName}${curExt}"
  22. mv "${fsName}${prevExt}" "${fsName}${curExt}"
  23. else
  24. if [[ -f "${fsName}${prevExt}" ]]; then
  25. echo "[$(date +'%F %H:%M:%S')] - Renaming ${fsName}${prevExt} to ${fsName}${curExt}"
  26. mv "${fsName}${prevExt}" "${fsName}${curExt}"
  27. else
  28. echo "[$(date +'%F %H:%M:%S')] - ${fsName}${prevExt} not found"
  29. fi
  30. fi
  31. fi
  32. done
  33. done
  34.  
  35. if [[ ${vFound} -gt 0 ]]; then
  36. echo "[$(date +'%F %H:%M:%S')] - Reopening nginx log files"
  37. nginx -s reopen
  38. fi
  39.  
  40. for fsName in $(find ${filePath} -type f -and -name "*.log.1"); do
  41. echo "[$(date +'%F %H:%M:%S')] - Compressing ${fsName}"
  42. gzip -9 ${fsName}
  43. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement