oracle數據泵備份(Expdp命令)
Oracle備份方式主要分爲數據泵導出備份、熱備份與冷備份三種,今天首先來實踐一下數據泵備份與還原。數據泵導出/導入屬於邏輯備份,熱備份與冷備份都屬於物理備份。oracle10g開始推出了數據泵(expdp/impdp),可以使用並行參數選項,因此,相對於傳統的exp命令來說,執行效率更高。
一、知曉expdp命令
|
二、準備工作
1、查詢路徑信息
查看已經創建的路徑信息:
SELECT * FROM dba_directories;
2、創建路徑
創建路徑需要sys權限,需要有create any directory權限纔可以創建路徑。
選項:DIRECTORY=directory_object
Directory_object用於指定目錄對象名稱。需要注意,目錄對象是使用CREATE DIRECTORY語句建立的對象,而不是OS目錄。
eg: CREATE OR REPLACE directory backup_path AS 'D:\APP\ORADATA\db_backup'; --創建路徑名爲dackup_path的路徑,並指向硬盤的指定位置
對新創建的路徑進行授權操作:
eg:grant read,write on directory backup_path to orcldev; --將對路徑的讀寫權限分配各orcldev用戶。
三、操作實例
執行expdp和impdp命令需要擁有exp_full_database和imp_full_database權限,授權語句如下:
eg:grant exp_full_database,imp_full_database to orcldev;
1、導出orcldev這個schema的所用對象[schemas or full]
eg:expdp orcldev/oracle@orcldev directory=backup_path dumpfile=orcldev_schema.dmp logfile=orcldev_schema_2013.log schemas=orcldev
2、導出orcldev這個用戶下的某些表[tables]
eg:C:\>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_table.dmp logfile=orcldev_table_2013.log tables=('TAB_TEST','TAB_A')
3、只導出orcldev這個用戶的元數據[content]
eg:C:\>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_meta.dmp logfile=orcldev_meta_2013.log
SCHEMAS=orcldev CONTENT=METADATA_ONLY
4、只導出orcldev這個用戶50%的抽樣數據[sample]
eg:C:\>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_samp.dmp logfile=orcldev_samp_2013.log schemas=orcldev sample=50
5、採用並行方式備份整庫[parallel]
parallel參數只有在oracle10g之後的版本(包含10g)有效。
oracle_online:you can use the DUMPFILE parameter during export operations to specify multiple dump files, by using a substitution variable (%U) in the filename. This is called a dump file template. The new dump files are created as they are needed, beginning with 01 for %U, then using 02,03,and so on.
eg:C:\>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_parallel_%U.dmp logfile=orcldev_parallel_2013.log parallel=4
"%U"表示自動生成遞增的序列號。
6、導出orcldev這個方案對象,但不包含索引[exclude]
eg: --可以剔除的對象有:VIEW,PACKAGE,FUNCTION,index,constraints,table,schema,user等等
1) C:\>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_exclude.dmp logfile=orcldev_exclude.log SCHEMAS=orcldev EXCLUDE=index
2) C:\>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_exclude.dmp logfile=orcldev_exclude.log SCHEMAS=orcldev EXCLUDE=INDEX:"LIKE 'TEST%'" --導出這個orcldev方案,剔除以TEST開頭的索引
3) C:\>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_exclude.dmp logfile=orcldev_exclude.log EXCLUDE=SCHEMA:"='SCOTT'"
C:\>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_exclude.dmp logfile=orcldev_exclude.log EXCLUDE=USER:"='SCOTT'"
--備份整庫但剔除SCOTT這個用戶的對象。
注意:include與exclude不能同時使用。
7、PARFILE選項
expdp命令可以調用parfile文件,在parfile裏可以寫備份腳本,可以使用query選項。
Oracle highly recommends that you place QUERY
specifications in a parameter file; otherwise, you might have to use operating system-specific escape characters on the command line before each quotation mark.
如expdp.txt 內容如下:
USERID=orcldev/oracle directory=dackup_path dumpfile=orcldev_parfile.dmp logfile=orcldev_parfile.log TABLES='TAB_TEST' QUERY="WHERE TRAN_DATE=TO_DATE('2013-08-31','YYYY-MM-DD')"
執行方法:expdp parfile=expdp.txt 即可執行備份
使用parfile好處是使用query選項是不用使用轉義字符,如果將query參數放到外邊的話,需要將""進行轉義。
eg:
UNIX寫法:
expdp orcldev/oracle directory=backup_path dumpfile=2013.dmp logfile =2013.log schemas=orcldev INCLUDE=TABLE:\"IN \(\'TEST_A\',\'TEST_B\'\)\" --在Unix系統執行是需要將單引號進行轉義操作,否則會報錯。
WINDOWS寫法:
expdp orcldev/oracle directory=backup_path dumpfile=2013.dmp logfile =2013.log schemas=orcldev INCLUDE=TABLE:"IN \('TEST_A','TEST_B')"
8、TABLESPACE導出表空間
eg:expdp orcldev/oracle directory=backup_path dumpfile=2013.dmp logfile =2013.log tablespaces=user,orcldev
9、Version選項
VERSION選項默認值是COMPATIBLE,即兼容模式。在我們備份的時候,可以指定版本號。
eg:expdp orcldev/oracle directory=backup_path dumpfile=2013.dmp logfile =2013.log full=Y VERSION=10.2.0.4
10、FLASHBACK_TIME選項
指定導出特定時間點的表數據,可以聯繫一下FLASHBACK功能。
eg:C:\>expdp orcldev/oracle directory=dackup_path dumpfile=orcldev_flash.dmp logfile=orcldev_flash.log SCHEMAS=orcldev FLASHBACK_TIME="TO_TIMESTAMP('2013-09-28 14:30:00','DD-MM-YYYY HH24:MI:SS')"
四、參考