shell打包上个月的日志文件

来源:互联网 发布:db2 查看数据库版本 编辑:程序博客网 时间:2024/05/18 06:52
#!/bin/sh
yy=`date +%Y`
mm=`date +%m`
dd=`date +%d`
#echo $yy-$mm-$dd
if [ $mm -eq '01' ] ; then
taryear=`expr $yy - 1`;
tarmm=12;
else
taryear=$yy;
tarmm=`expr $mm - 1`;
fi
if [ $tarmm  -lt 10 ] ; then
tarmm=0$tarmm
fi
#echo $taryear-$tarmm
filepath="/var/shell/logs"
tar -zcvf $filepath/test.$taryear-$tarmm.tar.gz $filepath/test.$taryear-$tarmm-*.log
#rm $filepath/test.$taryear-$tarmm-*.log

#########delete 6 month ago
if [ $mm -lt '07' ] ; then
delyear=`expr $yy - 1`;
delmm=`expr $mm + 6`;
else
delyear=$yy;
delmm=`expr $mm - 6`;
fi
if [ $delmm  -lt 10 ] ; then
delmm=0$delmm
fi
rm $filepath/test.$delyear-$delmm.tar.gz



可通过crontab每月定时执行。

原创粉丝点击