fjzcau
作者fjzcau·2014-06-07 23:05
系统运维工程师·金融

Oracle数据库表清理

字数 59717阅读 2789评论 2赞 1
1)清理后的报告
-------------------------------------------------------------------------------------
|                                                                                   |
|                          Oracle数据库表清理日志报告                               |
|                                                                                   |
|                          制作: fjz  日期:20120912                                |
-------------------------------------------------------------------------------------
|序号     清理的表名               清理前表的记录数         清理后表的记录数        |
| 1       rxlinm06                 330316                   16812                   |
| 2       rxlinm06_20120908                                 158                     |
| 3       rxlinm06_20120909                                 172                     |
| 4       rxlinm06_20120910                                 7842                    |
| 5       rxlinm06_20120911                                 2251                    |
| 6       rxlinm06_20120912                                 2805                    |
| 7       rxlinm06_20120913                                 3584                    |
| 8       rxlinm06_20120914                                 0                       |
| 9       agl23mh0                 825368                   825368                  |
| 10      agl23mh0_20120908                                 0                       |
| 11      agl23mh0_20120909                                 96847                   |
| 12      agl23mh0_20120910                                 198942                  |
| 13      agl23mh0_20120911                                 180953                  |
| 14      agl23mh0_20120912                                 174872                  |
| 15      agl23mh0_20120913                                 173754                  |
| 16      agl23mh0_20120914                                 0                       |
| 17      agl36mh0                 186810                   186810                  |
| 18      agl36mh0_20120908                                 0                       |
| 19      agl36mh0_20120909                                 37335                   |
| 20      agl36mh0_20120910                                 37350                   |
| 21      agl36mh0_20120911                                 37365                   |
| 22      agl36mh0_20120912                                 37372                   |
| 23      agl36mh0_20120913                                 37388                   |
| 24      agl36mh0_20120914                                 0                       |
| 25      agl37m00                 825368                   825368                  |
| 26      agl37m00_20120908                                 0                       |
| 27      agl37m00_20120909                                 96847                   |
| 28      agl37m00_20120910                                 198942                  |
| 29      agl37m00_20120911                                 180953                  |
| 30      agl37m00_20120912                                 174872                  |
| 31      agl37m00_20120913                                 173754                  |
| 32      agl37m00_20120914                                 0                       |
| 33      agl85m00                 13878103                 928297                  |
| 34      agl85m00_20120908                                 102929                  |
| 35      agl85m00_20120909                                 96847                   |
| 36      agl85m00_20120910                                 198942                  |
| 37      agl85m00_20120911                                 180953                  |
| 38      agl85m00_20120912                                 174872                  |
| 39      agl85m00_20120913                                 173754                  |
| 40      agl85m00_20120914                                 0                       |
-------------------------------------------------------------------------------------

2)run_clean.ksh 运行调度的脚本
#!/bin/ksh

#######################################################
# 步骤 1 变量初始化
#######################################################
#-- sc_modify 生产环境变量 --#
#export ORACLE_BASE=/oracle
#export ORACLE_SID=coredb
#export ORACLE_HOSTNAME=Coredb
#export AIXTHREAD_SCOPE=S
#export ORACLE_HOME=$ORACLE_BASE/app/product/10.2.0/db
#export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$LD_LIBRARY_PATH
#export PATH=$ORACLE_HOME/bin:$PATH
#export DBCA_RAW_CONFIG=/oracle/dbca_raw_config
#export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK

#-- sc_modify 准生产环境变量 --#
export SHELL=/usr/bin/ksh
export ORACLE_BASE=/oracle
export ORACLE_SID=coredbzsc
export ORACLE_HOSTNAME=coredbzsc
export AIXTHREAD_SCOPE=S
export ORACLE_HOME=$ORACLE_BASE/app/product/10.2.0/db
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/lib32:$LD_LIBRARY_PATH
export PATH=$ORACLE_HOME/bin:$PATH
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK

CurDate=`date +%Y%m%d`;

bin_dir="/tzz/bin";
base_log_dir="/tzz/log";
log_dir="/tzz/log/tab_clean";

#-- sc_modify 生产dump路径是 /linshi , 准生产/orabak --#
dump_dir="/orabak";

sim1_log="${base_log_dir}/bsbsim1_tab_clean.log" ;
sim1_dmp_file="bsbsim1_clean_${CurDate}.dmp" ;
sim1_dmp_log="bsbsim1_clean_expdp_${CurDate}.log";
cat /dev/null > $sim1_log;

