1. 程式人生 > 其它 >時過境遷:Oracle跨平臺遷移之XTTS方案與實踐

時過境遷:Oracle跨平臺遷移之XTTS方案與實踐

作者簡介

謝金融

雲和恩墨東區交付部 Oracle 工程師,多年來從事 Oracle 第三方服務,曾服務過金融、製造業、物流、政府等許多行業的客戶,精通資料庫遷移、效能優化和故障診斷。

本文由恩墨大講堂146期線上分享整理而成。課程回看可點選文末“閱讀原文”。

1

前期準備

1.1. 檢查資料庫時區,保持兩邊一致

select dbtimezone from dual;
DBTIME
------
+08:00

1.2. 檢查資料庫字符集,保持兩邊一致

select * from nls_database_parameterswhere parameter like '%CHARACTERSET%';
PARAMETER                      VALUE
------------------------------------------------------------------
NLS_CHARACTERSET               AL32UTF8
NLS_NCHAR_CHARACTERSET         UTF8

1.3. 檢查目標端補丁情況

目標端 PSU無特殊需求,可以隨情況安裝最新即可。

select 'opatch',comments fromdba_registry_history
'OPATC COMMENTS
--------------------------------------------------------
opatch PSU 11.2.0.4.4

如果準備階段使用 DBMS_FILE_TRANSFER方法,目標端建議安裝的補丁如下:

Patch 19023822,修復目標端使用 DBMS_FILE_TRANSFER.GET_FILE包獲取源端資料檔案出現 ORA-03106 的情況。

Patch 22171097: MERGE REQUEST ON TOP OF DATABASE PSU11.2.0.4.6 FOR BUGS 17534365 19023822

如果準備階段使用 RMAN 方法,目標端沒有小補丁安裝需求。

1.4. 檢查元件安裝情況,對於不一樣的元件,需要有所標記

Select comp_name from dba_registry;
源端元件情況
COMP_NAME
--------------------------------------------------------------------------------
OracleApplication Express
OracleMultimedia
OracleXML Database
OracleExpression Filter
OracleRules Manager
OracleWorkspace Manager
OracleDatabase Catalog Views
OracleDatabase Packages and Types
JServerJAVA Virtual Machine
OracleXDK
OracleDatabase Java Packages
目標段元件情況
COMP_NAME
--------------------------------------------------------------------------------
OracleEnterprise Manager
OracleWorkspace Manager
OracleDatabase Catalog Views
OracleDatabase Packages and Types

備註:元件不同,可能導致源端的部分物件無法匯入到目標端。

1.5. 檢查是否使用了 Key Compression 的索引組織表

Key compression
Selectindex_name,table_name from dba_indexes where compression=’ENABLED’;

索引組織表

Select owner,table_namefrom dba_tables where iot_type is not null;

如果存在,目標端需要安裝 Patch 14835322,否則索引組織表的物件無法匯入到目標端。

目前該補丁只在 11.2.0.3 版本下提供,針對其他版本,如果 IOT表或者鍵值壓縮索引無法匯入時,建議在元資料匯入完成後,將該類物件傳輸過去。

1.6. 檢查源端是否有 Cluster 物件

If the sourcecontains cluster objects, then run "analyze cluster &cluster_namevalidate structure cascade" after XTTS has been completed in the targetdatabase and if it reports an ORA-1499 open the trace file and review if it hasentries like:
kdcchk: indexpoints to block 0x01c034f2 slot 0x1 chain length is 256
kdcchk: chain count wrong 0x01c034f2.1 chain is 1 index says 256
last entry 0x01c034f2.1 blockcount = 1
kdavls: kdcchk returns 3 when checking cluster dba 0x01c034a1 objn 90376
Then to repairthis inconsistency either:
1. rebuild thecluster index.
or
2. Install fix bug 17866999 and run dbms_repair.repair_cluster_index_keycount
If afterrepairing the inconsistency the "analyze cluster &cluster_namevalidate structure cascade" still reports issues then recreate theaffected cluster which involves recreating its tables.

