清理一个月前的日志

. ~/.bash_profile
#用于删除上一个月的日志
#后来发现LINYX上取上一个月直接可以date -d "-1 month" +%m悲剧
export LANG=en_US
#定义清理日志路径
CLEAR_PATH="/oracle/app/diag/rdbms/dwh/dwh/trace/"
MONTH=`date +"%m"`
#echo $MONTH
if [ "$MONTH" -eq "01" ]||[  "$MONTH" -eq "1" ] ;then
	LMONTH=12
	echo $LMONTH
else
	LMONTH_TMP=`expr $MONTH - 1`
	echo $LMONTH_TMP > tmp.txt
	num=`awk '{print length($0)}' tmp.txt`
	if test "$num" -eq "1" ;
	then
		LMONTH=`echo "0"$LMONTH_TMP`
		echo $LMONTH
	else
		LMONTH=$LMONTH_TMP
		echo $LMONTH
	fi
fi
statistics()
	{
		if test "$LMONTH" = "01" ;then
			LMONTH=Jan
		elif [ "$LMONTH" = "02" ];then
			LMONTH=Feb
		elif [  "$LMONTH" = "03" ];then
			LMONTH=Mar
		elif [  "$LMONTH" = "04" ];then
			LMONTH=Apr
		elif [  "$LMONTH" = "05" ];then
			LMONTH=May
		elif [  "$LMONTH" = "06" ];then
			LMONTH=Jun
		elif [  "$LMONTH" = "07" ];then
			LMONTH=Jul
		elif [  "$LMONTH" = "08" ];then
			LMONTH=Aug
		elif [  "$LMONTH" = "09" ];then
			LMONTH=Sep
		elif [  "$LMONTH" = "10" ];then
			LMONTH=Oct
		elif [  "$LMONTH" = "11" ];then
			LMONTH=Nov
		elif [  "$LMONTH" = "12" ];then
			LMONTH=Dec
		fi
	}
	statistics
	echo "last month is $LMONTH"
	cd $CLEAR_PATH
	#构建脚本
	#ls -lt | awk 'BEGIN{print "'"$LMONTH"'"}{if ($6~/'$LMONTH'/) print $0}' | awk '{if ($9~/log/) print $9}' >$HOME/tools/file
	ls -lt | awk 'BEGIN{print "'"$LMONTH"'"}{if ($6~/'$LMONTH'/) print $0}' | awk '{if ($9~/trc$/) print $9}' > $HOME/tools/file
	sed -n 's/^/& rm  /p' $HOME/tools/file > $HOME/tools/rm.sh

	sed '1s/^/&tar -cvf $YEAR$MONTH.tar /' $HOME/tools/file   >  $HOME/tools/.1
	sed 's/$/& \\/' $HOME/tools/.1 | sed '$s/\\//' > $HOME/tools/.2
	#第1行插入
	sed '1 i \YEAR=`date '+%Y'`' $HOME/tools/.2 > $HOME/tools/.3
	sed '1 i \MONTH=`date -d "-1 month" +%m`' $HOME/tools/.3  > $HOME/tools/.4
	sed '1 i \cd $CLEAR_PATH' $HOME/tools/.4 > $HOME/tools/.5
	sed '1 i \CLEAR_PATH="/oracle/app/diag/rdbms/dwh/dwh/trace/"' $HOME/tools/.5 > $HOME/tools/tar.sh
	sh $HOME/tools/tar.sh
	if test "$?" = "0" ;then
		echo "打包成功"
		cd $CLEAR_PATH
		sh $HOME/tools/rm.sh
		echo "删除文件成功"
		fi

你可能感兴趣的:(清理一个月前的日志)