glrpt_log="${base_log_dir}/bsbglrpt_tab_clean.log";
glrpt_dmp_file="bsbglrpt_clean_${CurDate}.dmp";
glrpt_dmp_log="bsbglrpt_clean_expdp_${CurDate}.log";
cat /dev/null > $glrpt_log;

cd $dump_dir; #-- 进入 dump目录 --#


#######################################################
# 步骤 2 处理bsbsim1
#######################################################
echo "n==> 导表开始时间 `date +%Y%m%d_%H%M%S` " >> $sim1_log ;

if [ -r ${sim1_dmp_file} ]
then
      rm ${sim1_dmp_file} ;
fi

#######################################################
# 步骤 2-1 处理bsbsim1,先expdp,导表备份。
#######################################################
#-- sc_modify 生产 bsbsim1/sim1core  准生产 bsbsim1/password--#
expdp bsbsim1/password directory=linshi_dump tables=iqeybals,ieqsacts,ieqsigns,ieqtdtls,ivrfycss,intmsglg,rxlinm06,t60m0xxx,yupxxlog,iacctrep,iacctreq  dumpfile=$sim1_dmp_file  logfile=$sim1_dmp_log ;

echo "n=====> expdp 日志 ${dump_dir}/${sim1_dmp_log} 重定向到此:n" >> $sim1_log;
cat $sim1_dmp_log >> $sim1_log;
mv  $sim1_dmp_log  ${log_dir}/${sim1_dmp_log};
echo "n=====> expdp 日志 结束 n" >> $sim1_log;

if [ ! -r $sim1_dmp_file ]
then
    echo "=====> sim1 导表 失败 缺少 $sim1_dmp_file 文件"  >> $sim1_log ;
else
    echo "=====> sim1 导表 完成 时间 `date +%Y%m%d_%H%M%S` n" >> $sim1_log ;

 #######################################################
 # 步骤 2-2 处理bsbsim1,调用清表程序bsb_tab_clean.ksh
 #######################################################
    #-- 导表成功再清理 --#
    nohup ${bin_dir}/bsb_tab_clean.ksh 1 >>  $sim1_log   &
fi


#######################################################
# 步骤 3 处理bsbglrpt
#######################################################
echo "n==>导表开始时间 `date +%Y%m%d_%H%M%S` " >> $glrpt_log ;

if [ -r ${glrpt_dmp_file} ]
then
      rm ${glrpt_dmp_file} ;
fi

#######################################################
# 步骤 3-1 处理bsbglrpt,先expdp,导表备份。
#######################################################
#-- sc_modify 生产 bsbglrpt/glrptcor 准生产 bsbglrpt/password --#
expdp bsbglrpt/password directory=linshi_dump tables=rxlinm06,agl23mh0,agl36mh0,agl37m00,agl85m00 logfile=$glrpt_dmp_log dumpfile=$glrpt_dmp_file ;

echo "n=====> expdp 日志 ${dump_dir}/${glrpt_dmp_log} 重定向到此: n" >> $glrpt_log;
cat $glrpt_dmp_log >> $glrpt_log;
mv  $glrpt_dmp_log  ${log_dir}/${glrpt_dmp_log};
echo "n=====> expdp 日志 结束 n" >> $glrpt_log;

if [ ! -r $glrpt_dmp_file ]
then
    echo "=====> glrpt 导表 失败 缺少 $glrpt_dmp_file 文件"  >> $glrpt_log ;
else
    echo "=====> glrpt 导表 完成 时间 `date +%Y%m%d_%H%M%S` n" >> $glrpt_log ;

 #######################################################
 # 步骤 3-2 处理bsbglrpt,调用清表程序bsb_tab_clean.ksh
 #######################################################
    #-- 导表成功再清理 --#
    nohup ${bin_dir}/bsb_tab_clean.ksh 2 >>  $glrpt_log &
fi

3) bsb_tab_clean.ksh 主脚本
#!/bin/ksh

#======================================================================#
#                                                                      #
#                     通用函数,变量等测试                             #
#                                                                      #
#======================================================================#

