################################file:bak_orgcdr.sh
#!/bin/ksh
######################################################
# 数据文件按天增量备份脚本
# 功能:按mtime将前一天的文件copy/link备份目录下,不删除原文件
# 执行频率:每天执行(crontab)
######################################################
##########function #############
#date=YYYYMMDD
#Usage:command date days
GetPrevDate(){
str=$1
days=$2
yy=`echo $str|cut -c 1-4`
mm=`echo $str|cut -c 5-6`
dd=`echo $str|cut -c 7-8`
sav_dd=$days
days=`expr $days - $dd`
while [ $days -ge 0 ]
do
mm=`expr $mm - 1`
[ $mm -eq 0 ] && mm=12 && yy=`expr $yy - 1`
aaa=`cal $mm $yy`
bbb=`echo $aaa|awk '{print $NF}'`
days=`expr $days - $bbb`
done
dd=`expr 0 - $days`
expr $dd : "^.$" > /dev/null && dd=0$dd
expr $mm : "^.$" > /dev/null && mm=0$mm
echo $yy$mm$dd
}
#======================main==================#
todayDate=`date "+%Y%m%d"`
#备份数据源目录
srcPath="/bill/data/pppath/bak/"
#备份目的目录
bakPath="/bill/backup/cdr/${todayDate}/"
#记录处理结果的日志文件目录
logPath="/bill/backup/logpath/cdr/"
#处理日志文件名
logFile="/bill/backup/logpath/cdr/${todayDate}.log"
if [ ! -d $bakPath ]; then
mkdir $bakPath;
fi
#生成时间戳文件,精确到分
beginDate="`GetPrevDate ${todayDate} 1`0001"
endDate="`GetPrevDate ${todayDate} 2`0001"
#for test
#beginDate="200412260001"
#endDate="200412270001"
touch -t "${beginDate}" /bill/backup/script/beginfile
touch -t "${endDate}" /bill/backup/script/endfile
echo "***********************************************************" >$logFile
echo "The cdr backup process is starting at [ `date` ]" >>$logFile
echo "Backup file's mtime between [ $beginDate - $endDate ) " >>$logFile
echo "SrcPath=/bill/data/pppath/bak/" >>$logFile
echo "BakPath=/bill/backup/cdr/${todayDate}/" >>$logFile
echo "Process log is written in $logFile " >>$logFile
echo "Backup files list as: " >>$logFile
#在源目录查找满足条件的文件,并拷贝到备份目录
find $srcPath /( -newer /bill/backup/script/beginfile -a ! -newer /bill/backup/script/endfile /) -depth -print|xargs cpcdr $bakPath $logFile;
echo "Backup cdr completed at [ `date` ]." >>$logFile
#检查处理日志看是否有错误
awk 'BEGIN {cnt=0;} {if($1~/ERROR/) cnt++;} END {if(cnt==0) printf("Backup Successed./n"); else printf("There is %d ERROR found.Please check it./n",cnt);}' $logFile >>$logFile
echo "***********************************************************" >>$logFile
PreNDate=`GetPrevDate ${todayDate} 31`
#echo $PreNDate
cd $logPath
#删除31天前的日志文件
while read file ;
do
if [ -f $file ]; then
rm -f `echo $file |awk '{if(substr($0,1,8)<='$PreNDate') print($0)}' `
fi
done <
`ls *.log`
!
################################file:cpcdr(此文件需要有x权限)#!/bin/ksh#this file is called by bak_orgcdr.shbakpath="$1"logfile="$2"shift 2for x in "$@"do #echo cp $x $bakpath; #cp -f $x $bakpath; ln -f $x $bakpath`basename $x` if [ $? -ne 0 ];then echo "ERROR! cp $x $bakpath failed! " >>$logfile else echo "cp $x $bakpath successed." >>$logfile fidone