1.7. 檢查 RMAN DEVICE TYPE DISK 是否配置 COMPRESSED

ORA-19837:invalid blocksize 0 in backup piece header
ORA-06512: at "SYS.X$DBMS_BACKUP_RESTORE", line 2338
ORA-06512: at line 40
配置壓縮會導致以上報錯,通過rman > show all; 檢查

1.8. SYS、SYSTEM 使用者是否有物件存在於業務表空間

如資料庫使用者 SYS、SYSTEM 在業務表空間上建立有物件,則這些物件不能通過 XTTS 遷移,需要在目標庫手工建立。

select table_name, owner, tablespace_name from dba_tables where tablespace_name not in('SYSTEM','SYSAUX') and owner in ('SYS','SYSTEM');

1.9. 檢查表空間自包含

在傳輸階段可能因為目標端資料檔案目錄所限制,需要將各個表空間拆分進行傳送,在匯入元資料階段,考慮到字包含特性,需要將所有的表空間彙總進行傳送。

檢查表空間時,只檢查業務表空間的自包含情況;系統表空間,臨時表空間,undo 表空間不在檢查列。

exec dbms_tts.transport_set_check('將所有需要傳輸的表空間名寫在這裡用逗號隔開' ,TRUE,True);


select * fromtransport_set_violations;

1.10. 檢查源端 Compatible 引數

source 端不可以是 windows P7,source 端的 COMPATIBLE.RDBMS 必須大於 10.2.0,且不大於目標端 COMPATIBLE.RDBMS

show parameter compatible

如果目標端資料庫版本是 11.2.0.3 或更低。那麼需要在目標端裝 11.2.0.4 並建立例項,然後用來進行備份集轉換。如果 11.2.0.4 中轉例項使用 ASM。那麼 ASM 版本也必須是 11.2.0.4,否則報錯 ORA-15295。

1.11. 如果啟用 Block Change Tracking 功能

該功能在源端資料量較大或者資料改變較大時啟用,需要在源端安裝補丁 Bug 16850197。

該補丁在以下版本 PSU中提供

  • 2.0.3.9
  • 2.0.4

如果源庫是在上述版本前,需要安裝個別補丁。

1.12. 檢查目標端的 DB_FILES 引數

在元資料匯入階段,如果目標端的 DB_FILES 引數小於源端的 DB_FILES 引數,會導致元資料匯入出錯,所以要確保目標端引數比源端大於或者等於。

Show parameter db_files

1.13. 確認生產庫的 recycle in 功能是關閉,並手工清空回收站

showparameter recyclebin;
 
NAME        TYPE                   VALUE
--------------------------------- --------
recyclebin  string                 on (這裡應該為 off)
關閉回收站
alter systemset recyclebin=’off’ scope=both;

1.14. 資料檔案狀態檢查

Select distinct status from v$datafile;
正常返回應為:ONLINE、SYSTEM。

2

XTTS 操作步驟

  1. 遷移步驟大致如下:
  2. 初始化階段,配置引數檔案
  3. 開啟塊追蹤特性
  4. 準備階段,進行資料全量同步
  5. 增量階段,進行資料檔案的增量同步
  6. 在停機同步前,在目標端新建 Owner 使用者,臨時表
  7. 傳輸階段,最後一次增量同步,並匯入元資料
  8. 將原庫中的其餘使用者匯入到目標端中
  9. 校驗資料檔案,檢查有無壞塊

3

RMAN 方式

注意:

使用 RMAN方式有一個嚴重的限制:同一批次的表空間傳輸到目標端後,都在同一個目錄下,即目標端的最終存放資料檔案的目錄大小要足夠存放一個批次中所有的表空間資料檔案。

源端和目標端需要掛載儲存用於存放所有資料檔案的映象檔案,建議使用 NFS 的方式將儲存遠端從源端掛載到目標端,減少備份傳送時間。