function my_sh_test {
    # count=`echo ${count}+1|bc`
    # log_seq_no=`expr $log_seq_no + 1`
    # count=$(($count+1))
    # ((count+=1))

 i_dat="20120723";
 i_n=2;
 echo "------------ test date_day: $i_dat -------------";
 echo "-->1 next day:"`next_day $i_dat`",last day:"`last_day $i_dat`;
 echo "-->  next $i_n day:"`next_n_day $i_dat $i_n`",last $i_n day:"`last_n_day $i_dat $i_n`;
 echo "-->  add $i_n day:"`add_ndays $i_dat $i_n`;
 echo "-->  add -$i_n day:"`add_ndays $i_dat -$i_n`;
 echo "-->  cur mon days:"`cur_mon_days $i_dat`" ,last mon days:"`last_mon_days $i_dat`;
 
str="00123";
echo "-->3 str=$str";
#str=`echo $str | sed s/^0*//gi`;
str=`echo $str | bc`;
echo "-->4 str_0=$str";
str="99$str";
echo "-->5 str=$str";

((dd=1));
echo "-->6 dd=$dd";
dd=$((($dd-1)%12));
echo "-->7 dd=$dd";

typeset -i s;
typeset str;

str=`int_add 13 3`;
echo "-->8 add $strn";

s=`int_add 2 12`;
echo "n-->  s=$sn";

echo "--> substr测试: `substring "123456789" 0 5`";

echo "--> add_nhours -- yesterday:`add_nhours 24`";
echo "--> add_nhours -- tomorrow: `add_nhours -24`";
echo "----1------";
echo "----2------n";
echo "----3------";

exit 0;
}


#======================================================================#
#                                                                      #
#                           通用函数声明                               #
#                                                                      #
#======================================================================#

#----------------------------------------------------------#
# 函数名: cur_mon_days
# 用法  : cur_mon_days "20120723"; 返回 31
# 功能  : 计算给定日期所在月的天数
#----------------------------------------------------------#
function cur_mon_days {
    ((i_date=$1));
    year=`echo $i_date | cut -c 1-4 | bc`;
    mon=`echo $i_date | cut -c 5,6 | bc`;

    case "$mon" in
    1 | 3 | 5 | 7 | 8 | 10 | 12)
        ((days=31))
        ;;
    4 | 6 | 9 | 11)
        ((days=30))
        ;;
    *)
        days=xx
        ;;
    esac

    if [ $mon -eq 2 ]; then
        if (( (year%4 == 0 && year%100 != 0) || (year%400 == 0) )); then
            ((days=29));
        else
            ((days=28));
        fi
    fi

    echo $days;
}

#----------------------------------------------------------#
# 函数名: last_mon_days
# 用法  : last_mon_days "20120723"; 返回30
# 功能  : 计算给定日期所在上一个月的天数
#----------------------------------------------------------#
function last_mon_days {
    ((i_date=$1));
    year=`echo $i_date | cut -c 1-4 | bc`;
    mon=`echo $i_date | cut -c 5,6 | bc`;

    #-- last month --#
    mon=$((($mon-1)%12));
    if [ $mon -eq 0 ]; then
        ((mon=12));
    fi

    case "$mon" in
    1 | 3 | 5 | 7 | 8 | 10 | 12)
        ((days=31))
        ;;
    4 | 6 | 9 | 11)
        ((days=30))
        ;;
    *)
        days=xx
        ;;
    esac

    if [ $mon -eq 2 ]; then
        if (( (year%4 == 0 && year%100 != 0) || (year%400 == 0) )); then
            ((days=29));
        else
            ((days=28));
        fi
    fi

    echo $days;
}

#----------------------------------------------------------#
# 函数名: next_day
# 用法  : next_day "20120723";  返回 20120724
# 功能  : 计算给定日期下一天的日期
#----------------------------------------------------------#
function next_day {
    ((i_date=$1));
    year=`echo $i_date | cut -c 1-4`;
    mon=`echo $i_date | cut -c 5,6`;
    day=`echo $i_date | cut -c 7,8`;
    cur_m_days=`cur_mon_days $i_date`; # count days of current month

    day=`echo $day | bc`;
    mon=`echo $mon | bc`;

    if [ $day -ge $cur_m_days ]; then
        day="01";
        if [ $mon -ge 12 ]; then
            mon="01";
            ((year+=1));
        else
            ((mon+=1));
            if [ $mon -lt 10 ]; then
                mon="0$mon";   
            fi
        fi
    else
        ((day+=1));
        if [ $mon -lt 10 ]; then
            mon="0$mon";   
        fi
        if [ $day -lt 10 ]; then
            day="0$day";   
        fi
    fi
    echo "$year$mon$day";
}

