要求,因网站磁盘 空间有限,需每20天自动打包一次日志文件,然后将巳打包的日志删除。
#!/bin/bash
#write by zhanghuiyun
PATH=$PATH;export PATH
dest="/usr/local/apache/logs"
backupdir="/usr/local/apache/apachelog/"
filename="apachelog.tar.gz"
name="*-access*"
log="/var/log/apachelog"
date="`date --date="today"|awk '{print $2" "$3}'`"
filedate=`date +%Y%m%d`
excludefile="/usr/local/apache/apachelog/excludefile"
rmfile="/usr/local/apache/apachelog/rmfile"
#将当天的不需打包的访问日志文件列表导入一个文件
find $dest -name "*-access*" -type f -mtime -25|xargs ls -lat|grep "$date"|awk '{print $9}'>$excludefile
#将不需打包的错误 日志放到上面文件的末尾
find $dest -name "*.log" >>$excludefile
cd $backupdir
#tar cvzf fulllogs.tar.gz.$filedate $dest >/dev/null
#打包,-X是排除不需备份的文件
tar cvzf $filename.$filedate -X $excludefile $dest >$log
if [ $? -eq 0 ]
then
echo "`basename $0`,$date backup finished!" >>$log
#将备份好的文件列表导入一个文件为后面的删除动作提拱源
tar tvzf $filename.$filedate | awk '{print $6}' | awk -F"/" '{print $5}' | grep -v "error_log" | sed '/^$/d' >$rmfile
#删除巳备份的文件
for i in `cat $rmfile`
do
rm -f $dest/$i >>$log
done
else
echo "`basename $0`,$date backup failure!" >>$log
exit
fi
#tar tvzf $filename.$filedate | awk '{print $6}' | awk -F"/" '{print $5}' | grep -v "error_log" | sed '/^$/d' >$rmfile
#for i in `cat $rmfile`
#do
#rm -f $dest/$i >>$log
#done
阅读(1446) | 评论(0) | 转发(0) |