資料匯出shell指令碼(上)

DBA_建瑾發表於2014-07-05

    DBA經常需要使用資料泵作一些資料的匯入匯出操作,為此本人編寫了基於資料庫使用者匯入匯出操作的shell指令碼。
丟到伺服器上,直接執行指令碼,方便日常操作。同時開發人員也可以按照提示完成資料匯入匯出操作。(此指令碼應用於IBM? AIX作業系統,所以使用Korn Shell指令碼語法編寫,指令碼內容在此文章附錄)

本章介紹匯出資料指令碼


1. 
上傳指令碼檔案expUser_test.ksh到源資料庫伺服器oracle使用者目錄下

[oracle@ENMOEDU ~]$ ls –trl

-rwxr-x--- 1 oracle oinstall 2760 Jul  1 23:45 expUser_test.ksh

 

2.修改expUser_test.ksh檔案所屬使用者並授予執行許可權

[oracle@ENMOEDU ~]$ su

Password:

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

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

[root@ENMOEDU oracle]# su - oracle

[oracle@ENMOEDU ~]$ ll expUser_test.ksh

-rwxrwx--- 1 oracle oinstall 2760 Jul  1 23:45 expUser_test.ksh

 

3.執行匯出指令碼

[oracle@ENMOEDU ~]$ ksh

$ ./expUser_test.ksh       ---執行指令碼

 

USERNAME                      DEFAULT_TABLESPACE           

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

ESO                             ESO_TS1                               

T1                              TEST1                                          

TEST                            TEST_SPACE                   

SCOTT                           USERS              

---以上資訊顯示資料庫所有使用者與所屬表空間

Please specify the username to export

 

Enter value for username: ESO      ----輸入需要匯出的使用者名稱     

Username:ESO

estimate export messages...

 

Export: Release 11.2.0.3.0 - Production on Wed Jul 2 11:43:56 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" ESTIMATE_ONLY=yes schemas=ESO

Estimate in progress using BLOCKS method...

Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA

.  estimated "ESO"."PUB_ONLINE_HIST"                 7 MB

Total estimation using BLOCKS method: 7 MB

Job "SYS"."SYS_EXPORT_SCHEMA_01" successfully completed at 11:44:02

 

DIRECTORY_NAME                          DIRECTORY_PATH                                                                           

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

SHARE_DIR                               /bidpro                                                                                   

MY_DIR                                  /home/oracle/dmp                                                                                                            

---以上顯示資料庫所有目錄資訊 

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

 

Enter value for directory_name[DATA_PUMP_DIR]: MY_DIR ---輸入匯出檔案目錄

Directory name :MY_DIR  /home/oracle/dmp

 

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

 

Enter value for file_name[ESO20140702]:    ---回車生成預設檔名

File name: ESO20140702.dmp

 

starting export data...

 

Export: Release 11.2.0.3.0 - Production on Wed Jul 2 11:47:25 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=ESO20140702.dmp logfile=ESO20140702.log DIRECTORY=MY_DIR schemas=ESO 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 "ESO"."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/ESO20140702.dmp     ---匯出完成顯示dmp檔案目錄

Job "SYS"."SYS_EXPORT_SCHEMA_01" successfully completed at 11:47:57

 

 注:指令碼為按資料庫使用者匯出與匯入。目標資料庫使用者名稱與對應表空間名,必須與源資料庫使用者名稱與對應表空間名相同。

附錄:

expUser_test.ksh
#This shell for export 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 <
set linesize 300  heading on feedback off pagesize 100
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
#estimate total size of exp dump file
echo "estimate export messages..."
expdp \'/ as sysdba\'   ESTIMATE_ONLY=yes  schemas=$username
#find directory path
result=`sqlplus -S / as sysdba <
set linesize 300  heading on feedback off pagesize 100
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" -eq ""] && echo "you do not input the directory name for export data file. " && exit 0 
#show directory
directory=${directory:-"DATA_PUMP_DIR"}
#find directory path by directory name from sqlplus
dpath=`sqlplus -S / as sysdba <
set linesize 300  heading off feedback off pagesize 0
col directory_path for a90
col directory_name for a40
select directory_path from dba_directories where directory_name='$directory';
exit
EOF`
echo "Directory name :"$directory"  "$dpath
#input filename
echo -ne "\nPlease specify the prefix of the dump file (.dmp) to export\n\n"
read filename?"Enter value for file_name[$username$DATE]: "
filename=${filename:-"$username$DATE"}
#show filename
echo "File name:" $filename.dmp
#start run expdp 
echo -ne  "\nstarting export data...\n"
expdp \'/ as sysdba\' dumpfile=$filename.dmp logfile=$filename.log  DIRECTORY=$directory  schemas=$username COMPRESSION=all


        DBA_建瑾
        2014.7.5

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29324876/viewspace-1209889/,如需轉載,請註明出處,否則將追究法律責任。

相關文章