Created
July 21, 2013 17:08
-
-
Save Swop/6049178 to your computer and use it in GitHub Desktop.
Rotate multi-sites Nginx logs like cronolog style. The logs must organized like so: /foo/bar/mysite1/mysite1_access.log, /foo/bar/mysite1/mysite1_error.log, /foo/bar/mysite2/mysite2_access.log ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
LOG_DIR="/srv/data-srv1/logs/nginx" | |
NGINX_PID_FILE="/var/run/nginx.pid" | |
DATE_DIR=`date +%Y/%m/%d/%H` | |
cd $LOG_DIR | |
for dir in `ls`; do | |
if [ ! -d $dir ]; then | |
continue; | |
fi | |
DO_ROTATE=false | |
if [ -f ${dir}/${dir}_access.log ]; then | |
/bin/mkdir -p ${LOG_DIR}/${dir}/${DATE_DIR} > /dev/null 2>&1 | |
/bin/mv ${LOG_DIR}/${dir}/${dir}_access.log ${LOG_DIR}/${dir}/${DATE_DIR}/${dir}_access.log | |
#/bin/cp ${LOG_DIR}/${dir}/${dir}_access.log ${LOG_DIR}/${dir}/${DATE_DIR}/${dir}_access.log | |
DO_ROTATE=true | |
fi | |
if [ -f ${dir}/${dir}_error.log ]; then | |
/bin/mkdir -p ${LOG_DIR}/${dir}/${DATE_DIR} > /dev/null 2>&1 | |
/bin/mv ${LOG_DIR}/${dir}/${dir}_error.log ${LOG_DIR}/${dir}/${DATE_DIR}/${dir}_error.log | |
#/bin/cp ${LOG_DIR}/${dir}/${dir}_error.log ${LOG_DIR}/${dir}/${DATE_DIR}/${dir}_error.log | |
DO_ROTATE=true | |
fi | |
if $DO_ROTATE; then | |
kill -USR1 `cat $NGINX_PID_FILE` | |
find ${LOG_DIR}/${dir}/${DATE_DIR} -name '*.log' | xargs -n1 bzip2 -f -9 | |
#/bin/gzip ${log_dir}/${date_dir}/access.log & | |
#/bin/gzip ${log_dir}/${date_dir}/error.log & | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment