資料匯入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. ---匯入完成
注:指令碼為按資料庫使用者匯出與匯入。目標資料庫使用者名稱與對應表空間名,必須與源資料庫使用者名稱與對應表空間名相同。
附錄:
#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 <
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
#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=${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`
#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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29324876/viewspace-1217550/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 資料匯出shell指令碼(上)指令碼
- Shell指令碼匯入外部指令碼內容指令碼
- 定時將資料匯入到hive的shell指令碼Hive指令碼
- shell,ant指令碼實現自動資料泵(exp.expdp)匯出匯入資料指令碼
- Excel匯入Sqlserver資料庫指令碼ExcelSQLServer資料庫指令碼
- Elasticsearch批量匯入資料指令碼(python)Elasticsearch指令碼Python
- Oracle expdp/impdp匯入匯出備份資料庫指令碼Oracle資料庫指令碼
- mysqldump壓縮備份匯出匯入(含定期備份shell指令碼)MySql指令碼
- mysql資料庫之間、table增量匯入指令碼MySql資料庫指令碼
- [ Shell ] 通過 Shell 指令碼匯出 CDL 網表指令碼
- MySQL Shell import_table資料匯入MySqlImport
- impdp 匯入 指令碼指令碼
- 使用shell 指令碼備份資料指令碼
- [ Shell ] 通過 Shell 指令碼匯出 GDSII/OASIS 檔案指令碼
- shell入門--第一個shell指令碼指令碼
- MySQL匯出資料庫指令碼MySql資料庫指令碼
- Linux 平臺下 Oracle 資料泵備份(expdp) SHELL 指令碼LinuxOracle指令碼
- MySQL 批量更新、刪除資料shell指令碼MySql指令碼
- MySQL資料庫備份的shell指令碼MySql資料庫指令碼
- 巧用shell生成資料庫檢查指令碼資料庫指令碼
- 建立管理MySQL資料庫的shell指令碼MySql資料庫指令碼
- Oracle Sql loader 匯入資料指令碼的編寫過程OracleSQL指令碼
- 表統計資訊匯出匯入指令碼指令碼
- Windows 下 MySQL 資料匯入 RedisWindowsMySqlRedis
- EBS 10g/11g 資料庫匯出匯入遷移指令碼 - transform資料庫指令碼ORM
- 【shell 指令碼】兩種登入方式指令碼
- Centos下使用php呼叫shell指令碼CentOSPHP指令碼
- 通過shell指令碼批量操作mysql資料庫指令碼MySql資料庫
- Liunx備份mysql資料庫的shell指令碼MySql資料庫指令碼
- 【HIVE】hive 使用shell指令碼跑歷史資料Hive指令碼
- 巧用shell指令碼分析資料庫使用者指令碼資料庫
- 透過shell指令碼得到資料字典的資訊指令碼
- 通過shell指令碼得到資料字典的資訊指令碼
- 簡單資料庫及表建立shell指令碼資料庫指令碼
- shell指令碼不換行重新整理資料指令碼
- 監控Oracle資料庫的常用shell指令碼Oracle資料庫指令碼
- shell指令碼指令碼
- MySQL資料匯入匯出亂碼問題MySql