nginx日记分割脚本

此脚本实现把nginx日志按时期进行分割备份。可以添加到crond内定期执行。

#!/bin/bash

# backup log file for nginx
#
# author: yagas
# msn: [email protected]
# date: 2011/11/25

pidfile="/var/run/nginx.pid"
logdir="/usr/local/nginx/logs"
bakdir="/image/backup/logs"
today=(`date -d "today" +"%Y-%m-%d"`)

# check log dir exist
if [ ! -e $logdir ]; then
        echo "not found directory: ${logdir}"
        exit
fi

# check pid file exist
if [ ! -e $pidfile ]; then
        echo "not found pid file: ${pidfile}"
        exit
fi

mkdir /tmp/$today > /dev/null 2>&1
res=$?

if [ $res == 1 ]; then
        if [ ! -e /tmp/$today ]; then
                echo "failed make directory: /tmp/${today}"
  exit
        fi
fi

mv ${logdir}/* /tmp/$today
kill -HUP `cat ${pidfile}`

cd /tmp
tar -czvf ${bakdir}/log_${today}.tgz $today > /dev/null 2>&1
res=$?

if [ $res == 0 ]; then
        echo "backup successfully"
else
        echo "backup failed"
fi

rm -Rf $today
exit

你可能感兴趣的:(nginx,shell,职场,休闲)