3.1. 初始化階段,配置引數檔案

3.1.1. 建立 Dest 端到 Source 端的 DBLINK

該步驟創立的 DBLINK用於在傳輸階段的元資料匯入時使用

ttslink=
  (DESCRIPTION =
    (ADDRESS =(PROTOCOL = TCP)(HOST = ip地址)(PORT = 1521))
   (CONNECT_DATA =
      (SERVER =DEDICATED)
     (SERVICE_NAME = 例項名)
    )
  )
create public database link ttslink connect to systemidentified by password using 'ttslink';

測試DBLINK的連通性

select * from v$version@ttslink;

來了

3.1.2. Source 端和 Dest 端建立目錄用於存放 XTTS指令碼

[oracle@source]$ pwd
/home/oracle/xtt
[oracle@source]$ unzip rman_xttconvert_v3.zip
Archive: rman_xttconvert_v3.zip
inflating: xtt.properties
inflating: xttcnvrtbkupdest.sql
inflating: xttdbopen.sql
inflating: xttdriver.pl
inflating: xttprep.tmpl
extracting: xttstartupnomount.sql

3.2. 開啟塊追蹤特性

SQL> alter database enable block change trackingusing file '+datadg';
 
Database altered.

按照文件所示,開啟這個特性後,使用 XTTS會觸發 BUG16850197,需要安裝個別補丁,但是目前該補丁只在 11.2.0.3.8 上有提供。

經過測試,其餘版本沒有觸發 bug。

注意:

RAC 環境下,需要將塊追蹤檔案存放在共享儲存上,否則會導致 DFS LOCKHANDLE,導致例項異常。

3.3. 準備階段,進行資料全量同步

3.3.1. 修改引數

源端平臺 ID 查詢

select * from v$transportable_platform

/home/oracle/xtt2/ xtt.properties
tablespaces=XTTS 需要大寫
platformid=13
dfcopydir=/home/oracle/xtt2/dfcopydir
backupformat=/home/oracle/xtt2/backup
stageondest=/home/oracle/xtt2/stageondest
storageondest=/oggtest/oggtest
backupondest=/home/oracle/xtt2/backupondest
parallel=2
rollparallel=2
 
mkdir /home/oracle/xtt2/dfcopydir
mkdir /home/oracle/xtt2/backup
mkdir /home/oracle/xtt2/stageondest
mkdir /home/oracle/xtt2/backupondest

附錄解釋:

tablespaces=               ----需要遷移的表空間
platformid=           ----源 OS 平臺 ID
dfcopydir=             ----源資料庫備份檔案存放目錄
backupformat=            ---源資料庫增備檔案存放目錄
stageondest=              ----目標據庫備份檔案存放目錄
storageondest=         ----目標據庫正式檔案存放目錄
backupondest=         ----目標據庫增備檔案存放目錄
parallel=                ----備份,轉化的並行度
rollparallel=             ----增備的並行度
getfileparallel=            ---- 使用 dbms_file_transfer 方式的並行度

3.3.2. 將 XTTS目錄 Copy 傳送到目標端

scp -r /home/oracle/xtt2 sprac2:/home/oracle

3.3.3. 設定 TMPDIR

If TMPDIR is not set, output files are created in andinput files are expected to be in /tmp.
[oracle@source]$ export TMPDIR=/home/oracle/xtt2
[oracle@dest]$ export TMPDIR=/home/oracle/xtt2

3.3.4. 源端 Prepare

perl /home/oracle/xtt2/xttdriver.pl -p

初始化之後產生 xttplan.txt rmanconvert.cmd

xttplan.txt 記錄了當前 SCN,也就是下次需要增量的開始 SCN

rmanconvert.cmd 記錄了檔案轉換的名字

3.3.5. 源端生成的資料檔案 Copy 到目標端

scp dfcopydir/XTTS_10.tf sprac2:/home/oracle/xtt2/stageondest

