点击(此处)折叠或打开
- #!/usr/bin/env sh
- #
- # File: hpl.sh
- # Creation Date: 2013/6/14 11:02:28
- # Last Modified: 2013/6/14 11:02:29
- # Purpose:脚本使用informix HPL导出导入数据,可灵活导出导入一张或多张表
- #
- #-----------------------------------------------------------------------------------
- usage()
- {
- echo "=================使用说明=================== "
- echo "Usage: "
- echo " `basename $0` [-d] -t [-p u] "
- echo " `basename $0` [-f] [-d] -t -p l "
- echo " "
- echo "Options: "
- echo " -d destdbname 目标表空间名称,默认为tnmsdb2 "
- echo " -f fromdbname 源表空间名称,默认为tnmsdb2 "
- echo " -t tables 表名称 "
- echo " -p parameters 导出导入参数 u 为导出,l 为导入,默认为导出l "
- echo " "
- echo "例子: "
- echo "导出一张表: "
- echo " `basename $0` -d tnmsdb2_hn -t ems_event_history -p u "
- echo "导出多张表: "
- echo " `basename $0` -d tnmsdb2_hn -t ems_event_history,ems_event -p u "
- echo "导入一张表: "
- echo " `basename $0` -f tnmsdb2_hn -d tnmsdb2 -t ems_event_history -p l "
- echo "导入多张表: "
- echo " `basename $0` -f tnmsdb2_hn -d tnmsdb2 -t ems_event_history,ems_event-p l "
- echo "使用默认参数导出: "
- echo " `basename $0` -t "ems_event_history ems_event" "
- echo "使用默认参数导入: "
- echo " `basename $0` -t "ems_event_history ems_event" -p l "
- exit 0
- }
- #程序选项
- while getopts "d:f:t:p:" OPTION
- do
- case $OPTION in
- d) destdbname="${OPTARG}"
- ;;
- f) fromdbname="${OPTARG}"
- ;;
- t) tables="${OPTARG}"
- ;;
- p) parameters="${OPTARG}"
- ;;
- *|?) usage
- ;;
- esac
- done
- #检查脚本后面是否有跟参数
- if [ $# = 0 ];then
- echo "error:`basename $0` parameter is error"
- usage
- exit 0
- fi
- #初始化参数
- parameter()
- {
- #获取表名称
- if [ -z "$tables" ];then
- echo error:table name is not exist.
- echo "例如:`basename $0` -t "ems_event_history ems_event""
- exit 0
- else
- names=$tables
- fi
- #目标表空间名称默认为tnmsdb2
- if [ -z $destdbname ];then
- destdbname=tnmsdb2
- fi
- #源表空间名称默认为tnmsdb2
- if [ -z $fromdbname ];then
- fromdbname=tnmsdb2
- fi
- #参数默认为导出u
- if [ -z $parameters ];then
- parameters=u
- fi
- }
- #数据导出函数
- unload()
- {
- onpladm create project outport_data
- onpladm create job $tabname -p outport_data -d $destdbname/record/$tabname -D$destdbname -t $tabname -fu -zD
- onpladm run project outport_data -fu
- #onpladm delete job $tabname -fu
- onpladm delete project outport_data
- #onpladm list job
- }
- #数据导入函数
- load()
- {
- onpladm create project import_data
- onpladm create job $tabname -p import_data -d $fromdbname/record/$tabname -D$destdbname -t $tabname -flc -zD
- onpladm run project import_data -fl
- #onpladm delete job $tabname -fl
- onpladm delete project import_data
- }
- #导出表结构
- dbschema()
- {
- dbschema -d $destdbname -t $tabname -ss $destdbname/schema/$destdbname.sql
- }
- #开始执行程序
- parameter
- if [ $parameters = u ];then
- if [ ! -d $destdbname ];then
- mkdir $destdbname
- mkdir $destdbname/record
- mkdir $destdbname/schema
- else
- echo "$destdbname目录已存在,请先删除此目录再执行."
- exit 0
- fi
- fi
- #删除onpload数据库(project和job信息都存放在onpload数据库中)
- echo 'drop database onpload;'|dbaccess sysadmin
- if [ $? -eq 0 ];then
- echo "onpload库删除成功"
- else
- echo "onpload库不存在,可以忽略此报错."
- fi
- #执行导出导入程序
- for tabname in $names
- do
- #导出数据
- if [ $parameters = u ];then
- unload
- fi
- #导入数据
- if [ $parameters = l ];then
- load
- fi
- done