#----------------------------------------------------------#
# 函数名: last_day
# 用法  : last_day "20120723";  返回 20120722
# 功能  : 计算给定日期前一天的日期
#----------------------------------------------------------#
function last_day {
    ((i_date=$1));
    year=`echo $i_date | cut -c 1-4`;
    mon=`echo $i_date | cut -c 5,6`;
    day=`echo $i_date | cut -c 7,8`;

    last_m_days=`last_mon_days $i_date`; # count days of last month

    day=`echo $day | bc`;
    mon=`echo $mon | bc`;

    if [ $day -le 1 ]; then
        day=$last_m_days;
        if [ $mon -le 1 ]; then
            mon=12;
            ((year-=1));
        else
            ((mon-=1));
            if [ $mon -lt 10 ]; then
                mon="0$mon";   
            fi
        fi
    else
        ((day-=1));
        if [ $mon -lt 10 ]; then
            mon="0$mon";   
        fi
        if [ $day -lt 10 ]; then
            day="0$day";   
        fi
    fi
    echo "$year$mon$day";
}

#----------------------------------------------------------#
# 函数名: next_n_day
# 用法  : next_n_day "20120723" 3;  返回 20120726
# 功能  : 计算给定日期将来n天的日期
# 说明  : n必须大于0
#----------------------------------------------------------#
function next_n_day {
    ((next_nday=$1));
    ((ndays=$2));

    while [ $ndays -gt 0 ]
    do
        next_nday=`next_day $next_nday`;
        ((ndays-=1));
    done
   
    echo "$next_nday";
}

#----------------------------------------------------------#
# 函数名: last_n_day
# 用法  : last_n_day "20120723" 3;  返回 20120720
# 功能  : 计算给定日期前n天的日期
# 说明  : n必须大于0
#----------------------------------------------------------#
function last_n_day {
    ((last_nday=$1));
    ((ndays=$2));

    while [ $ndays -gt 0 ]
    do
        last_nday=`last_day $last_nday`;
        ndays=$(($ndays-1));
    done
   
    echo "$last_nday";
}

#----------------------------------------------------------#
# 函数名: add_ndays
# 用法  : add_ndays "20120723" 3;  返回 20120726
#          add_ndays "20120723" -2; 返回 20120721
# 功能  : 计算给定日期相对n天的日期
# 说明  : n大于0,调用next_n_day;n小于0,调用last_n_day。
#----------------------------------------------------------#
function add_ndays {
    ((i_date=$1));
    ((ndays=$2));

    ((next_nday=$i_date));
    ((i=$ndays));

    if [ $i -ge 0 ]; then
        next_nday=`next_n_day $next_nday $i`;
    else
        i=`echo $i | cut -c 2-`;
        next_nday=`last_n_day $next_nday $i`;
    fi

    echo "$next_nday";
}

#----------------------------------------------------------#
# 函数名: add_nhours 假设当前日期20120723
# 用法  : add_nhours  24;  返回 20120722
#          add_nhours -48;  返回 20120725
# 功能  : 计算当前日期相对n小时的日期
# 说明  : 会改变环境变量,建议不用,推荐add_ndays函数
#----------------------------------------------------------#
function add_nhours {
    typeset -i nhours;
    ((nhours=$1));
    oldtz=`echo $TZ`;  #系统原时区 -8
   
    tz_hour=`echo $TZ|sed 's/.*(..)/1/'`; #取最后两位 -8
    new_tz=`echo $TZ|sed 's/..$//'`;
   
    if [ $nhours -ge 0 ]; then
      tz_hour=`expr $tz_hour + $nhours`;
    else
      nhours=`echo $nhours | cut -c 2-`;     #从第2个字节开始取$nhours串,即去掉负号
      tz_hour=`expr $tz_hour - $nhours`;   
    fi
   
    TZ=$new_tz$tz_hour;   
    export TZ;
    nhours_date=`date "+%Y%m%d"`;
    TZ=$oldtz;
    export TZ;
   
    echo $nhours_date;
}

#----------------------------------------------------------#
# 函数名: make_space_str
# 用法  : make_space_str 3; 返回长度为3的空格串:'   '
# 功能  : 生成空格串
#----------------------------------------------------------#
function make_space_str
{
    cnt=$1;
   
    str=" ";
    if [ $cnt -le 0 ]
    then
        echo "";
    fi

    while [ $cnt -gt 0 ]
    do
        str="$str ";
        cnt=`expr $cnt - 1`;
    done
   
    echo "$str";
}

