#!/bin/sh
fid_dir="./pile"
bak_dir="./baklog" #save *tar.gz dir
log_dir="./log.bak.tar" #get log.bak dir
bak_fn=`date +%Y%m%d%H%M%S`.tar.gz
savecnt=4
########################################
findname="$bak_dir/*.tar.gz"
D1="1"
D2="2"
D3="3"
D4="4"
D5="5"
D6="6"
########################################
#create dir
if [ ! -d "$bak_dir" ]; then
mkdir $bak_dir
fi
#found saved files
for k in `ls $findname 2>/dev/null`;do
if [ "$k" = "$bak_dir/1.tar.gz" ];then
D1=$k
elif [ "$k" = "$bak_dir/2.tar.gz" ];then
D2=$k
elif [ "$k" = "$bak_dir/3.tar.gz" ];then
D3=$k
elif [ "$k" = "$bak_dir/4.tar.gz" ];then
D4=$k
elif [ "$k" = "$bak_dir/5.tar.gz" ];then
D5=$k
elif [ "$k" = "$bak_dir/6.tar.gz" ];then
D6=$k
else
rm -f $k
fi
done
#get new ID
i=0
if [ "$D1" != "$bak_dir/1.tar.gz" ];then
i=1
elif [ "$D2" != "$bak_dir/2.tar.gz" ];then
i=2
elif [ "$D3" != "$bak_dir/3.tar.gz" ];then
i=3
elif [ "$D4" != "$bak_dir/4.tar.gz" ];then
i=4
elif [ "$D5" != "$bak_dir/5.tar.gz" ];then
i=5
elif [ "$D6" != "$bak_dir/6.tar.gz" ];then
i=6
fi
#next file and dlt
dlt=$((i+1))
if [ $dlt -gt 6 ] || [ $dlt -gt $(($savecnt+1)) ];then
dlt=1
fi
if [ -f "$bak_dir/$dlt.tar.gz" ];then
rm -f "$bak_dir/$dlt.tar.gz"
fi
#find files then tar all. bak it to bak_dir
if [ $dlt -eq 2 ];then
#find $fid_dir -name "*.ini" > "$bak_dir/m5"
find $fid_dir -name "*.log" -or -name "*.ini" -or -name "*.sh" | xargs tar -zcf "$bak_dir/$i.tar.gz"
else
find $fid_dir -name "*.log" | xargs tar -zcf "$bak_dir/$i.tar.gz"
fi
#DO tar
#tar -cf $log_dir $bak_dir/*.tar.gz