3.3.6. 轉換資料檔案 Copy 的 Endian 模式

export TMPDIR=/home/oracle/xtt2
scp rmanconvert.cmd sprac2:/home/oracle/xtt2
perl xttdriver.pl -c

轉換成功之後會生成 xttnewdatafiles.txt

整個過程,存放在 stageondest 的 copy,將會轉換寫入到 storageondest 引數設定的最終存放點。轉換完成後,copy 就可以刪除了。

3.4. 增量階段,進行資料檔案的增量同步

3.4.1. 源端建立增量備份

[oracle@sprac1 xtt2]$ perl xttdriver.pl -i

第一次增量備份之後產生的配置檔案為 tsbkupmap.txt incrbackups.txt,這兩個為增量與資料檔案對應關係配置,在做增量恢復時候需要用到。增量備份檔案放在了backupformat 指定位置。

3.4.2. 傳輸增量備份到目標端

[oracle@sprac1 xtt2]$ scp `cat incrbackups.txt`sprac2:/home/oracle/xtt2/stageondest

3.4.3. 目標端應用增量備份

[oracle@sprac1 xtt2]$ scp xttplan.txt tsbkupmap.txtsprac2:/home/oracle/xtt2
[oracle@sprac2 xtt2]$ perl xttdriver.pl -r 目標端執行

3.4.4. 源端為之後的增量備份確定新的 SCN

[oracle@sprac1 xtt2]$ perl xttdriver.pl -s

該步驟會將 -i 時生成的 xttplan.txt.new 改名為 xttplan.txt,並將原來的 xttplan.txt 備份。

3.4.5. 重複前滾操作

重複操作步驟 3.4.1 - 3.4.4,恢復增量資料。

3.5. 停機同步前,在目標端新建 Owner 使用者

3.5.1. 建立臨時表空間

依照原庫上的臨時表空間大小,在目標端建立同名臨時表空間。

3.5.2. 建立 Owner 使用者

在源端執行下列語句

