nginx日记分割脚本

来源:互联网 发布:linux export source 编辑:程序博客网 时间:2024/06/05 16:59

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

#!/bin/bash# backup log file for nginx## author: yagas# msn: yagas60@msn.com# date: 2011/11/25pidfile="/var/run/nginx.pid"logdir="/usr/local/nginx/logs"bakdir="/image/logs"today=(`date -d "today" +"%Y-%m-%d"`)if [ ! -e $logdir ]; then    echo "not found directory: ${logdir}"    exitfimkdir /tmp/$today > /dev/null 2>&1res=$?if [ $res == 1 ]; then    if [ ! -e $today ]; then        echo "failed make directory: /tmp/${today}"exit    fifimv ${logdir}/* /tmp/$todaykill -HUP `cat ${pidfile}`cd /tmptar -czvf ${bakdir}/log_${today}.tgz $today > /dev/null 2>&1res=$?if [ $res == 0 ]; then    echo "backup successfully"else    echo "backup failed"firm -Rf $todayexit