日期:2014-05-16 浏览次数:20690 次
0 3 1 * * /root/scripts/sweep_apache_log > /root/logs/sweep_apache_log.log 2>&1
# define saving days
SAVING_DAYS=90
# define log directory
LOG_DIR=/var/log/httpd
ACCESS_PREFIX='access.'
ERROR_PREFIX='error.'
LOG_SUFFIX='.log'
# compose archive file name
lastmonth=`date +%Y%m -d '2 months ago'`
for prefix in $ACCESS_PREFIX $ERROR_PREFIX ; do
arcfile=${LOG_DIR}/${prefix}${lastmonth}.log
# already archived?
if [ -f ${arcfile} ]; then
gzip -9 ${arcfile}
echo 'logs are already archived, but they are not compressed'
continue
fi
if [ -f ${arcfile}.gz ]; then
echo 'logs are already sweeped'
continue
fi
# ok, trying to cat them and then gzip it
cat ${LOG_DIR}/${prefix}${lastmonth}*${LOG_SUFFIX} > ${arcfile}
gzip -9 ${arcfile}
# delete individual logs
rm -f ${LOG_DIR}/${prefix}${lastmonth}*${LOG_SUFFIX}
# end of for
done
# delete old archive files
find ${LOG_DIR} -type f -name '*.gz' -mtime +${SAVING_DAYS} -exec rm -f {} \;
# EOF
0 3 1 * * /root/script