免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4625 | 回复: 4
打印 上一主题 下一主题

这个数据文件按天增量备份脚本有人在linux 上实现过吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-12 11:22 |只看该作者 |倒序浏览
-------------------------------------------------------------------------------
文件: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 "rocess 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,<='$PreNDate') print($0)}' `
    fi
done <<!
`ls *.log`
!

-------------------------------------------------------------------------------
文件:cpcdr
-------------------------------------------------------------------------------
#!/bin/ksh
#this file is called by bak_orgcdr.sh
bakpath="$1"
logfile="$2"
shift 2
for 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
        fi
done
#取日期的函数版内一个前辈写的
#这个脚本很简单,也许对大家有用

环境:HP-UX B.11.23 U ia64

我在linux as5上测试一看通不过:
[root@linux-as52 home]# ./back.sh
expr: warning: unportable BRE: &iexcl;°^.$&iexcl;±: using `^' as the first character
of the basic regular expression is not portable; it is being ignored
expr: warning: unportable BRE: &iexcl;°^.$&iexcl;±: using `^' as the first character
of the basic regular expression is not portable; it is being ignored
expr: warning: unportable BRE: &iexcl;°^.$&iexcl;±: using `^' as the first character
of the basic regular expression is not portable; it is being ignored
expr: warning: unportable BRE: &iexcl;°^.$&iexcl;±: using `^' as the first character
of the basic regular expression is not portable; it is being ignored
find: warning: you have specified the -depth option after a non-option argument (, but options are not positional (-depth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.
而且改了老问题又有新问题出现

论坛徽章:
0
2 [报告]
发表于 2008-09-12 11:23 |只看该作者
dd=`expr 0 - $days`
        expr $dd : "^.$" > /dev/null && dd=0$dd
        expr $mm : "^.$" > /dev/null && mm=0$mm
这几句是什么意思?

论坛徽章:
0
3 [报告]
发表于 2008-09-12 14:21 |只看该作者
原帖由 lanyuku 于 2008-9-12 11:23 发表
dd=`expr 0 - $days`
        expr $dd : "^.$" > /dev/null && dd=0$dd
        expr $mm : "^.$" > /dev/null && mm=0$mm
这几句是什么意思?



expr : expr
           The matching operator :  (colon)  compares  the  first
           argument  with  the  second argument, which must be an
           internationalized basic regular expression (BRE);  see
           regex(5) and NOTES. Normally, the /usr/bin/expr match-
           ing operator returns the number of bytes  matched  and
           the  /usr/xpg4/bin/expr  matching operator returns the
           number of characters matched (0 on  failure).  If  the
           second   argument  contains  at  least  one  BRE  sub-
           expression [\(...\)], the  matching  operator  returns
           the string corresponding to \1.

我琢磨着是这样的:
第一句是给dd赋值
第二三句是判断变量dd,mm是否包含有字符(不为空),如果有,则给其加上前导符0,如果没有,则啥也不做

你可以在执行前后分别echo一下那个变量看看,或者单独拿出来放在一个测试脚本里测试。

论坛徽章:
23
15-16赛季CBA联赛之吉林
日期:2017-12-21 16:39:27白羊座
日期:2014-10-27 11:14:37申猴
日期:2014-10-23 08:36:23金牛座
日期:2014-09-30 08:26:49午马
日期:2014-09-29 09:40:16射手座
日期:2014-11-25 08:56:112015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:0315-16赛季CBA联赛之山东
日期:2017-12-21 16:39:1915-16赛季CBA联赛之广东
日期:2016-01-19 13:33:372015亚冠之山东鲁能
日期:2015-10-13 09:39:062015亚冠之西悉尼流浪者
日期:2015-09-21 08:27:57
4 [报告]
发表于 2008-09-12 14:29 |只看该作者
原帖由 welcome008 于 2008-9-12 14:21 发表


第二三句是判断变量dd,mm是否包含有字符(不为空),如果有,则给其加上前导符0,如果没有,则啥也不做


mm表示月份,目的是把1-9月份变成01-09。
^.$ 这里的用意是判断 mm是不是只有一个字符。

论坛徽章:
0
5 [报告]
发表于 2008-09-12 14:40 |只看该作者
原帖由 ly5066113 于 2008-9-12 14:29 发表


mm表示月份,目的是把1-9月份变成01-09。
^.$ 这里的用意是判断 mm是不是只有一个字符。


学习了

需要把mm,dd格式化两字符的
谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP