免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5112 | 回复: 15
打印 上一主题 下一主题

将数据导出成为文本格式的备份的shell脚本(感谢you) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-10 16:55 |只看该作者 |倒序浏览
#将数据库中表的内容导出成为一个文本格式的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

论坛徽章:
0
2 [报告]
发表于 2003-06-10 17:19 |只看该作者

将数据导出成为文本格式的备份的shell脚本(感谢you)

好像不能导出所有表哦,导出单个表没有问题

论坛徽章:
0
3 [报告]
发表于 2003-06-10 17:33 |只看该作者

将数据导出成为文本格式的备份的shell脚本(感谢you)

shell脚本中有一个 exit 99是调试用的,忘了删了,呵呵
    现在可以了 :)
    谢谢你的提醒 :)

论坛徽章:
0
4 [报告]
发表于 2003-06-11 09:35 |只看该作者

将数据导出成为文本格式的备份的shell脚本(感谢you)

不错,谢谢你的工作。

论坛徽章:
0
5 [报告]
发表于 2003-06-23 16:09 |只看该作者

将数据导出成为文本格式的备份的shell脚本(感谢you)

好,继续.

论坛徽章:
0
6 [报告]
发表于 2003-07-26 18:56 |只看该作者

将数据导出成为文本格式的备份的shell脚本(感谢you)

SQLLOAD就可以呀,不必如此复杂

论坛徽章:
0
7 [报告]
发表于 2003-07-27 12:32 |只看该作者

将数据导出成为文本格式的备份的shell脚本(感谢you)

这和sqlload是两回事。只是好像没有必要这么做。

论坛徽章:
0
8 [报告]
发表于 2003-07-28 08:50 |只看该作者

将数据导出成为文本格式的备份的shell脚本(感谢you)

偶明白了 ! 谢谢了!!

论坛徽章:
0
9 [报告]
发表于 2003-07-28 09:36 |只看该作者

将数据导出成为文本格式的备份的shell脚本(感谢you)

真是感谢
很不错

论坛徽章:
0
10 [报告]
发表于 2003-07-28 09:51 |只看该作者

将数据导出成为文本格式的备份的shell脚本(感谢you)

支持原创
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP