用dbms_streams_tablespace_adm來表空間的遷移(2)

jolly10發表於2009-10-26
由於PULL_SIMPLE_TABLESPACE只能遷移一個資料檔案的表空間,多於一個資料檔案的表空間需要用PULL_TABLESPACES來完成。
其中PULL過程實際上包含了CLONE以及ATTACH兩個過程,其中還包括了DBMS_FILE_TRANSFER包的呼叫,使得在目標資料庫執行一個命令就完成表空間遷移的所有的操作。[@more@]

PULL_TABLESPACES過程完成以下動作

1. Makes any read/write tablespace in the specified tablespace set at the remote database read-only
2. Uses Data Pump to export the metadata for the tablespaces in the tablespace set
3. Uses a database link and the DBMS_FILE_TRANSFER package to transfer the datafiles for the tablespace set and the

log file for the Data Pump export to the current database
4. Places the datafiles that comprise the specified tablespace set in the specified directories at the local

database
5. Places the log file for the Data Pump export in the specified directory at the local database
6. If this procedure made a tablespace read-only, then makes the tablespace read/write
7. Uses Data Pump to import the metadata for the tablespaces in the tablespace set at the local database

在我的測試中,源資料庫以及目標資料庫的環境一致,只是有小版本的差異,現只檢查一下需要遷移表空間的情況:

SQL> SELECT FILE_NAME FROM DBA_DATA_FILES WHERE TABLESPACE_NAME ='NADSPACE';

FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/NADSPACE001.dbf
/u01/app/oracle/oradata/orcl/NADSPACE002.dbf


directory在源庫和目標庫在上個便子中都有建立好

需要遷移的表空間的使用者以及許可權需要建立好

源庫的情況:

SQL> SELECT DISTINCT OWNER FROM DBA_SEGMENTS
2 WHERE TABLESPACE_NAME IN ('NADSPACE');

OWNER
------------------------------
NAD
SCOTT


SQL> SELECT 'GRANT ' || GRANTED_ROLE || ' TO ' || GRANTEE || ';'
2 FROM DBA_ROLE_PRIVS
3 WHERE GRANTEE IN ('NAD','SCOTT')
4 ORDER BY GRANTEE, GRANTED_ROLE;

'GRANT'||GRANTED_ROLE||'TO'||GRANTEE||';'
-----------------------------------------------------------------------
GRANT CONNECT TO NAD;
GRANT RESOURCE TO NAD;
GRANT CONNECT TO SCOTT;
GRANT RESOURCE TO SCOTT;


SQL> SELECT 'GRANT ' || PRIVILEGE || ' TO ' || GRANTEE || ';'
2 FROM DBA_SYS_PRIVS
3 WHERE GRANTEE IN ('NAD','SCOTT')
4 AND PRIVILEGE NOT IN
5 (SELECT PRIVILEGE FROM DBA_SYS_PRIVS
6 WHERE GRANTEE IN ('CONNECT', 'RESOURCE'))
7 ORDER BY GRANTEE;

'GRANT'||PRIVILEGE||'TO'||GRANTEE||';'
--------------------------------------------------------------------------------
GRANT UNLIMITED TABLESPACE TO NAD;
GRANT UNLIMITED TABLESPACE TO SCOTT;


利用上面的授權,就可以在目標資料庫建立使用者,並授權,篇幅所限,這個過程就省略了。


下面就可以執行PULL_TABLESPACES過程了,需要注意,這個過程的輸入引數包括了索引組織表型別和記錄,因此需要透過建立一個PL/SQL匿名塊來完成呼叫,注意輸入引數變數的宣告需要用DBMS_STREAMS_TABLESPACE_ADM包中的型別進行宣告,否則呼叫會報錯

SQL> DECLARE
V_JOB_NAME VARCHAR2(30) := 'MY_IMP_TABLESPACE';
V_TABLESPACE_NAME DBMS_STREAMS_TABLESPACE_ADM.TABLESPACE_SET;
V_DIRECTORY_NAME DBMS_STREAMS_TABLESPACE_ADM.DIRECTORY_OBJECT_SET;
V_LOGFILE DBMS_STREAMS_TABLESPACE_ADM.FILE;
BEGIN
V_TABLESPACE_NAME(1) := 'NADSPACE';
V_DIRECTORY_NAME(1) := 'D_TRANS';
V_LOGFILE.DIRECTORY_OBJECT := 'D_TRANS';
V_LOGFILE.FILE_NAME := 'imp_tablespaces.log';
DBMS_STREAMS_TABLESPACE_ADM.PULL_TABLESPACES(V_JOB_NAME, 'NAD', V_TABLESPACE_NAME, V_DIRECTORY_NAME, V_LOGFILE);
END;
13 /