#----------------------------------------------------------#
# 函数名: substring
# 用法  : substring "string" 开始位置 长度;
# 功能  : 返回子串
#----------------------------------------------------------#
function substring
{
    typeset string="$1" out
    typeset -i offset=$2 length=$3

    while (( offset > 0 ))
    do
        string="${string#?}"
        (( offset = offset - 1 ))
    done

    while (( length > 0 ))
    do
        out="$out${string%${string#?}}"
        string="${string#?}"
        (( length = length - 1 ))
    done

    # print "$out"
    echo "$out"
}

#----------------------------------------------------------#
# 函数名: int_add
# 用法  : int_add 1 2; 返回3
# 功能  : 整数相加
#----------------------------------------------------------#
function int_add {
    typeset -i sum;
    typeset -i i1;
    typeset -i i2;
   
    ((i1=$1));
    ((i2=$2));
    sum=$i1+$i2;
    echo $sum;
}

#----------------------------------------------------------#
# 函数名: write_log
# 用法  : write_log $logfile "log content";
# 功能  : 将日志内容 "log content" 追加写入 日志文件$logfile
#----------------------------------------------------------#
function write_log {
    logfile=$1;
    log_content=$2;
    if [ ! -f $logfile ]; then
        echo "-->log file --$logfile-- not exist!n" | tee -a $logfile;
    fi
    echo  "$log_content" | tee -a $logfile;
}


#======================================================================#
#                                                                      #
#                      BSB 清理表(定制)函数声明                      #
#                                                                      #
#======================================================================#

#----------------------------------------------------------#
# 函数名: bsb_truncate
# 用法  : bsb_truncate "表名" ;
# 功能  : truncate 表
#----------------------------------------------------------#
function bsb_truncate {
    tab_name=$1;
sqlplus -s /nolog << !
    $ORACLE_CONNECT
    set pages 0
    set head off
    set lines 200
    set feedback off
    spool ${LOGIN_USER}_sp.tmp
   
    truncate table $tab_name;   
   
    --在日志中记录语句
    select '    truncate table ${tab_name};' from dual;
   
    spool off
!
    cat ${LOGIN_USER}_sp.tmp >> $LOG_FILE;
    rm ${LOGIN_USER}_sp.tmp;
}

#----------------------------------------------------------#
# 函数名: bsb_create_tmp_table
# 用法  : bsb_create_tmp_table "表名" "列名" "日期" ;
# 功能  : 给定日期的数据插入到"表名_日期"中。
#----------------------------------------------------------#
function bsb_create_tmp_table {
    tab_name=$1;
    col_name=$2;
    tab_date=$3;
   
    where_condition=" ${col_name}='$tab_date' ";
   
    if [ "${tab_name}" = "intmsglg" ]; then
        where_condition=" to_char(${col_name},'yyyymmdd')='$tab_date' ";
        #write_log $LOG_FILE "  --> 特殊字段表:$where_condition" ;
    elif [ "${tab_name}" = "iacctrep" ]; then
        #-- iacctrep 表 在expdp成功后不保留数据 --#
        return 1;
    elif [ "${tab_name}" = "iacctreq" ]; then
        #-- iacctreq 表 在expdp成功后不保留数据 --#
        return 1;
    fi

sqlplus -s /nolog << !
    $ORACLE_CONNECT
    set pages 0
    set head off
    set lines 200
    set feedback off
    set serveroutput on
    spool ${LOGIN_USER}_sp.tmp
   
    --select '    > 1-1 删除临时表 drop table ${tab_name}_${tab_date}; ' from dual;
    --drop table ${tab_name}_${tab_date};
 
    create table ${tab_name}_${tab_date} as
    select * from ${tab_name} where $where_condition;
   
    --在日志中记录创建临时表的语句
    select '    1-1 创建临时表 create table ${tab_name}_${tab_date} as select * from ${tab_name} where  ${col_name}  = ' || '''' ||  '$tab_date' || '''' from dual;
   
    --在日志中记录临时表的记录数,临时表清理前为空
    select '    1-2 临时表记录数 2_${tab_name}_${tab_date}_count(*)=' || count(*)
    from ${tab_name}_${tab_date};
 
    spool off
!
    cat ${LOGIN_USER}_sp.tmp >> $LOG_FILE;
    rm ${LOGIN_USER}_sp.tmp;
}


