此脚本实现把nginx日志按时期进行分割备份。可以添加到crond内定期执行。
#!/bin/bash
# backup log file for nginx
#
# author: yagas
# msn: yagas60@msn.com
# date: 2011/11/25
pidfile="/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}"
exit
fi
mkdir /tmp/$today > /dev/null 2>&1
res=$?
if [ $res == 1 ]; then
if [ ! -e $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