#!/bin/sh
if [ $# != 6 ];
then
echo "Usage: $0 <dir> <pre> <rm-day> <tar-day> <app_num> <dir_num>"
echo "e.g.: $0 /data/invoice flow 7 3 55 1000"
exit 0
fi
vdir=$1 #/data/invoice
vpre=$2 #flow
vrmday=$3 #7
vtarday=$4 #3
vappnum=$5 #55
vdirnum=$6 #1000
function backup_dir()
{
local i=0
if [ ! -d $1 ]; then
return
fi
rm_file="$2_"`date -d "$3 days ago" +%Y%m%d`".tar.gz"
logfile="$2_"`date -d "$4 days ago" +%Y%m%d`"*.log"
tarfile="$2_"`date -d "$4 days ago" +%Y%m%d`".tar.gz"
cd $1
for i in `seq 1 $vdirnum`
do
cd $i
rm -f ${rm_file}
tar cvf ${tarfile} ${logfile}
rm -f ${logfile}
cd -
done
}
function remove_dir()
{
local i=0
if [ ! -d $1 ]; then
return
fi
cd $1
for i in `seq 1 $vdirnum`
do
cd $i
rm -f *.tar.gz
tarfile="$2_"`date -d "$4 days ago" +%Y%m%d`"*.log"
rm -f $tarfile
cd -
done
}
#特殊的appid
arr=(18 34 16 41 5 10 36 40 45 55)
#删除指定时间段的数据
rmdate=(14 14 15 3 10 10 10 31 20)
#打包指点时间段的数据
tardate=(7 7 14 3 7 7 7 30 30)
appid=($(seq 1 1 ${vappnum}))
for var in ${arr[*]}
do
f=$(($var - 1))
unset appid[$f]
done
#测试
#unset appid
#appid=(1)
for var in ${appid[*]}
do
cur_dir="$vdir/$var/"
backup_dir $cur_dir $vpre $vrmday $vtarday
done
len=${#arr[@]}
len=$(( $len - 1 )) # 55特殊处理
i=0
while [ $i -lt $len ]
do
appid=${arr[$i]}
cur_dir="$vdir/$appid"
if [[ "$appid" = "41" || "$appid" = "45" ]];
then
remove_dir $cur_dir $vpre ${rmdate[$i]} ${tardate[$i]}
else
backup_dir $cur_dir $vpre ${rmdate[$i]} ${tardate[$i]}
fi
i=$(( $i + 1 ))
done