#----------------------------------------------------------#
# 函数名: bsb_insert_table
# 用法  : bsb_insert_table "表名";
# 功能  : 保留前7天的数据
#----------------------------------------------------------#
function bsb_insert_table {
    tab_name=$1;
    col_name=$2;
    tab_date=$3;
   
    if [ "${tab_name}" = "iacctrep" ]; then
        #-- iacctrep 表 在expdp成功后不保留数据 --#
        return 1;
    elif [ "${tab_name}" = "iacctreq" ]; then
        #-- iacctreq 表 在expdp成功后不保留数据 --#
        return 2;
    fi
sqlplus -s /nolog << !
    $ORACLE_CONNECT
    set pages 0
    set head off
    set lines 200
    set feedback off
    spool ${LOGIN_USER}_sp.tmp
   
    insert into $tab_name select * from ${tab_name}_${tab_date};
    commit;
   
    --在日志中记录插入操作的语句
    select '    insert into $tab_name select * from ${tab_name}_${tab_date};' from dual;
 
  spool off
!
    cat ${LOGIN_USER}_sp.tmp >> $LOG_FILE;
    rm ${LOGIN_USER}_sp.tmp;
}

   
#----------------------------------------------------------#
# 函数名:bsb_before_clean_count
# 用法  :bsb_before_clean_count "表名";
# 功能  :清理前,计算表的记录数
#----------------------------------------------------------#
function bsb_before_clean_count {
    tab_name=$1;   
sqlplus -s /nolog << !
    $ORACLE_CONNECT
    set pages 0
    set head off
    set lines 200
    set feedback off
    spool ${LOGIN_USER}_sp.tmp
   
    --清理前原表的记录数
    select '    清理前原表的记录数 1_${tab_name}_count(*)=' || count(*) from $tab_name;   
    spool off
!
    cat ${LOGIN_USER}_sp.tmp >> $LOG_FILE;
    rm ${LOGIN_USER}_sp.tmp;
}
   
#----------------------------------------------------------#
# 函数名:bsb_after_clean_count
# 用法  :bsb_after_clean_count "表名";
# 功能  :清理完后,计算表的记录数
#----------------------------------------------------------#
function bsb_after_clean_count {
    tab_name=$1;
sqlplus -s /nolog << !
    $ORACLE_CONNECT
    set pages 0
    set head off
    set lines 200
    set serveroutput on
    set feedback off
    spool ${LOGIN_USER}_sp.tmp
   
    --清理后原表的记录数
    select '    清理后原表的记录数 2_${tab_name}_count(*)=' || count(*) from $tab_name;
   
    spool off
!
    cat ${LOGIN_USER}_sp.tmp >> $LOG_FILE;
    rm ${LOGIN_USER}_sp.tmp;
}

#----------------------------------------------------------#
# 函数名: bsb_result_row
# 用法  : bsb_result_row 序号 "表名" "$log";
# 功能  : 汇总每个表清理前后的记录数
#----------------------------------------------------------#
function bsb_result_row
{
    tab_name=$1;
    tab_no=$2;
    log=$3;
   
    tabnm_count=`sed -n "/1_${tab_name}_count(*)=d*/p" $LOG_FILE |
                    awk -F"[=]" '{print $2}' |
                    awk '{print $1}'`;
   
    tabnm_count2=`sed -n "/2_${tab_name}_count(*)=d*/p" $LOG_FILE |
                    awk -F"[=]" '{print $2}' |
                    awk '{print $1}'`;
   
    tabnm_len2=`echo ${#tabnm_count2}`;
    tabnm_len2=`expr 21 - $tabnm_len2`;
   
    sstr=`make_space_str $tabnm_len2`;
   
    printf "%-10s%-25s%-25s%-25sn" "| $tab_no" "$tab_name" "$tabnm_count" "$tabnm_count2 $sstr |"
      | tee -a $log;
}

