#cut_nginxlog.sh
#!/bin/bash
#by author:albert.dong
#Mailto:dongliang101@gmail.com
# The Nginx logs path
#logs_path="/data/var/logs/tengine"
#logs_name=xxx.com-access.`date -d "+6 hour" +"%F_%H-%M.log"`
#mv ${logs_path}upstream_response_time.log ${logs_path}${upstream_logs_name}
#kill -USR1 `cat /var/run/nginx.pid`
#gzip ${logs_path}${logs_name}
#gzip ${logs_path}${upstream_logs_name}
#0 0 * * * /data/scripts/cut_nginxlog.sh
#######################################################
#set -x
Logs_path="/data/var/logs/tengine/"
NginxPid="/var/run/nginx.pid"
Bakdir="/data/var/logs/tengine/backup"
LogNameList="m.wehobi.com-access nginx_error"
Datedir=`date +%Y%m%d`
#mkdir -p ${Bakdir}/$datedir >/dev/null 2>&1
if [ ! -d "${Bakdir}/${Datedir}" ]; then
mkdir -p ${Bakdir}/${Datedir}
DateBak="${Bakdir}/${Datedir}"
echo $DateBak
fi
if test -d ${Logs_path};then
for name in ${LogNameList}
do
if test -e ${Logs_path}/${name}.log;then
#logs_name=${name}.`date -d "+6 hour" +"%F_%H-%M.log"`
logs_name=${name}.`date +%Y%m%d%H%M.log`
mv ${Logs_path}/${name}.log ${Bakdir}/${Datedir}/${logs_name}
kill -USR1 `cat ${NginxPid}`
gzip ${DateBak}/${logs_name}
#mv ${Logs_path}/${logs_name}.gz ${Bakdir}/${Datedir}
find ${Logs_path} -name "${name}.*.log.gz" -mtime +15 | xargs rm -rf
find ${Logs_path} -type f \( -name "*.gz" -o -name "*.log" \) -mtime +15 -delete
fi
done
fi