测试了这个脚本(收集自网络),很不错,做异构数据库迁移或oracle大版本之间迁移非常有用。

在config.cfg里面配好相应的用户和表名就可得到相应的select语句和该表的控制文件,运行generate.sh可以得到该表的数据文件,运行sqlldr可以把生成的数据文件插入到目标库表中。目前功能有限,一次只能生成一个用户下的表。

config--配置文件
script --储存数据导出sql
unl --数据文件
ctl --控制文件
log --日志文件
bad --sqlldr导入错误数据
temp --临时文件

./temp.sh--生成源系统用户建表字段sql

#!/bin/sh
DB_USER=test
DB_PWD=test
DB_TNS=orcl2

sqlplus -s $DB_USER/$DB_PWD@$DB_TNS<<EOF
set echo off
set feedback off
set pagesize 0
set heading off
set linesize 120
set trimspool on
spool temp.txt
select table_name from user_tables;
spool off
EOF

./config/config.cfg:--源系统用户配置文件

#################################
# This config file is prepare for the script run.sh
#
#################################

# connect database username and password and the tnsname
# this parameter can help you to login the database
username=test
passwd=test
tnsname=orcl2

# the owner of target table which is the table's owner
owner=dbo

# the table that you want to get its record to generate data file
# you can add the table name and separate them with ','

#awk '{a[NR]=$1}END{for (i=1;i<=NR;i++) print a[i]}' $file

./config/sqlldr.cfg:--目标系统用户配置文件

#######################################
# This config file is prepare for the script sqlldr.sh
#######################################

# the target user and password and tnsname
# you can sql load the data file into the username/passwd@tnsname
username=dbo
passwd=dbo
tnsname=tcell

./run.sh:--从源系统表中生成控制文件及sql导出脚本

#!/bin/sh
#run.sh

############################################################
## parameter initialization
############################################################
username=""
passwd=""
tns=""
owner=""
table=""

############################################################
## read the config file
## config file directory is ./config/config.cfg
############################################################
eval $(awk -F"=" '{
if ($1~/username/) print "username="$2;
if ($1~/passwd/) print "passwd="$2;
if ($1~/tnsname/) print "tns="$2;
if ($1~/owner/) print "owner="$2;
#if ($1~/tables/) print "tables="$2
}' ./config/config.cfg)

############################################################
## generate the script file get_unl file
## to get the table's unl file
############################################################
rm -rf temp
mkdir temp

rm -rf script
mkdir script

rm -rf ctl
mkdir ctl

rm -rf unl
mkdir unl

file=./temp.txt
tables=($(cut -f1 $file))

echo "set heading off
set head off
set pagesize 0
set linesize 3000
set feedback off
set termout off
set trims on" > script/get_unl.sql

