1. 程式人生 > 實用技巧 >oracle的匯入匯出expdp和impdp

oracle的匯入匯出expdp和impdp

環境:oracle12c

1、exp、imp和expdp、impdp的區別

前者是客戶端工具程式,通過網路的方式匯出檔案到本地。

後者是伺服器端工具程式,直接在伺服器端進行匯入匯出,不走網路

2、expdp、impdp檢視幫助方式

$ expdp help=y
$ impdp help=y

3、expdp匯出

Format:  expdp KEYWORD=value or KEYWORD=(value1,value2,...,valueN)

1)Fine-grained object and data selection 
過濾資料和過濾物件
CONTENT Specifies data to unload.
Valid keyword values are: [ALL], DATA_ONLY and METADATA_ONLY.

QUERY
Predicate clause used to export a subset of a table.
For example, QUERY
=employees:"WHERE department_id > 10". INCLUDE Include specific object types. For example, INCLUDE=TABLE_DATA. include=[object_type]:[name_clause] include=package,function,table:"='emp'" include=table:"IN('dept','emp')" include=table,view EXCLUDE Exclude specific object types. For example, EXCLUDE
=SCHEMA:"='HR'". SAMPLE Percentage of data to be exported. sample=emp:50 REUSE_DUMPFILES Overwrite destination dump file if it exists [NO]. LOGFILE Specify log file name [export.log]. JOB_NAME Name of export job to create. ESTIMATE_ONLY Calculate job estimates without performing the export [NO]. ESTIMATE Calculate job estimates. Valid keyword values are: [BLOCKS] and STATISTICS. DUMPFILE Specify list of destination dump
file names [expdat.dmp]. For example, DUMPFILE=scott1.dmp, scott2.dmp, dmpdir:scott3.dmp. DIRECTORY Directory object to be used for dump and log files.

4、建立datapump的匯出檔案儲存的目錄物件-directorie

desc dba_directories ;                             #資料字典
SELECT * FROM dba_directories;                     #試圖
[oracle@12c ~]$ mkdir dpdir
[oracle@12c ~]$ ll /home/oracle/dpdir
CREATE DIRECTORY dpdir AS '/home/oracle/dpdir';    #建立directory

SELECT * FROM dba_directories
WHERE directory_name='DPDIR';
SYS    DPDIR    /home/oracle/dpdir    0

每個目錄都包含了read write兩個許可權 將該許可權授予使用者或角色
SELECT * FROM dba_tab_privs
WHERE grantee='HR' AND table_name='DPDIR';  --no rows

GRANT READ,WRITE ON DIRECTORY dpdir TO hr;      #賦予使用者讀寫許可權
SELECT * FROM dba_tab_privs
WHERE grantee='HR' AND table_name='DPDIR';      #檢視使用者許可權
HR    SYS    DPDIR    SYS    READ    NO    NO    NO    DIRECTORY    NO
HR    SYS    DPDIR    SYS    WRITE    NO    NO    NO    DIRECTORY

5、執行方式

命令列介面
引數檔案介面    使用parfile   PARFILE Specify parameter file name.
互動式命令介面  expdp  impdp
EMCC WEB介面

6、匯入匯出的模式

full                                      #全庫
schema                                    #物件
table                                     #表
tablespace                                #表空間
tansportable tablespace
transportable database

7、匯出table

TABLES  
Identifies a list of tables to export.
For example, TABLES=HR.EMPLOYEES,SH.SALES:SALES_1995.   #命令模式

[oracle@12c /]$ expdp hr/hr directory=dpdir dumpfile=employees.dmp tables=employees logfile=employees.log job_name=employees_job
[oracle@12c /]$ expdp hr/hr directory=dpdir dumpfile=employees.dmp tables=employees logfile=employees.log job_name=employees_job reuse_dumpfiles=yes
[oracle@12c /]$ expdp hr/hr directory=dpdir dumpfile=employees.dmp tables=employees logfile=employees.log job_name=employees_job reuse_dumpfiles=yes query="'WHERE rownum<11'"
$ vim employees.par                                     #引數檔案模式
directory=dpdir
dumpfile=employee.dmp
tables=employees,departments
logfile=employee.log
content=metadata_only
job_name=employees_job
reuse_dumpfiles=yes
[oracle@12c ~]$ expdp hr/hr parfile=/home/oracle/employees.par

8、匯入匯出schemas

schemas 
[oracle@12c ~]$ expdp system/oracle directory=dpdir dumpfile=hr_schema.dmp schemas=hr logfile=hr_schema.log job_name=hr_schema_job

[oracle@12c ~]$ expdp \'sys/oracle as sysdba\' directory=dpdir dumpfile=hr_schema.dmp schemas=hr logfile=hr_schema.log job_name=hr_schema_job reuse_dumpfiles=yes


SELECT * FROM dba_roles          #檢視匯入匯出角色許可權
WHERE role LIKE 'EXP%' OR role LIKE 'IMP%';
EXP_FULL_DATABASE    14    NO    NONE    YES    Y    YES    NO
IMP_FULL_DATABASE    15    NO    NONE    YES    Y    YES    NO

PARALLEL Change the number of active workers for current job.

9、全庫匯入匯出

FULL Export entire database [NO].
[oracle@12c ~]$ expdp \'sys/oracle as sysdba\' directory=dpdir dumpfile=full20200718.dmp full=y logfile=full20200718.log job_name=full20200818_job reuse_dumpfiles=yes

10、表空間匯出匯入

#模擬表空間中有不同型別資料table、index、其他使用者表
Identifies a list of tablespaces to export.
SELECT tablespace_name FROM dba_tablespaces;

SELECT tablespace_name,file_name FROM dba_data_files;

CREATE TABLESPACE tbs05 
DATAFILE '/u01/app/oradata/ORCL/datafile/tbs0501.dbf' SIZE 100M;

CREATE TABLE hr.tt01 TABLESPACE  tbs05
AS
SELECT * FROM dba_objects;

SQL> conn hr/hr
Connected.
SQL> DROP TABLE TEST01;

Table dropped.
SQL> CREATE TABLE test01(
  2  ID NUMBER PRIMARY KEY,
  3  name VARCHAR2(20)
  4  ) TABLESPACE tbs05;

Table created.
SQL> INSERT INTO test01 VALUES(1,'A');
1 row created.
SQL> COMMIT;


SELECT table_name,owner FROM dba_tables
WHERE tablespace_name='TBS05';
TEST01    SYS
TT01    HR
TEST01    HR

SELECT index_name,owner FROM dba_indexes
WHERE tablespace_name='TBS05';
SYS_C007706    HR
SYS_C007708    SYS

[oracle@12c ~]$ expdp \'sys/oracle as sysdba\' directory=dpdir dumpfile=tbs05_0718.dmp logfile=tbs05_0718.log tablespaces=tbs05

SELECT file_name FROM dba_data_files
WHERE tablespace_name='TBS05';
/u01/app/oradata/ORCL/datafile/tbs0501.dbf
--模擬表空間檔案出現問題  使用impdp進行恢復

