oracle shell导入数据,数据导入shell脚本(下)

DBA经常需要使用数据泵作一些数据的导入导出操作,为此本人编写了基于数据库用户导入导出操作的shell脚本。

丢到服务器上,直接执行脚本,方便日常操作。同时开发人员也可以按照提示完成数据导入导出操作。(此脚本应用于IBM? AIX操作系统,所以使用Korn Shell脚本语法编写,脚本内容在此文章附录)

本章介绍导入数据脚本

1.上传脚本文件impUser_test.ksh到目标数据库服务器oracle用户目录下

[oracle@ENMOEDU ~]$ ls –trl

-rwxrwx--- 1 oracle oinstall 3200 Jul  2 00:26 impUser_test.ksh

2.修改脚本所属用户并增加执行权限

[oracle@ENMOEDU ~]$ su

Password:

[root@ENMOEDU oracle]# chown oracle:oinstall impUser_test.ksh

[root@ENMOEDU oracle]# chmod 770 impUser_test.ksh

[root@ENMOEDU oracle]# su - oracle

3.执行导入脚本

[oracle@ENMOEDU ~]$ ksh

$ ./impUser_test.ksh       ---执行脚本

USERNAME                      DEFAULT_TABLESPACE

------------------------------------------------------------

ESO27                         ESO_TS1

TEST                          TEST_SPACE

SCOTT                         USERS

---以上信息显示数据库所有用户与所属表空间

Please specify the username to export

Enter value for username: ESO27          ---输入导入用户名

Username:ESO27

DIRECTORY_NAME                          DIRECTORY_PATH

-------------------------------------------------------------------------------

SHARE_DIR                               /bidpro

MY_DIR                                  /home/oracle/dmp

DATA_PUMP_DIR                           /u01/app/oracle/admin/enmo/dpdump/

---以上显示数据库所有目录信息

Choose a Directory Name from the list above (case-sensitive).

Enter value for directory_name[DATA_PUMP_DIR]: MY_DIR ---输入dmp文件目录

MY_DIR:/home/oracle/dmp        ---注:一定要把dmp文件放在此目录

Please specify the prefix of the dump file (.dmp) to import

Enter value for filename[ESO20140702]:        ---输入dmp文件名

File name:ESO20140702.dmp

starting backup old data...

Export: Release 11.2.0.3.0 - Production on Wed Jul 2 14:14:31 2014

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Starting "SYS"."SYS_EXPORT_SCHEMA_01":  "/******** AS SYSDBA" dumpfile=backup_ESO20140702.dmp logfile=backup_ESO20140702.log DIRECTORY=MY_DIR schemas=ESO27 compression=all

Estimate in progress using BLOCKS method...

Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA

Total estimation using BLOCKS method: 7 MB

Processing object type SCHEMA_EXPORT/USER

Processing object type SCHEMA_EXPORT/SYSTEM_GRANT

Processing object type SCHEMA_EXPORT/ROLE_GRANT

Processing object type SCHEMA_EXPORT/DEFAULT_ROLE

Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA

Processing object type SCHEMA_EXPORT/TABLE/TABLE

Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX

Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT

Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS

Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS

. . exported "ESO27"."PUB_ONLINE_HIST"             1.277 MB   31860 rows

Master table "SYS"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded

******************************************************************************

Dump file set for SYS.SYS_EXPORT_SCHEMA_01 is:

/home/oracle/dmp/backup_ESO2720140702.dmp  ---在导入之前备份原有数据

Job "SYS"."SYS_EXPORT_SCHEMA_01" successfully completed at 14:15:07

Continue the import[y or n]: y     ---是否执行导入操作

starting import new data...

Import: Release 11.2.0.3.0 - Production on Wed Jul 2 14:17:07 2014

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production

With the Partitioning, OLAP, Data Mining and Real Application Testing options

Master table "SYS"."SYS_IMPORT_FULL_02" successfully loaded/unloaded

Starting "SYS"."SYS_IMPORT_FULL_02":  "/******** AS SYSDBA" dumpfile=ESO20140702.dmp logfile=ESO20140702.log DIRECTORY=MY_DIR table_exists_action=replace remap_schema=ESO7:ESO27

Processing object type SCHEMA_EXPORT/USER

ORA-31684: Object type USER:"ESO27" already exists

Processing object type SCHEMA_EXPORT/SYSTEM_GRANT

Processing object type SCHEMA_EXPORT/ROLE_GRANT

Processing object type SCHEMA_EXPORT/DEFAULT_ROLE

Processing object type SCHEMA_EXPORT/PRE_SCHEMA/PROCACT_SCHEMA

Processing object type SCHEMA_EXPORT/TABLE/TABLE

Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA

. . imported "ESO27"."PUB_ONLINE_HIST"             1.277 MB   31860 rows

Processing object type SCHEMA_EXPORT/TABLE/INDEX/INDEX

Processing object type SCHEMA_EXPORT/TABLE/CONSTRAINT/CONSTRAINT

Processing object type SCHEMA_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS

Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS

Job "SYS"."SYS_IMPORT_FULL_02" completed with 1 error(s) at 14:17:16

end for import data.       ---导入完成

注:脚本为按数据库用户导出与导入。目标数据库用户名与对应表空间名,必须与源数据库用户名与对应表空间名相同。

附录:

impUser_test.ksh#This shell for import database user

#@Jianjin

#export ORACLE_SID=enmo

#export ORACLE_BASE=/u01/app/oracle

#export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1

#export NLS_LANG=American_America.ZHS16CGB231280

#export PATH=$ORACLE_HOME/bin:$PATH

#date format

DATE=`date +"%Y%m%d"`

#select all user

users=`sqlplus -S / as sysdba

col username for a30

col default_tablespace for a30

select username,default_tablespace from dba_users;

exit

EOF`

#show user list form sqlplus

echo $users|awk  '{size=split($0,a," ")

for(i=1;i<=size-1;i=i+2){

printf("%-30s", a[i])

printf("%-30s\n", a[i+1])

if (i==(size-1))

printf("\n")

}}'

#input user

echo -ne "\nPlease specify the username to export\n\n"

read username?"Enter value for username: "

echo  "Username:"$username

if [[ $username = "" ]];then

echo "database username is null"

exit

fi

#find directory path

result=`sqlplus -S / as sysdba

col directory_path for a90

col directory_name for a40

select directory_name,directory_path from dba_directories;

exit

EOF`

#show directory list from sqlplus

echo $result|awk  '{size=split($0,a," ")

for(i=1;i<=size-1;i=i+2){

printf("%-40s", a[i])

printf("%-90s\n", a[i+1])

if (i==(size-1))

printf("\n")

}}'

#input directory_name

echo -ne "Choose a Directory Name from the list above (case-sensitive).\n\n"

read directory?"Enter value for directory_name[DATA_PUMP_DIR]: "

directory=${directory:-"DATA_PUMP_DIR"}

#find directory path by directory name from sqlplus

dpath=`sqlplus -S / as sysdba

col directory_path for a90

col directory_name for a40

select directory_path from dba_directories where directory_name='$directory';

exit

EOF`

#show directory

#$path=${path/%s/\/\//\//g}

#$path=$dpath/

echo $directory:$dpath

#input filename

echo -ne "\nPlease specify the prefix of the dump file (.dmp) to import\n\n"

read filename?"Enter value for filename[$username$DATE]: "

filename=${filename:-"$username$DATE"}

#show filename

echo "File name:"$filename.dmp

#test file exist

if test -e $dpath/$filename.dmp

#if import file is exist

then

#backup old data

echo -ne "\nstarting backup old data...\n"

expdp \'/ as sysdba\' dumpfile=backup_$username$DATE.dmp logfile=backup_$username$DATE.log  DIRECTORY=$directory schemas=$username compression=all

#

# echo "Press enter to continue the import:"

#while read answer;do

#  if [ -z "$answer" ];then

#      echo 'you hit enter'

#      break

#  fi

#done

read key?" Continue the import[y or n]: "

if [[ $key = "n" ]];then

echo "You interrupted the import operation"

exit

fi

#import new data

echo -ne "\nstarting import new data...\n"

impdp \'/ as sysdba\' dumpfile=$filename.dmp logfile=$filename.log  DIRECTORY=$directory table_exists_action=replace remap_schema=$username:$username

echo "end for import data."

#if import file is not exist

else

echo "$dpath/$filename.dmp file is not exist!"

fi

DBA_建瑾

2014.7.12

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值