PL/SQL procedure successfully completed.

檢視目標庫的情況:

SQL> SELECT FILE_NAME FROM DBA_DATA_FILES WHERE TABLESPACE_NAME ='NADSPACE';

FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/NADSPACE002
/u01/app/oracle/oradata/orcl/NADSPACE001

發現遷移過來的資料檔案的字尾沒有了,還不知道原因。

表空間遷移完成後,載入的表空間處於只讀狀態,可以根據具體的需要決定是否將其置為可寫狀態。

以上的例子是源資料庫的表空間的資料檔案都在同一個目錄下,且目標資料庫也是在一個目錄下的。如果datafile在多個目錄下該如何操作?在平時工作中一般很少一個表空間的資料檔案都在一個目錄下。

檢查源庫

SQL> SELECT FILE_NAME FROM DBA_DATA_FILES WHERE TABLESPACE_NAME ='NADSPACE';

FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/NADSPACE001.dbf
/u01/app/oracle/oradata/orcl/NADSPACE002.dbf
/u02/app/oracle/oradata/orcl/NADSPACE003.dbf
/u02/app/oracle/oradata/orcl/NADSPACE004.dbf


建立/u02的directory

CREATE OR REPLACE DIRECTORY
D_DATAFILE_2 AS
'/u02/app/oracle/oradata/orcl';


Prompt Privs on DIRECTORY D_DATAFILE_2 TO SYSTEM to SYSTEM;
GRANT READ, WRITE ON DIRECTORY SYS.D_DATAFILE_2 TO SYSTEM;

SQL> select * from dba_directories where directory_name like 'D_DATAFILE%'
2 ;

OWNER DIRECTORY_NAME DIRECTORY_PATH
------------------------------ --------------- --------------------------------------------------
SYS D_DATAFILE /u01/app/oracle/oradata/orcl
SYS D_DATAFILE_2 /u02/app/oracle/oradata/orcl


檢查目錄庫的directory

CREATE OR REPLACE DIRECTORY
D_TRANS_2 AS
'/u02/app/oracle/oradata/orcl';


Prompt Privs on DIRECTORY D_TRANS_2 TO SYSTEM to SYSTEM;
GRANT READ, WRITE ON DIRECTORY SYS.D_TRANS_2 TO SYSTEM;


SQL> select * from dba_directories where directory_name like 'D_TRANS%'
2 ;

OWNER DIRECTORY_NAME DIRECTORY_PATH
------------------------------ --------------- --------------------------------------------------
SYS D_TRANS /u01/app/oracle/oradata/orcl
SYS D_TRANS_2 /u02/app/oracle/oradata/orcl



指定多個V_DIRECTORY_NAME的值,分別對應每個datafile的directory

SQL> DECLARE
V_JOB_NAME VARCHAR2(30) := 'MY_IMP_TABLESPACE';
V_TABLESPACE_NAME DBMS_STREAMS_TABLESPACE_ADM.TABLESPACE_SET;
V_DIRECTORY_NAME DBMS_STREAMS_TABLESPACE_ADM.DIRECTORY_OBJECT_SET;
V_LOGFILE DBMS_STREAMS_TABLESPACE_ADM.FILE;
BEGIN
V_TABLESPACE_NAME(1) := 'NADSPACE';
V_DIRECTORY_NAME(1) := 'D_TRANS';
V_DIRECTORY_NAME(2) := 'D_TRANS';
V_DIRECTORY_NAME(3) := 'D_TRANS_2';
V_DIRECTORY_NAME(4) := 'D_TRANS';
V_LOGFILE.DIRECTORY_OBJECT := 'D_TRANS';
V_LOGFILE.FILE_NAME := 'imp_tablespaces.log';
DBMS_STREAMS_TABLESPACE_ADM.PULL_TABLESPACES(V_JOB_NAME, 'NAD', V_TABLESPACE_NAME, V_DIRECTORY_NAME, V_LOGFILE);
END;

PL/SQL procedure successfully completed.

完成後檢查目標庫的datafile,由於只有V_DIRECTORY_NAME(3) 指定的是/u02的目標,所以導進來的第3個datafile是放在/u02的,其它的都在/u01上。

SQL> SELECT FILE_NAME FROM DBA_DATA_FILES WHERE TABLESPACE_NAME ='NADSPACE';

FILE_NAME
--------------------------------------------------------------------------------
/u01/app/oracle/oradata/orcl/NADSPACE004
/u02/app/oracle/oradata/orcl/NADSPACE003
/u01/app/oracle/oradata/orcl/NADSPACE002
/u01/app/oracle/oradata/orcl/NADSPACE001

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

相關文章