#----------------------------------------------------------#
# 函数名: bsb_result
# 用法  : bsb_result "$log";
# 功能  : 汇总报告,报告日志:$log
#----------------------------------------------------------#
function bsb_result
{
    log=$1;
    cat /dev/null > $log;
    makdate=`date +%Y%m%d`;
    echo "n" | tee -a $log;
    echo "-------------------------------------------------------------------------------------" | tee -a $log;
    echo "|                                                                                   |" | tee -a $log;
    echo "|                             BSB 清理日志报告                                      |" | tee -a $log;
    echo "|                                                                                   |" | tee -a $log;
    echo "|                          制作: fjz  日期:$makdate                                |" | tee -a $log;
    echo "-------------------------------------------------------------------------------------" | tee -a $log;
    echo "|序号     清理的表名               清理前表的记录数         清理后表的记录数        |" | tee -a $log;

    cnt=0;
    for i_table_name in ${BSB_TABLE_CLEAN_NAME[*]}
    do
        #-- 统计原表的记录 --#
        cnt=`expr $cnt + 1`;   
        bsb_result_row  "$i_table_name" "$cnt" "$log";
       
        if [ "${i_table_name}" = "iacctrep" ]; then
            continue;
        elif [ "${i_table_name}" = "iacctreq" ]; then
            continue;
        fi
       
        #-- 统计临时表的记录 --#
        cur_day=${BEGIN_DAY};
        while [ 1=1 ]
        do
            cnt=`expr $cnt + 1`;   
            bsb_result_row  "${i_table_name}_${cur_day}" "$cnt" "$log";
           
            if [ "${cur_day}" = "${END_DAY}" ]; then           
                break;       
            fi
           
            #-- get next day for loop --#
            cur_day=`next_day $cur_day`;
        done
    done
       
    echo "-------------------------------------------------------------------------------------" | tee -a $log;
    echo "n" | tee -a $log;
}

#----------------------------------------------------------#
# 函数名: main
# 用法  : main;
# 功能  : 清表主函数
#----------------------------------------------------------#
function main {
    write_log  $LOG_FILE "n=====> 清理程序开始时间:`date +%Y%m%d_%H%M%S` n";
   
    write_log $LOG_FILE "=====> 用户 ${LOGIN_USER} 需要清理的表 ";
    cnt=0;
    for i_table_name in ${BSB_TABLE_CLEAN_NAME[*]}
    do
        write_log  $LOG_FILE  "  $cnt  ${i_table_name}" ;
        cnt=`expr $cnt + 1`;
    done
    write_log $LOG_FILE "n=====> 保留表的开始日期 BEGIN_DAY: ${BEGIN_DAY}, 结束日期 END_DAY:${END_DAY}n";

    i_cnt=0; #数组下标从0开始
   
    #-- 循环遍历要清理的表 --#
    for i_table_name in ${BSB_TABLE_CLEAN_NAME[*]}
    do
  write_log $LOG_FILE "nn==> 开始清理 第 $i_cnt 个表:$i_table_name 时间:`date +%Y%m%d_%H%M%S` ";
       
        #-- 1 创建临时表保存前7天的数据 --#
        write_log $LOG_FILE "n--> 第 1 步 创建临时表保存前7天的数据 ";
        cur_day=${BEGIN_DAY};
        while [ 1=1 ]
        do
            bsb_create_tmp_table "$i_table_name" "${BSB_TABLE_CLEAN_COLUMN_NAME[$i_cnt]}" "${cur_day}";
           
            if [ "${cur_day}" = "${END_DAY}" ]; then           
                break;       
            fi
            cur_day=`next_day $cur_day`; #下一天日期
        done
        #-- 2 清空前原表的数据量 --#
        write_log $LOG_FILE "--> 第 2 步 清空前原表的数据量 ";
        bsb_before_clean_count "$i_table_name";
        #-- 3 将原表数据清空 --#
        write_log $LOG_FILE "--> 第 3 步 将原表数据清空 ";
        bsb_truncate "$i_table_name";
        #-- 4 将前7天数据插入原表 --#
        cur_day=${BEGIN_DAY};
        write_log $LOG_FILE "--> 第 4 步 将前7天数据插入原表 ";
        while [ 1=1 ]
        do
            bsb_insert_table "$i_table_name"  "${BSB_TABLE_CLEAN_COLUMN_NAME[$i_cnt]}"  "${cur_day}";
           
            if [ "${cur_day}" = "${END_DAY}" ]; then           
                break;
            fi
           
            #-- 下一天日期 --#
            cur_day=`next_day $cur_day`;
        done
       
        #-- 5 清空后原表的数据量 --#
        write_log $LOG_FILE "--> 第 5 步 清空后原表的数据量 ";
        bsb_after_clean_count "$i_table_name";
       
        i_cnt=`expr $i_cnt + 1`; #下一个表
       
    done #end: 循环遍历要清理的表
   
    bsb_result "$BSB_TABLE_CLEAN_RESULT_LOGFILE"; #汇总报告

    write_log $LOG_FILE "n=====> 清理程序结束时间:`date +%Y%m%d_%H%M%S` n";
}


