Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- filePath="/var/log/nginx/"
- fileCountRetention=4
- fileSizeLimit=5M
- vFound=0
- for fsName in $(find ${filePath} -type f -and -name "*.log" -and -size +${fileSizeLimit}); do
- vFound=1
- echo "[$(date +'%F %H:%M:%S')] - Found ${fsName} with size over ${fileSizeLimit}"
- echo "[$(date +'%F %H:%M:%S')] - Renaming ${fsName} to ${fsName}.1"
- mv ${fsName} ${fsName}.1
- for (( cur=${fileCountRetention}; cur>0; cur-- )); do
- if [[ ${cur} -gt 1 ]]; then
- let prev=${cur}-1
- prevExt=".${prev}.gz"
- curExt=".${cur}.gz"
- if [[ ${cur} -eq ${fileCountRetention} ]] && [[ -f "${fsName}${curExt}" ]]; then
- echo "[$(date +'%F %H:%M:%S')] - Removing ${fsName}${curExt}"
- rm -f "${fsName}${curExt}"
- mv "${fsName}${prevExt}" "${fsName}${curExt}"
- else
- if [[ -f "${fsName}${prevExt}" ]]; then
- echo "[$(date +'%F %H:%M:%S')] - Renaming ${fsName}${prevExt} to ${fsName}${curExt}"
- mv "${fsName}${prevExt}" "${fsName}${curExt}"
- else
- echo "[$(date +'%F %H:%M:%S')] - ${fsName}${prevExt} not found"
- fi
- fi
- fi
- done
- done
- if [[ ${vFound} -gt 0 ]]; then
- echo "[$(date +'%F %H:%M:%S')] - Reopening nginx log files"
- nginx -s reopen
- fi
- for fsName in $(find ${filePath} -type f -and -name "*.log.1"); do
- echo "[$(date +'%F %H:%M:%S')] - Compressing ${fsName}"
- gzip -9 ${fsName}
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement