动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   数据库 → 阅读文章

 将数据导出成为文本格式的备份的shell脚本

作者来源: 
阅读 数 401 人次 , 2006-5-11 11:20:00 


#将数据库中表的内容导出成为一个文本格式的shell脚本
#有两种使用方法(假设这个脚本的名字叫做unload):
# 1.将一个用户中所有的数据库表的内容到出来:
             unload userid/passwd[@connection]
# 2.只导出一个表的内容:
#            unload userid/passwd[@connection]  table_name
#这里要感谢you的帖子,是他让我学会了如何设置sqlplus环境,从而
#将数据库数据分解出来。
#
#我还想写出一个根据数据库中的数据字典的内容自动生成ctl文件的脚本,
#以便于将文本的数据库内容使用sqlldr导入到数据库中
#请各位提示我可能要涉及的数据字典是哪些 :)
#             

sep=','   # --分隔符,可以修改成自己想要的分隔符,如'|'
load_table( ){
rm -f table1.txt
echo " set colsep $sep;
set echo off;
set feedback off;
set heading off;
set pagesize 0;
set linesize 1000;
set numwidth 12;
set termout off;
set trimout on;
set trimspool on;
spool table1.txt;
select table_name from user_tables;
spool off;
"  | sqlplus $userid   >/dev/null
if [ "$?" -ne 0 ] ; then
echo  sqlplus $userid error in get table name <"$?">!!
echo please check userid and passwd or database.
exit 
fi

if [[ -f table1.txt ]]
then
cat table1.txt | grep -v "^SQL>" | tr -d ' ' >table.txt
rm -f table1.txt
tables=`cat table.txt`
rm table.txt
else
echo "get table name error"
exit
fi
}

if [ "X$1" = "X" ]; then
echo "Usage: $0 <userid/passwd@connection> <table_name>"
exit
echo \c "Userid:"
read userid1
echo \c "Passwd:"
echo off
read passwd
userid=$userid1$passwd
echo on
else
userid=$1
fi

if [ "X$2" = "X" ]; then 
load_table;
if [[ "X$tables" = "X" ]];then
echo "no table in user $userid"
exit
fi
else 
tables=$2
fi

for table in $tables
do
rm -f wk_$table.txt
echo " set colsep $sep;
set echo off;
set feedback off;
set heading off;
set pagesize 0;
set linesize 1000;
set numwidth 12;
set termout off;
set trimout on;
set trimspool on;
spool wk_$table.txt; 
select * from $table; 
spool off; 
"  | sqlplus $userid  >/dev/null
if [ "$?" -ne 0 ] ; then
echo  error:sqlplus $userid error in unload table $table!!
echo please check userid and passwd or database.
exit 
fi

if [[ -f wk_$table.txt ]]
then
cat wk_$table.txt | grep -v "^SQL>" >$table.txt
sed -e "s/ *$//g" $table.txt >wk_$table.txt
mv  wk_$table.txt $table.txt
if [[ `grep "ORA-" $table.txt`  = "" ]]; then
echo "unload table $table....\t\t\t\t\t\t ok"
else 
cat $table.txt
err="$err $table"
fi

else 
echo $0 error
fi
done
if [[  "X$err" = "X" ]];then
echo unload complete!
else
echo "unload table $err error, please check it!"
fi
  
 本文Tags备份  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:ORACLE问题,每天10问(十一)
· 下一篇:Oracle9iR2在ReahatAS3上的升级导致exp不正常的问题
· 1069错误(由于登录失败而无法启动服务)解决方法
· 基于WEB的数据库查询
· MySQL 从表中取出随机数据
· MySQL的几点用法
· MySQL数据库技术(16)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077