#!/bin/bash

date=$(date "+%H:%M")

date1=$(date +%F)

date2=$(date +%F -d last-day)

date3=$(date +%k)

date4=$(date +%M)

PID=/usr/local/nginx/logs/nginx.pid

bak_dir=/usr/local/nginx/logs/log_bak/${date1}

bak_name=$(hostname | awk -F'.' '{print $1}')

log_dir=/usr/local/nginx/logs


if [ ! -d $bak_dir ] ; then mkdir -p $bak_dir ; fi


cd $log_dir

for file in $(find . -type f -name "*.log" | grep -v error); do

   mv $file ${bak_dir}/${file}_${date}_bak

done

:> error.log


kill -USR1 `cat $PID`


if [[ $date3 -eq 4 && $date4 -lt 3 ]]; then

   cd $(dirname $bak_dir)

   tar zcf ${bak_name}_${date2}_nginx.tar.gz ${date2}

   rm -rf ${date2}

   find . -maxdepth 1 -type f -name "*.gz" -ctime +1 -exec rm -f {} \;

fi