Oracle12c的datapump功能跟以前差不多,在多租戶的環(huán)境中執(zhí)行導(dǎo)入導(dǎo)出以及使用一些更細(xì)化的參數(shù)的時候,幾乎沒有區(qū)別,依然很好用,效率很高。目前有很多的用戶仍然在使用exp/imp工具在執(zhí)行一些遷移、備份、過濾和轉(zhuǎn)移數(shù)據(jù)的工作,相比起來,數(shù)據(jù)泵的效率更高、更易用并且更方便管理,但exp/imp在有些時候可以完成datapump不適用的情況。兩者的操作風(fēng)格和格式很相似,相信在您掌握了其中任一種工具之后,另一種會很容易上手。
數(shù)據(jù)泵可以高效備份、復(fù)制、保護(hù)和傳輸大量的數(shù)據(jù)和源數(shù)據(jù)。在導(dǎo)入和導(dǎo)出過程中可以做到過濾數(shù)據(jù)和對象,并且能夠在全數(shù)據(jù)庫級、方案級、表級和表空間級實現(xiàn)導(dǎo)入導(dǎo)出。
在此,我們簡單體驗一下數(shù)據(jù)泵在可插拔數(shù)據(jù)庫中的基本操作過程,你將會發(fā)現(xiàn),跟以前傳統(tǒng)的非插拔數(shù)據(jù)庫幾乎沒有太大區(qū)別。
在Oracle12c的可插拔數(shù)據(jù)庫環(huán)境中,首先啟動CDB:
[oracle@cafe ~]$ export ORACLE_SID=cup -----CDB的名字自定義為cup,顧名思義cup作為容器儲存數(shù)據(jù)庫,我們的PDB自定義為tea。
[oracle@cafe ~]$ sqlplus / as sysdba
SQL*Plus: Release 12.1.0.2.0 Production on Fri Jan 23 15:06:41 2015
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to an idle instance.
SQL> startup -----啟動CDB
ORACLE instance started.
Total System Global Area 767557632 bytes
Fixed Size 2929112 bytes
Variable Size 574623272 bytes
Database Buffers 184549376 bytes
Redo Buffers 5455872 bytes
Database mounted.
Database opened.
SQL> alter pluggable database tea open; -----在CDB中啟動可插拔數(shù)據(jù)庫(PDB)tea。
Pluggable database altered.
SQL> alter session set container = tea; -----切換到PDB中(tea)。
Session altered.
SQL> show con_name
CON_NAME
------------------------------
TEA -----經(jīng)驗證,已切換到tea數(shù)據(jù)庫容器。
SQL> show parameter service;
做數(shù)據(jù)泵的導(dǎo)出需要首先創(chuàng)建目錄對象,主要用于作為導(dǎo)出目的地存放導(dǎo)出文件用,應(yīng)該事檢查操作系統(tǒng)中有充足的空間可用并且該空間在數(shù)據(jù)庫服務(wù)器本地。這樣做的好處之一是可以對目錄結(jié)構(gòu)等信息達(dá)到保密的安全效果,不贊成使用默認(rèn)的路徑,因為可能會導(dǎo)致空間不足問題,以至于影響數(shù)據(jù)庫正常功能。
在數(shù)據(jù)庫系統(tǒng)中創(chuàng)建相關(guān)目錄:
[root@cafe /]# mkdir /oradump
[root@cafe /]# chmod -R 777 /oradump
[root@cafe /]# chown -R oracle.oinstall /oradump
SQL> create directory dump_dir as '/oradump'; -----在數(shù)據(jù)庫中創(chuàng)建該目錄的對象信息。
Directory created.
SQL> select directory_name,directory_path from dba_directories;
DIRECTORY_NAME DIRECTORY_PATH
-------------- --------------
DUMP_DIR /oradump
插入測試數(shù)據(jù),以備驗證后期驗證導(dǎo)出、導(dǎo)入的效果:
SQL> create table test(id number);
Table created.
SQL> insert into test values(1);
1 row created.
SQL> CREATE USER eric IDENTIFIED BY gao ACCOUNT UNLOCK;
User created.
SQL> grant dba to eric;
Grant succeeded.接下來我們執(zhí)行一次全庫導(dǎo)出:
expdp eric/gao directory=dump_dir dumpfile=fullbak.dmp logfile=fullbak.log full=y
參數(shù)解釋:
directory:指定的是咱們剛創(chuàng)建的目錄對象中的路徑別名,在dba_directories中對應(yīng)著directory_name字段。
dumpfile:自定義導(dǎo)出文件的filename。
logfile:可以把整個導(dǎo)入、導(dǎo)出過程中的操作信息輸出到文件中,以便后期驗證備份效果和排錯用。
full:等于y的時候為全庫導(dǎo)出模式。
注意:導(dǎo)出所用的用戶不要用sys,一般用具有dba角色的用戶即可,system也可用。
導(dǎo)出過程:
[oracle@cafe ~]$ expdp eric/gao@tea directory=dump_dir dumpfile=fullbak.dmp logfile=fullbak.log full=y
Export: Release 12.1.0.2.0 - Production on Fri Jan 23 18:34:12 2015 -----此行包含版本、導(dǎo)出操作開始的時間,該時間可用于計算導(dǎo)出總時間和記錄操作時間點。
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Starting "ERIC"."SYS_EXPORT_FULL_01": eric/********@tea directory=dump_dir dumpfile=fullbak.dmp logfile=fullbak.log full=y "ERIC"."SYS_EXPORT_FULL_01"為當(dāng)前執(zhí)行的job的名字,可用于中途介入導(dǎo)入、導(dǎo)出過程中調(diào)整策略和查看狀態(tài)。
Estimate in progress using BLOCKS method...
Processing object type DATABASE_EXPORT/EARLY_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/TABLE_DATA
Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 4.265 MB 估算本次導(dǎo)入、導(dǎo)出的總大小。
...省略多行...
Processing object type DATABASE_EXPORT/PRE_SYSTEM_IMPCALLOUT/MARKER
Processing object type DATABASE_EXPORT/PRE_INSTANCE_IMPCALLOUT/MARKER
Processing object type DATABASE_EXPORT/TABLESPACE
. . exported "SYS"."KU$_USER_MAPPING_VIEW" 6.109 KB 38 rows
...省略多行...
. . exported "SYS"."NACL$_WALLET_EXP" 0 KB 0 rows
. . exported "WMSYS"."WM$EXP_MAP" 7.718 KB 3 rows
Master table "ERIC"."SYS_EXPORT_FULL_01" successfully loaded/unloaded
******************************************************************************
Dump file set for ERIC.SYS_EXPORT_FULL_01 is:
/oradump/fullbak.dmp
Job "ERIC"."SYS_EXPORT_FULL_01" successfully completed at Fri Jan 23 18:49:29 2015 elapsed 0 00:12:55
此行包含job名稱,完成時間和總共使用時間,有時可能沒有elapsed,可以根據(jù)該操作剛開始的時間做減法,算總時間。
查看一下導(dǎo)出的文件:
[root@cafe oradump]# ls -lh
總用量 2.8M
-rw-r----- 1 oracle oinstall 2.8M 1月 23 18:49 fullbak.dmp
導(dǎo)出的實際容量和其估算的容量還是有一定出入的,因此建議在準(zhǔn)備存儲空間的時候多預(yù)留一些。
-rw-r--r-- 1 oracle oinstall 9.4K 1月 23 18:49 fullbak.log
我們也可以只估算導(dǎo)出文件的大小而不進(jìn)行實際的導(dǎo)出操作:
expdp eric/gao@tea estimate_only=y directory=dump_dir logfile=n full=y
此處我們使用了estimate_only參數(shù),將該參數(shù)置于y狀態(tài)即可開啟估算功能,此處不需要寫dumpfile參數(shù),否則可能會報錯。
輸出結(jié)果:
[oracle@cafe ~]$ expdp eric/gao@tea estimate_only=y directory=dump_dir logfile=n full=y
Export: Release 12.1.0.2.0 - Production on Fri Jan 23 22:08:30 2015
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Starting "ERIC"."SYS_EXPORT_FULL_01": eric/********@tea estimate_only=y directory=dump_dir logfile=n full=y
Estimate in progress using BLOCKS method...
Processing object type DATABASE_EXPORT/EARLY_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/TABLE_DATA
Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA
. estimated "SYS"."KU$_USER_MAPPING_VIEW" 16 KB
. estimated "ORDDATA"."ORDDCM_DOCS" 1.25 MB
. estimated "WMSYS"."WM$CONSTRAINTS_TABLE 320 KB
. estimated "WMSYS"."WM$LOCKROWS_INFO 192 KB
. estimated "WMSYS"."WM$UDTRIG_INFO 192 KB
. estimated "LBACSYS"."OLS$AUDIT_ACTIONS" 64 KB
. estimated "LBACSYS"."OLS$DIP_EVENTS" 64 KB
. estimated "LBACSYS"."OLS$INSTALLATIONS" 64 KB
. estimated "LBACSYS"."OLS$PROPS" 64 KB
...省略多行...
Total estimation using BLOCKS method: 4.265 MB
此處我們看到,該值跟咱們上一次做實際全庫導(dǎo)出過程中算的值一樣。
Job "ERIC"."SYS_EXPORT_FULL_01" successfully completed at Fri Jan 23 22:10:18 2015 elapsed 0 00:01:32
數(shù)據(jù)泵有一個好處,那就是交互操作模式,我們可以在作業(yè)運行中去監(jiān)控運行狀態(tài),并且可以暫停、啟動作業(yè),也可以終止作業(yè)。
查看正在執(zhí)行的導(dǎo)入導(dǎo)出狀態(tài):
Export> status -----查看當(dāng)前作業(yè)狀態(tài)
Job: SYS_EXPORT_FULL_02 -----作業(yè)名,上面的全庫導(dǎo)出我們也看到過有對應(yīng)的名字
Operation: EXPORT -----操作類型:
Mode: FULL -----模式:全庫導(dǎo)出
State: EXECUTING -----狀態(tài):執(zhí)行中
Bytes Processed: 0
Current Parallelism: 1 -----當(dāng)前并行數(shù),如果指定了parallel參數(shù),便會影響該值,并且下面會對應(yīng)的有worker1~workerN
Job Error Count: 0
Dump File: /oradump/fullbak.dmp
bytes written: 4,096 -----已寫入的字節(jié)
Worker 1 Status:
Instance ID: 1
Instance name: cup -----實例名為cup,容器數(shù)據(jù)庫CDB的名稱
Host name: cafe -----操作系統(tǒng)主機名
Process Name: DW00 -----相關(guān)寫進(jìn)程,如果設(shè)置了parallel的話,會有更多DWNN。
State: EXECUTING
返回導(dǎo)入/導(dǎo)出命令行輸出狀態(tài):
Export> continue_client
Processing object type DATABASE_EXPORT/EARLY_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/TABLE_DATA
Processing object type DATABASE_EXPORT/NORMAL_OPTIONS/VIEWS_AS_TABLES/TABLE_DATA
Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA
Total estimation using BLOCKS method: 4.578 MB
Processing object type DATABASE_EXPORT/PRE_SYSTEM_IMPCALLOUT/MARKER
Processing object type DATABASE_EXPORT/PRE_INSTANCE_IMPCALLOUT/MARKER
Processing object type DATABASE_EXPORT/TABLESPACE
Processing object type DATABASE_EXPORT/PROFILE
暫停正在運行的導(dǎo)入/導(dǎo)出操作,該操作不會導(dǎo)致作業(yè)停止,只是暫時停止,類似于斷點續(xù)傳:
Export> stop_job
Are you sure you wish to stop this job ([yes]/no): yes
附加正在運行的作業(yè),在這里就用到了咱們一再提到的job名稱了,使用attach參數(shù)指定作業(yè)名稱就可附加到正在執(zhí)行或者是暫停的作業(yè)中去:
[oracle@cafe oradump]$ expdp eric/gao@tea attach=eric.SYS_EXPORT_FULL_02
Export: Release 12.1.0.2.0 - Production on Fri Jan 23 22:24:04 2015
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Job: SYS_EXPORT_FULL_02
Owner: ERIC
Operation: EXPORT
Creator Privs: TRUE
GUID: 0D537C91F12C103FE0537EE0A8C01C87
Start Time: Friday, 23 January, 2015 22:24:16
Mode: FULL
Instance: cup
Max Parallelism: 1
Timezone: -07:00
Timezone version: 18
Endianness: LITTLE
NLS character set: ZHS16GBK
NLS NCHAR character set: AL16UTF16
EXPORT Job Parameters:
Parameter Name Parameter Value:
CLIENT_COMMAND eric/********@tea directory=dump_dir dumpfile=fullbak.dmp logfile=fullbak.log full=y
State: IDLING -----在這里我們看到j(luò)ob是處于暫停狀態(tài)的
Bytes Processed: 0
Current Parallelism: 1
Job Error Count: 0
Dump File: /oradump/fullbak.dmp
bytes written: 4,096
Worker 1 Status:
Instance ID: 1
Instance name: cup
Host name: cafe
Process Name: DW00
State: UNDEFINED
啟動暫停的作業(yè):
Export> start_job再次查看一下作業(yè)的狀態(tài)有沒有發(fā)生變化:
Export> status
Job: SYS_EXPORT_FULL_02
Operation: EXPORT
Mode: FULL
State: EXECUTING
Bytes Processed: 0
Current Parallelism: 1
Job Error Count: 0
Dump File: /oradump/fullbak.dmp
bytes written: 69,632
Worker 1 Status:
Instance ID: 1
Instance name: cup
Host name: cafe
Process Name: DW00
State: EXECUTING -----作業(yè)繼續(xù)執(zhí)行了
Object Schema: SYS
Object Name: KU$_USER_MAPPING_VIEW
Object Type: DATABASE_EXPORT/EARLY_OPTIONS/VIEWS_AS_TABLES/TABLE
Completed Objects: 1
Worker Parallelism: 1
終止數(shù)據(jù)泵作業(yè),在作業(yè)執(zhí)行中途遇到問題不得不停止的時候,可以用kill_job命令終止操作:
Export> kill_job
Are you sure you wish to stop this job ([yes]/no): yes
終止之后系統(tǒng)中不會有實際的導(dǎo)出文件生成:
[oracle@cafe oradump]$ ls -lt
總用量 4 -rw-r--r-- 1 oracle oinstall 1398 1月 23 22:31 fullbak.log
查看數(shù)據(jù)泵輸出的日志,我們看一下終止操作在日志中的信息:
Job "ERIC"."SYS_EXPORT_FULL_02" stopped due to fatal error at Fri Jan 23 22:31:01 2015 elapsed 0 00:06:48
我們刪除數(shù)據(jù)庫tea中的表,然后在數(shù)據(jù)庫全備的文件中恢復(fù)該表,以驗證導(dǎo)出的效果:
SQL> drop table test;
Table dropped.
SQL> select * from test;
select * from test
*
ERROR at line 1:
ORA-00942: table or view does not exist -----該表已不存在。
然后從全備份中單獨導(dǎo)入test表,在這里我們用全備的備份集,使用table參數(shù)保證單獨導(dǎo)入該表,保證減小系統(tǒng)開銷:
[oracle@cafe oradump]$ impdp eric/gao@tea directory=dump_dir dumpfile=fullbak.dmp logfile=imptab.log tables=test
Import: Release 12.1.0.2.0 - Production on Fri Jan 23 23:20:31 2015
Copyright (c) 1982, 2014, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
Master table "ERIC"."SYS_IMPORT_TABLE_01" successfully loaded/unloaded
Starting "ERIC"."SYS_IMPORT_TABLE_01": eric/********@tea directory=dump_dir dumpfile=fullbak.dmp logfile=imptab.log tables=test
Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE
Processing object type DATABASE_EXPORT/SCHEMA/TABLE/TABLE_DATA
. . imported "ERIC"."TEST" 5.046 KB 1 rows -----已導(dǎo)入1行數(shù)據(jù),5K的數(shù)據(jù)量。
Processing object type DATABASE_EXPORT/SCHEMA/TABLE/STATISTICS/TABLE_STATISTICS
Processing object type DATABASE_EXPORT/STATISTICS/MARKER
Job "ERIC"."SYS_IMPORT_TABLE_01" successfully completed at Fri Jan 23 23:21:37 2015 elapsed 0 00:00:59
驗證表test已經(jīng)恢復(fù)到了刪除之前的狀態(tài):
[oracle@cafe oradump]$ sqlplus eric/gao@tea
SQL*Plus: Release 12.1.0.2.0 Production on Fri Jan 23 23:26:53 2015
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Last Successful login time: Fri Jan 23 2015 23:23:49 +08:00
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
SQL> select * from test;
OK,數(shù)據(jù)已回來。在實際項目中,可能數(shù)據(jù)量、環(huán)境和客戶要求都比此次試驗規(guī)模龐大、情況復(fù)雜,但是萬變不離其宗,切記一定實現(xiàn)規(guī)劃好備份空間,密切的關(guān)注備份狀態(tài)。
本文為CSDN原創(chuàng)文章,未經(jīng)允許不得轉(zhuǎn)載,如需轉(zhuǎn)載請聯(lián)系market#csdn.net(#換成@)