#################################################################################
#                      程序从这里开始,上面是函数的声明。
#                   -->       
#                      步骤 1 全局变量声明
#                      步骤 2 变量初始化
#                      步骤 3 清表主函数main
#################################################################################

#######################################################
# 步骤 1 全局变量声明
#######################################################
LOGIN_USER="*";                             # oracle用户名
LOGIN_PASSWD="*";                           # 用户密码
ORACLE_CONNECT="*";                         # 用sqlplus登录
typeset BSB_TABLE_CLEAN_NAME[1000];         # 存储清理表的数组
typeset BSB_TABLE_CLEAN_COLUMN_NAME[1000];  # 存储清理表对应列的数组
DEL_DAYS=6;                                 # 清理后保留"DEL_DAYS + 1 = 7天"的数据

#######################################################
# 步骤 2 变量初始化
#######################################################
if [ $# -ne 1 ]; then
    echo "n****************************************************";
    echo " 参数错误(0:debug 1:bsbsim1 or 2:bsbglrpt)";
    echo " 用法: $0 0 ";
    echo "****************************************************n";
    exit 1;
fi

USER_TYPE=$1;   # 选择用户参数

if [ $USER_TYPE -eq 0 ]; then
    my_sh_test;    # 测试函数

# bsbsim1 用户初始化   
elif [ $USER_TYPE -eq 1 ]; then
set -A BSB_TABLE_CLEAN_NAME        iqeybals ieqsacts ieqsigns ieqtdtls ivrfycss intmsglg  rxlinm06 t60m0xxx yupxxlog iacctrep iacctreq
set -A BSB_TABLE_CLEAN_COLUMN_NAME qfrndat  qfrndat  qfrndat  qfrndat  qfrndat  stime    txdate   actdate  fldate    msgrep   msgreq
    LOGIN_USER="bsbsim1";
    LOGIN_PASSWD="password"; # sc_modify
    ORACLE_CONNECT="conn ${LOGIN_USER}/${LOGIN_PASSWD}";   

# bsbglrpt 用户初始化   
elif [ $USER_TYPE -eq 2 ]; then
    set -A BSB_TABLE_CLEAN_NAME        rxlinm06 agl23mh0  agl36mh0 agl37m00 agl85m00;
    set -A BSB_TABLE_CLEAN_COLUMN_NAME txdate   hsdate    hsdate   a37vcdt  a85vcdt
    LOGIN_USER="bsbglrpt";
    LOGIN_PASSWD="password"; # sc_modify  生产 glrptcor  准生产 password
    ORACLE_CONNECT="conn ${LOGIN_USER}/${LOGIN_PASSWD}";
else
    echo "n****************************************************";
    echo " 参数错误(0:debug 1:bsbsim1 or 2:bsbglrpt)";
    echo " 用法: $0 0 ";
    echo "****************************************************n";
    exit 2;
fi

END_DAY=`date +%Y%m%d`;                         # 清表的结束日期 
BEGIN_DAY=`add_ndays $END_DAY -$DEL_DAYS`;      # 清表的开始日期
LOG_DIR="/tzz/log/tab_clean";                   # 日志目录
LOG_FILE="$LOG_DIR/${LOGIN_USER}_clean_${END_DAY}.log";   # 日志文件
# 日志报告汇总文件
BSB_TABLE_CLEAN_RESULT_LOGFILE="$LOG_DIR/${LOGIN_USER}_result_${END_DAY}.log";
cat /dev/null > $LOG_FILE;                  # 初始化日志

#######################################################
# 步骤 3 清表主函数main
#######################################################
main;


如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

1

添加新评论2 条评论

fjzcaufjzcau系统运维工程师金融
2014-06-07 23:09
编程语言这东西,不用就忘,现在比较熟悉shell来了。C和perl都不熟了。
fjzcaufjzcau系统运维工程师金融
2014-06-07 23:08
日期函数主要还是曾经在一个项目组用的perl学来的。2年没用perl都忘的差不多了。
Ctrl+Enter 发表

作者其他文章

相关文章

相关问题

相关资料

X社区推广