for ((i=0;i<${#tables[@]};i++))
do
echo "spool ./temp/${tables[i]}.tmp
select table_name,column_name,data_type from all_tab_cols where owner=upper('$username') and table_name=upper('${tables[i]}') orde
r by column_id;
spool off" >> script/get_unl.sql
done

echo "exit" >> script/get_unl.sql

#echo $tables | awk -F"," '{
# print "set heading off"
# print "set head off"
# print "set pagesize 0"
# print "set linesize 3000"
# print "set feedback off"
# print "set termout off"
# print "set trims on"
# for(i=1;i<=NF;i++) {
# print "spool ./temp/"$i".tmp"
# print "select table_name,column_name,data_type from all_tab_cols where #owner=upper('\'''$owner''\'') a
nd table_name=upper('\''"$i"'\'') order by column_id;"
# print "spool off"
# }
# print "exit"
# }' > script/get_unl.sql

echo "the script of get unl had been generated :"
ls script | grep "get_unl"
echo ""

############################################################
## run the sql script
## generate the temp file of the table and column information
############################################################
sqlplus -S $username/$passwd@$tns @script/get_unl.sql
echo "the tempfile of table and column information had been generate :"
ls temp
echo ""

############################################################
## generate the sql script which is select from the table
############################################################
for loop in $(ls temp)
do
#table_name=`echo $loop | awk -F"." '{print $1}'`
table_name=$(basename $loop .tmp)
new_file_select="script/"$owner"."$table_name".sql"
new_file_ctl="ctl/"$owner"."$table_name".ctl"
rm -f new_file_select
awk 'BEGIN{
print "set heading off"
print "set head off"
print "set pagesize 0"
print "set linesize 3000"
print "set feedback off"
print "set termout off"
print "set trims on"
print "spool unl/'$owner'.'$table_name'.unl"
print "select "
}{
if (NR==1){
if($3~/DATE/) print "to_char("$2",'\''yyyymmddHH24miss'\'')"
else print $2
}
else {
if($3~/DATE/) print "||'\''|'\''||to_char("$2",'\''yyyymmddHH24miss'\'')"
else print "||'\''|'\''||"$2
}
}
END{
print "||'\''|'\''"
print "from '$username'."$1";"
print "spool off"
print "exit"
}' temp/"$loop" > $new_file_select
awk 'BEGIN{
print "load data"
print "badfile '\'bad/$table_name.bad\''"
print "truncate into table '$table_name'"
print "fields terminated by '\''|'\''"
print "trailing nullcols"
print "("
}{
if (NR==1){
if($3~/DATE/) print ","$2" date '\''yyyymmddHH24miss'\''"
else print $2
}
else {
if($3~/DATE/) print ","$2" date '\''yyyymmddHH24miss'\''"
else print ","$2
}
}
END{
print ")"
}' temp/"$loop" > $new_file_ctl
done

echo "the select script had been generated :"
ls script | grep ^[^get_unl]
echo ""
echo "the sqlldr control file had been generated :"
ls ctl | grep [sql$]

# delete the temporary directory temp
#rm -rf temp

./generate.sh--生成数据文件

#!/bin/sh
#generate_unl.sh

#################################
## this shell script is in accordance with the script file in the directory script
## to generate the data file
################################

for loop in `ls script | grep ^[^get_unl]`
do
echo "the $loop is generating...please wait..."
sqlplus -s test/test@orcl2 @script/$loop
done

./sqlldr.sh--将数据sqlldr到目标系统

#!/bin/sh
#sqlldr.sh

## read the config file
eval $(awk -F"=" '{
if ($1~/username/) print "username="$2;
if ($1~/passwd/) print "passwd="$2;
if ($1~/tnsname/) print "tns="$2;
}' config/sqlldr.cfg)

## check the log directory
if [ -d log ]
then
rm -rf log/*
else
mkdir log
echo "directory log is created"
fi

## check the bad file directory
if [ -d bad ]
then
rm -rf bad/*
else
mkdir bad
fi

## check the bad file directory

echo "-------begin load data---------"
for loop in `ls script`
do
if (echo $loop | grep ^[^get_unl]>/dev/null 2>&1)
then
#table_name=`echo $loop | awk -F"." '{print $1"."$2}'`
table_name=$(basename $loop .sql)
sqlldr $username/$passwd@$tns control=ctl/$table_name.ctl data=unl/$table_name.unl log=log/$table_name.log bad=bad/$table_name.bad
rows=50000 errors=1000 direct=true silent='(header,feedback)'
fi
done

for loop in `ls log`
do
echo "the sqlldr log file << $loop >> contain : "
grep -E 'not load|Total|success' log/$loop | grep -v 'thread'
echo ""
done

echo "------end load----------------"

数据导入日志可在log及bad目录下查看。

转载于:https://blog.51cto.com/mibon/912739

自动sqlldr脚本相关推荐

  1. Puppet客户端自动安装脚本

    运维自动化发展已经是势不可挡,传统的靠大量人力运维的方式渐不能满足企业IT发展的需求,其中Puppet自动化更是受到IT人士的青睐,那今天我们一起来研究一下puppet客户端的自动安装脚本.脚本不足之 ...

  2. Linux从入门到精通——自动安装脚本

    ###kickstart 自动安装脚本的制作### KickStart是什么,有什么作用?    KickStart是一种无人职守安装方式.KickStart的工作原理是通过记录典型的安装过程中所需人 ...

  3. 光遇自动弹琴脚本代码_光遇弹琴辅助软件下载-光遇自动弹琴脚本代码下载v1.0_86PS软件园...

    光遇弹琴辅助是一款可以帮助手残党的光遇玩家脱离苦海的神仙辅助软件.不仅操作简单,一键运行,而且特别省心,只需要点击运行然后不用再旁边看着,直接挂机就可以完成基本的任务.当别人都在练习乐谱的时候,你就可 ...

  4. Mysql自动备份脚本

    1.Mysql自动备份脚本 #!/bin/sh # 数据库基本信息 #数据库名称 DB_NAME="test" #用户名 DB_USER="root" #密码 ...

  5. Linux oracle数据库自动备份自动压缩脚本代码

    Linux oracle数据库自动备份自动压缩脚本代码 Linux oracle数据库备份完成后可以自动压缩脚本代码. 复制代码代码如下: #!/bin/bash #backup.sh #edit: ...

  6. Linux 多应用程序docker自动部署脚本

    2019独角兽企业重金招聘Python工程师标准>>> Linux 多应用程序docker自动部署脚本可以结合jenkins分布式部署 参数: 镜像名:端口的格式:版本号 例如:sp ...

  7. 完美解决Flask-Migrate使用SQLite生成自动迁移脚本的Bug

    一.问题描述 flask-migrate插件是对Alembic的简单封装,当程序使用SQLite数据库作为backend的时候,使用 flask migrate命令生成自动迁移脚本,使用flask u ...

  8. SQL Server镜像自动生成脚本

    SQL Server镜像自动生成脚本 镜像的搭建非常繁琐,花了一点时间写了这个脚本,方便大家搭建镜像 执行完这个镜像脚本之后,最好在每台机器都绑定一下hosts文件,不然的话,镜像可能会不work 1 ...

  9. oracle自动imp脚本

    摘自:http://www.pcppc.cn/shujuku/ORACLE/shujuku_163351.html 您正在看的ORACLE教程是:oracle自动imp脚本. 先删除用户,在重建用户, ...

最新文章

  1. App.config的典型应用
  2. 度量.net framework 迁移到.net core的工作量(转)
  3. 性价比超高的云服务器
  4. 关于JQuery简单介绍
  5. usb转pci_IT-GO PCI-E转USB转接卡台式机pcie转2口usb3.0扩展卡后置集线卡
  6. urllib2设置代理
  7. 《黑客秘笈——渗透测试实用指南》—第2章2.4节Web应用程序的扫描
  8. Android -- onWindowFocusChanged
  9. python import 问题
  10. Spring安全:防止暴力攻击
  11. zemax评价函数编辑器_ZEMAX软件使用入门
  12. StringStringBuilder的使用
  13. 2013计算机系统导论,计算机系统导论2013期末(20页)-原创力文档
  14. 美团大咖:程序员35岁前应做好的技术积累
  15. Python的if判断和两重判断
  16. w7系统怎么ping服务器,win7系统中如何ping端口命令
  17. DelayQueue用例
  18. USB数据采集卡,Labjack U6、T7 采集卡 VB、C++ 编程范例
  19. 电脑右下角图标不显示
  20. 【PS/AI】10款逼真的喷泉背景免费矢量设计素材

热门文章

  1. Linux学习-Xshell断开连接程序依然运行
  2. BZOJ-1192-[HNOI2006]鬼谷子的钱袋
  3. Android系统中提供的原子操作
  4. 不知道为什么,很多优秀的产品知道人确很少
  5. 触手可得的云原生 | 阿里云中间件发布多项新功能
  6. Niagara 泵阀
  7. IntelliJ IDEA 2018.1新特性
  8. 10 python中的常量
  9. 《Spring 5 官方文档》18. Web MVC 框架(五)
  10. 1.7nginx用户认证