select 'create user '||name||' identified by values'''||password||''' default tablespace users temporary tablespace temp;' fromuser$ where name in (‘輸入所需要建立的使用者名稱’)

將生成的語句,在目標端執行,建立使用者。

3.6. 傳輸階段,最後一次增量同步,並匯入元資料

3.6.1. 將原庫表空間置為只讀狀態

SQL> alter tablespace xtts read only;

3.6.2. 開始最後做增量備份

重複步驟 3.4.1 - 3.4.3,做最後的增量恢復。

3.6.3. 自定義 Role 處理

---自定義角色,Role(R_INIT_USER)
select GRANTED_ROLE from dba_role_privs whereGRANTEE='XTTS';
create role R_INIT_USER;
 
select 'grant '||PRIVILEGE||' to '||GRANTEE||';' fromdba_sys_privs where grantee='R_INIT_USER';

3.6.4. 自定義 Profile 處理

---執行源端保留的 profile 資訊
set long 1000000
SET PAGESIZE 3000
set lines 200
SET HEADING OFF
SET VERIFY OFF
SET FEEDBACK OFF
set echo on
set timing off
set wrap On
SET LONGCHUNKSIZE 400
spool create_profile.sql
with profile as(
select distinct profile from dba_profiles)
select dbms_metadata.get_ddl('PROFILE',PROFILE) fromprofile;

3.6.5. 臨時表處理

---建立臨時表
@temp_table.sql
 
源端生產庫採集指令碼,目標端執行
set long 1000000
SET PAGESIZE 3000
set lines 200
SET HEADING OFF
SET VERIFY OFF
SET FEEDBACK OFF
set echo on
set timing off
set wrap On
SET LONGCHUNKSIZE 400
select dbms_metadata.get_ddl('TABLE',TABLE_NAME,OWNER)
from DBA_TABLES
where TEMPORARY='Y' and owner in ();

3.6.6. 目標端匯入元資料

3.6.6.1. 一次元資料匯入

[oracle@sprac2 xtt2]$ perl xttdriver.pl -e
這個步驟會產生一個匯入樣本指令碼 xttplugin.txt,需要修改如下:
在一次元資料匯入階段,不能開啟並行,統計資訊匯入階段,可以將會話殺除終止
 
cat xttplugin.txt
impdp directory=DATA_PUMP_DIRlogfile=tts_imp.log 
network_link=xttlinktransport_full_check=no 
transport_tablespaces=XTTS 
transport_datafiles='/oggtest/oggtest/XTTS_10.xtf' exclude=statistics

將統計資訊排除,後面開啟並行收集統計資訊。

3.6.6.2. 二次元資料匯入

將第一次沒有匯入的過程,檢視,包,觸發器匯入。

impdp directory=DATA_PUMP_DIR logfile=tts_imp_2.lognetwork_link=xttlink schemas= 'XTTS' content=metadata_only  exclude=index,table,constraint,statistics

3.6.6.3. 重新在目標端蒐集統計資訊

execDBMS_STATS.GATHER_SCHEMA_STATS(ownname=>'XTTS',ESTIMATE_PERCENT=>5,method_opt=>'forall columns size 1',cascade=>true,force=>true,degree=>12);

3.7. 將原庫中的其餘使用者匯入到目標端中,許可權處理

使用如下指令碼重新賦權
SET SERVEROUTPUT ON
SET LINESIZE 1000
SET FEEDBACK OFF
SET TRIMSPOOL ON
set long 999999            
SET PAGESIZE 1000
spool grant_role_priv.sql
select 'grant '||GRANTED_ROLE||' to '||grantee||';'from dba_role_privs where grantee in() and admin_option='NO'
union
select 'grant '||GRANTED_ROLE||' to '||grantee||' withadmin option;' from dba_role_privs where grantee in() and admin_option='YES';
spool off
SET SERVEROUTPUT ON
SET LINESIZE 1000
SET FEEDBACK OFF
set long 999999 
SET TRIMSPOOL ON
SET PAGESIZE 1000
spool grant_sys_priv.sql
select 'grant '||privilege||' to '||grantee||';' fromdba_sys_privs where grantee in() and admin_option='NO'
union
select 'grant '||privilege||' to '||grantee||' withadmin option;' from dba_sys_privs where grantee in()and admin_option='YES';
spool off
SET SERVEROUTPUT ON
SET LINESIZE 1000
SET FEEDBACK OFF
SET TRIMSPOOL ON
set long 999999            
SET PAGESIZE 1000
spool grant_tab_privs.sql
select 'grant ' || privilege || ' on ' || owner || '.'|| table_name || ' to ' || grantee || ';' from dba_tab_privs where grantee in()grantable='NO'
union
select 'grant ' || privilege || ' on ' || owner || '.'|| table_name || ' to ' || grantee || ' with grant option;' from dba_tab_privswhere grantee in() and grantable='YES'; 
spool off

3.8. 校驗資料檔案,檢查有無壞塊

RMAN> validate tablespace xtts check logical;

3.9. 編譯無效物件,進行資料對比。

@$ORACLE_HOME/rdbms/admin/utlrp.sq

相關閱讀:

1. 我們都被騙了,所有的跨平臺遷移都可以通過XTTS實現

2. XTTS(Cross Platform Incremental Backup) 的測試例子

3. 遷移神技 XTTS-恩墨小王子再戰32TB跨平臺U2L

資源下載

關注公眾號:資料和雲(OraNews)回覆關鍵字獲取

‘2017DTC’,2017DTC大會PPT

‘DBALIFE’,“DBA的一天”海報

‘DBA04’,DBA手記4經典篇章電子書

‘RACV1’, RAC系列課程視訊及ppt

‘122ARCH’,Oracle 12.2體系結構圖

‘2017OOW’,Oracle OpenWorld資料

‘PRELECTION’,大講堂講師課程資料