11、可傳輸表空間TRANSPORT_TABLESPACES(方法:將表的源資料(表的定義)匯出,匯入到目標庫,data_file直接拷貝到目標庫

TRANSPORT_TABLESPACES  TTS 將表空間從一個數據庫copy到另一個數據庫  可以跨平臺
List of tablespaces from which metadata will be unloaded.   表空間物件的metadata


1、檢視源和目標端作業系統的平臺是否支援
SELECT * FROM v$transportable_platform
ORDER BY platform_id;

0x123456
big   低地址存放高位  buf[0] 0x12、 buf[1] 0x34 、 buf[2] 0x56
little  低地址存放低位 buf[0] 0x56、 buf[1] 0x34 、 buf[2] 0x12


SELECT * FROM v$transportable_platform
ORDER BY platform_id;

desc v$database

SELECT d.platform_name,endian_format FROM v$transportable_platform tp,v$database d
WHERE tp.platform_name=d.platform_name;
Linux x86 64-bit    Little

2 檢查表空間的自包含性
desc dbms_tts
TRANSPORT_SET_CHECK          TS_LIST                  CLOB           IN             
                             INCL_CONSTRAINTS         PL/SQL BOOLEAN IN     DEFAULT 
                             FULL_CHECK               PL/SQL BOOLEAN IN     DEFAULT 
EXECUTE dbms_tts.transport_set_check('TBS05',TRUE,TRUE);

SELECT * FROM transport_set_violations;   --檢查表空間自包含檢查結果
ORA-39907: 索引 HR.TEST02_IDX (在表空間 TBS02 中) 指向表 HR.TEST02 (在表空間 TBS05 中)。
3、將表空間設定為read-only
SELECT tablespace_name,status FROM dba_tablespaces
WHERE tablespace_name='TBS05';
TBS05    ONLINE
ALTER TABLESPACE tbs05 READ ONLY;

SELECT tablespace_name,status FROM dba_tablespaces
WHERE tablespace_name='TBS05';
TBS05    READ ONLY

4、生成transport tablespace Sets
包含表空間的資料檔案   --將原有的檔案直接拷貝到目標
表空間及物件 metadata   --expdp生成
[oracle@12c ~]$ expdp \'sys/oracle as sysdba\' directory=dpdir dumpfile=source_tbs05.dmp transport_tablespaces=tbs05 logfile=source_tbs05.log

SELECT file_name FROM dba_data_files
WHERE tablespace_name='TBS05';

[oracle@12c dpdir]$ ll /home/oracle/dpdir/source_tbs05.dmp 
-rw-r-----. 1 oracle oinstall 249856 Jul 19 16:18 /home/oracle/dpdir/source_tbs05.dmp
/u01/app/oradata/ORCL/datafile/tbs0501.dbf
將metadata檔案和表空間tbs05的資料檔案拷貝到目標伺服器端 

5、將表空間的檔案和metadata的檔案拷貝到目標端
源端:
rman target /
rman> convert tablespace tbs05 to platform '目標平臺' FORMAT '轉換後的檔案';

6、在target系統上使用impdp
$impdp \'sys/oracle as sysdba\' directory=dpdir dumpfile=source_tbs05.dmp transport_datafiles='目標伺服器的資料檔案' logfile=test_tbs05.log
將表空間設定為讀寫


SELECT * FROM dba_datapump_jobs;
SYS    FULL20200818_JOB    EXPORT                            FULL                              NOT RUNNING    0    0    0

SELECT * FROM dba_objects
WHERE object_name='FULL20200818_JOB';
SYS    FULL20200818_JOB        75125    75125    TABLE    19-7月 -20    19-7月 -20    2020-07-19:16:29:45    VALID    N    N    N    1        NONE        N    N    USING_NLS_COMP    N    N                

SELECT * FROM v$fixed_table
WHERE name LIKE '%LONGOPS%'
V$SESSION_LONGOPS    4294951245    VIEW    65537    0
SELECT * FROM v$session_longops
ORDER  BY start_time DESC;

12、匯出互動模式

[oracle@12c dpdir]$ expdp \'sys/oracle as sysdba\' directory=dpdir dumpfile=full20200718.dmp full=y logfile=full20200718.log job_name=full20200818_job reuse_dumpfiles=yes

ctr+c 退出當前的模式
Export> help
Export> status
Export> stop_job   #停止job
Export> start_job
Export> continue_client
$ expdp \'sys/oracle as sysdba\' attach=FULL20200818_JOB    #重新開始匯出

13、匯出重定義(改名稱,替換欄位內容)

#重定義幾種型別
remap_datafile remap_tablespace remap_table remap_data #建立一個表進行測試 CREATE TABLE hr.test01(
id NUMBER, name VARCHAR2(20), val NUMBER ); #插入資料 INSERT INTO hr.test01 VALUES(1,'ABC',1000); INSERT INTO hr.test01 VALUES(2,'EDG',2000); COMMIT; #重定義資料的語法(使用的是package) REMAP_DATA Specify a data conversion function. For example, REMAP_DATA=EMP.EMPNO:REMAPPKG.EMPNO. #對name、val欄位進行重定義,編寫package包定義 CREATE OR REPLACE PACKAGE hr.test01_pkg #包定義 IS FUNCTION f_remap_name(var_name VARCHAR2) RETURN VARCHAR2; FUNCTION f_remap_val(var_val NUMBER) RETURN NUMBER; END; / CREATE OR REPLACE PACKAGE BODY hr.test01_pkg #包主體 IS FUNCTION f_remap_name(var_name VARCHAR2) RETURN VARCHAR2 AS BEGIN RETURN dbms_random.string('A',5); END; FUNCTION f_remap_val(var_val NUMBER) RETURN NUMBER AS BEGIN RETURN floor(dbms_random.value(1,100000)); END; END; / expdp remap_data引數匯出資料進行轉換 #匯出使用包的方法進行資料轉換 expdp hr/hr directory=dpdir dumpfile=test01_0718.dmp tables=test01 remap_data=test01.name:test01_pkg.f_remap_name,test01.val:test01_pkg.f_remap_val SELECT * FROM hr.test01; 1 ABC 1000 2 EDG 2000 impdp hr/hr dumpfile=test01_0718.dmp directory=dpdir tables=test01 remap_table=test01:test02 #直接使用hr進行匯入 impdp \'sys/oracle as sysdba\' dumpfile=test01_0718.dmp directory=dpdir tables=test01 remap_table=test01:test02 remap_schema=hr:scott #使用dba進行匯入 SELECT * FROM hr.test02; 1 lAjvg 22119 2 yLWdW 44967