关闭

[原创]DB2 并行Export 脚本 --基于分区数据库

314人阅读 评论(0) 收藏 举报
原理是所有的节点输出文件指向同一个目录---使用共享存储也不错
 
#!/bin/sh
USAGE ()
{
  echo "Usage: `basename $0` [-d database] {-t tablename} [-p path] [-s filetype] [-o output] [-l logfile]";
  echo "Db2export Parameter Infomation:";
  echo "  Parameter -t must be specified                             [必须指定表名]";
  echo "  Parameter -d: Database Name,Default Value:sdncbi           [数据库名]";
  echo "  Parameter -p: OutPath Name,Default Value:Current Directory [输出目录]";
  echo "  Parameter -s: File Type,{IXF | DEL | WSF}                  [文件类型]";
  echo "  Parameter -o: File Name,Default Value:Table Name           [输出文件]";
  echo "  Parameter -f: Logfile Name,Default Value: null             [日志文件]";
  echo ""
  exit 1;
}
if [ $# -lt 2 ]
  then
   USAGE
  fi
dbname=""
tablename=""
pathdir=""
filetype=""
outfile=""
logfile=""
tmpfile=$(basename $0).tmp
#***************************Judge Parameter Begin
  while getopts :d:t:p:s:o:l: OPTION ;
  do
   case "$OPTION" in
     d) dbname="$OPTARG" ;;
     t) tablename="$OPTARG" ;;
     p) pathdir="$OPTARG" ;;
     s) filetype="$OPTARG" ;;
     o) utfile="$OPTARG" ;;
     l) logifle="$OPTARG" ;;
     \?) #usage statement
       USAGE;
       ;;
   esac
  done

  if [ "$tablename" = "" ]
   then
     echo "Parameter -t must be specified "
     USAGE;
   else
   if [ `expr index $tablename .` -le 0  ]
     then
       echo "The Table Name is the same as tabschema.tabname."
       echo "Eg: comm.dim_latn "
       exit 1
   fi
  fi

  if [ "$dbname" = "" ]
   then
     dbname="sdncbi"
  fi
  if [ "$pathdir" = "" ]
   then
     pathdir=`pwd`
  fi
  if [ "$filetype" = "" ]
   then
     filetype="del"
  fi
  if [ "$outfile" = "" ]
   then
    utfile=$tablename
  fi
#***************************Judge Parameter End
date
TABSCHEMA=`echo $tablename|cut -d. -f1`
TABNAME=`echo $tablename|cut -d. -f2`
# get partitioning key
db2look -e -d sdncbi -z $TABSCHEMA -t $TABNAME > $tmpfile 2>&1
flag=`cat $tmpfile|grep "CREATE TABLE"|wc -l`
if [ "$flag" -lt 1 ]
then
echo "Tabname is not exists,Please Imput a Correct Tabname!!"
exit
fi
key=`cat $tmpfile | grep "PARTITIONING KEY" | awk -F\" '{print $2}' | sed -e "s/\"//g" `
if [ ! -n "$key" ]; then
key=`cat $tmpfile | grep "HASH" | awk -F\" '{print $2}' | sed -e "s/\"//g" `
fi
#echo $key
#Export data to file
echo "Begin Export [${tablename}] to file" |tee -a $logfile
if [ -n "$key" ]; then
db2_all "\"||db2 connect to ${dbname};db2 'export to ${pathdir}/${outfile}.${filetype}.## of ${filetype} select * from
${tablename} where dbpartitionnum(${key}) = ## with ur';" >$tmpfile
cat $tmpfile|grep "Number of rows exported:" |tee -a $logfile
cat $tmpfile|grep "Number of rows exported:"|awk -F":" 'BEGIN{sum=0}{sum=sum+$3}END{printf("Total Export Rows:%d\n",sum)}' |tee -a $logfile
for file in ${fullname}.del.*;
   do
    ldfile=`echo $file|cut -d. -f4`
    newfile=`echo $oldfile|awk '{printf("%03d\n",$1);}'`
    #echo $oldfile -- $newfile
  
    if [ "$oldfile" != "$newfile" ]
     then
       mv $file ${fullname}.del.$newfile
    fi
   done
 
else
db2 connect to ${dbname};db2 "export to ${pathdir}/${outfile}.${filetype} of ${filetype} select * from ${tablename} with ur" >$tmpfile
cat $tmpfile|grep "Number of rows exported:" |tee -a $logfile
fi
if [ `cat $tmpfile|grep "Number of rows exported:"|wc -l` -gt 0 ]
  then
    echo "Export  [${tablename}] Success!!!!" |tee -a $logfile
    exit 1
  else
    echo "!!!Export  [${tablename}] Failed!!!!" |tee -a $logfile
    exit -1
fi
date
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5865次
    • 积分:142
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档