OCI BaseDBの19cから23cへのデータ移行(DataPump編) [OCI]

1.はじめに


 OCI上で19cから23cの環境へデータを移行する検証をしたので備忘までメモを残しておく。具体的なシナリオとしては、19cの環境でexpdpでエクスポートしたダンプファイルを、新規に23cの環境にimpdpでインポートする。ダンプファイルはノーマル(平文)と、TDEの暗号鍵で暗号化したもの(encryption_mode = transparent)の2種類を用いる。後者のインポートのために、19c環境のTDEのマスタ暗号鍵を23c環境へインポートすることも行う。

20231124_expimp23cTestEnv2.jpg

 手順の概要は以下の通り。

2.環境準備


・ソースDB


 データ移行の対象となる表の状態確認をする。EMP表100万件、DEPT表10万件、セグメントサイズは合計77MB程度である。
[oracle@oradbvm1 ~]$ sqlplus scott/XXX@oradb
SQL> select count(*) from emp;
  COUNT(*)
----------
   1000000

SQL> select count(*) from dept;
  COUNT(*)
----------
    100000
    
SQL>  select segment_name, bytes/1024/1024 mb from user_segments;
   SEGMENT_NAME    MB
_______________ _____
DEPT                5 ★
EMP                72 ★
IDX1_EMP           23
PK_DEPT             2
PK_EMP             16

SQL>

 Base Database Serviceでは/u01配下に150GB程度の領域がある。この配下にディレクトリオブジェクトを作成する。
[oracle@oradbvm1 ~]$ df -h
Filesystem                      Size  Used Avail Use% Mounted on
・・・
/dev/sdg                        196G   42G  145G  23% /u01 ★

[oracle@oradbvm1 ~]$ mkdir -p /u01/app/oracle/dump/scott
[oracle@oradbvm1 ~]$ chmod 777 /u01/app/oracle/dump/scott
[oracle@oradbvm1 ~]$ ls -ld /u01/app/oracle/dump/scott
drwxrwxrwx 2 oracle oinstall 4096 Nov 21 06:11 /u01/app/oracle/dump/scott

[oracle@oradbvm1 ~]$ sqlplus sys/XXX@oradb as sysdba
SQL> CREATE DIRECTORY expdir AS '/u01/app/oracle/dump/scott';
Directory created.
SQL> grant read, write on directory expdir to scott;
Grant succeeded.

・ターゲットDB


 OCIコンソールからソースDBと同じセグメントに新規インスタンスを作成する。この際、DBバージョンは23c、エディションはEE HPを選択する(詳細な手順については[1]を参照)。プロビジョニング完了後、以下設定を行う。
  • OSのoracleユーザのパスワードを設定する(sshのファイル転送のため)
  • tnsnames.oraにPDBへの接続エントリを追加する
  • ソースDBと同じスキーマ(scott)を作成、必要な権限を付与する

 ディレクトリオブジェクトの作成はソースと同じ手順を実施する。表はソース側のEMP・DEPT表のDDL(参考[2]参照)を実行する。

3.エクスポート


・(ソースDB)マスタ暗号鍵のエクスポート


 ソースDBのPDBの管理者で接続し、以下のようにマスタ暗号鍵をエクスポートする。
[oracle@oradbvm1 ~]$ sqlplus sys/XXX@oradb as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Wed Nov 22 09:21:03 2023
Version 19.20.0.0.0

Copyright (c) 1982, 2022, Oracle.  All rights reserved.

Connected to:
Oracle Database 19c EE High Perf Release 19.0.0.0.0 - Production
Version 19.20.0.0.0

SQL> administer key management export encryption keys with secret "XXX" to '/home/oracle/ewallet_oradbmv1_oradb.p12' force keystore identified by "XXX";

keystore altered.

SQL> !ls -l
total 6108
-rw-r--r-- 1 oracle asmadmin    2612 Nov 22 09:21 ewallet_oradbmv1_oradb.p12 ★
SQL> 

・(ソースDB)データのエクスポート


はじめにノーマル(平文)のエクスポートを実行する。平文でエクスポートされたことを示すORA-39173が出力されるが、処理は正常終了する。
[oracle@oradbvm1 scott]$ expdp scott/XXX@oradb schemas=scott directory=expdir dumpfile=scott.dmp logfile=expdpscott.log content=data_only

Export: Release 19.0.0.0.0 - Production on Sun Nov 12 16:20:46 2023
Version 19.20.0.0.0

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 19c EE High Perf Release 19.0.0.0.0 - Production
FLASHBACK automatically enabled to preserve database integrity.
Starting "SCOTT"."SYS_EXPORT_SCHEMA_01":  scott/********@oradb schemas=scott directory=expdir dumpfile=scott.dmp logfile=expdpscott.log content=data_only
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . exported "SCOTT"."EMP"                               55.15 MB 1000000 rows
. . exported "SCOTT"."DEPT"                              3.619 MB  100000 rows ★=~59MB
ORA-39173: Encrypted data has been stored unencrypted in dump file set. ★暗号化されてない
Master table "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_SCHEMA_01 is:
  /u01/app/oracle/dump/scott/scott.dmp
Job "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully completed at Sun Nov 12 16:20:51 2023 elapsed 0 00:00:04 ★正常終了

 続いて暗号化エクスポートを実行する。encryption=data_onlyと指定する。encryption_modeは指定していないが、TDEのウォレットがオープン状態なので、デフォルトでtransparent、つまりTDEのマスタ暗号鍵を利用して暗号化を行うこととなる。ORA-39173は発生しないので、暗号化されていることがわかる。
[oracle@oradbvm1 scott]$ expdp scott/XXX@oradb schemas=scott directory=expdir dumpfile=scotte.dmp logfile=expdpscotte.log content=data_only encryption=data_only

Export: Release 19.0.0.0.0 - Production on Sun Nov 12 16:52:47 2023
Version 19.20.0.0.0

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 19c EE High Perf Release 19.0.0.0.0 - Production
FLASHBACK automatically enabled to preserve database integrity.
Starting "SCOTT"."SYS_EXPORT_SCHEMA_01":  scott/********@oradb schemas=scott directory=expdir dumpfile=scotte.dmp logfile=expdpscotte.log content=data_only encryption=data_only
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . exported "SCOTT"."EMP"                               55.15 MB 1000000 rows
. . exported "SCOTT"."DEPT"                              3.619 MB  100000 rows ★=~59MB
Master table "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully loaded/unloaded
******************************************************************************
Dump file set for SCOTT.SYS_EXPORT_SCHEMA_01 is:
  /u01/app/oracle/dump/scott/scotte.dmp
Job "SCOTT"."SYS_EXPORT_SCHEMA_01" successfully completed at Sun Nov 12 16:52:52 2023 elapsed 0 00:00:04 ★正常終了

ディレクトリオブジェクトのファイルを確認すると、以下のように2つのダンプファイルが作成されていることがわかる。暗号化してもファイルサイズに変化はない。
[oracle@oradbvm1 scott]$ ll
total 120936
-rw-r--r-- 1 oracle asmadmin     1056 Nov 12 16:52 expdpscotte.log
-rw-r--r-- 1 oracle asmadmin     1104 Nov 12 16:48 expdpscott.log
-rw-r----- 1 oracle asmadmin 61911040 Nov 12 16:48 scott.dmp ★ノーマル(平文)
-rw-r----- 1 oracle asmadmin 61911040 Nov 12 16:52 scotte.dmp ★暗号化
[oracle@oradbvm1 scott]$

4.ダンプファイル転送


・マスタ暗号鍵の転送


 ソースDBからマスタ暗号鍵をscp転送する
[oracle@oradbvm1 ~]$ cd ~/
[oracle@oradbvm1 ~]$ scp ewallet_oradbmv1_oradb.p12 10.0.1.12:~/
oracle@10.0.1.12's password:
ewallet_oradbmv1_oradb.p12                    100% 2612     5.4MB/s   00:00

・ダンプファイルの転送


 ソースDBからダンプファイルをscp転送する。
[oracle@oradbvm1 ~]$ cd /u01/app/oracle/dump/scott
[oracle@oradbvm1 scott]$ scp *.dmp 10.0.1.12:/u01/app/oracle/dump/scott
oracle@10.0.1.12's password:
scott.dmp                                     100%   59MB 100.0MB/s   00:00
scotte.dmp                                    100%   59MB  98.0MB/s   00:00
[oracle@oradbvm1 scott]$

 ターゲットDBにて、ダンプファイルを確認する。
[oracle@oradbvm2 scott]$ ll
-rw-r----- 1 oracle oinstall  61911040 Nov 21 07:12 scott.dmp
-rw-r----- 1 oracle oinstall  61911040 Nov 21 07:12 scotte.dmp

5.インポート


・(ターゲットDB)データのインポート(ノーマル)


 ターゲットDBにて、インポートを行う。EMP表からDEPT表への参照整合性制約を無効にしてからインポートを行うと、問題なくインポートできる。
[oracle@oradbvm2 ~]$ sqlplus scott/XXX@oradb
SQL> alter table emp disable constraint fk_deptno;

[oracle@oradbvm2 ~]$ impdp scott/XXX@oradb schemas=scott directory=expdir dumpfile=scott.dmp logfile=impdpscott.log content=data_only table_exists_action=truncate

Import: Release 23.0.0.0.0 - Production on Fri Nov 24 09:55:25 2023
Version 23.3.0.23.09

Copyright (c) 1982, 2023, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 23c EE High Perf Release 23.0.0.0.0 - Production
Master table "SCOTT"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SCOTT"."SYS_IMPORT_SCHEMA_01":  scott/********@oradb schemas=scott directory=expdir dumpfile=scott.dmp logfile=impdpscott.log content=data_only table_exists_action=truncate
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."EMP"                               55.15 MB 1000000 rows
. . imported "SCOTT"."DEPT"                              3.619 MB  100000 rows
Job "SCOTT"."SYS_IMPORT_SCHEMA_01" successfully completed at Fri Nov 24 09:56:09 2023 elapsed 0 00:00:35 ★成功

 ここで試しに、暗号化したダンプファイルをインポートしようとすると、複合化に必要なマスタ暗号鍵が見つからないため、ORA-28362のエラーでインポートすることができない。このため、ソースDBのPDBのマスタ暗号鍵のインポートが必要となる。
SQL> !impdp scott/XXX@oradb schemas=scott directory=expdir dumpfile=scotte.dmp logfile=impdpscotte.log content=data_only table_exists_action=truncate

Import: Release 23.0.0.0.0 - Production on Fri Nov 24 09:56:52 2023
Version 23.3.0.23.09

Copyright (c) 1982, 2023, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 23c EE High Perf Release 23.0.0.0.0 - Production
ORA-39002: invalid operation
ORA-39189: unable to decrypt dump file set
ORA-28362: master key not found ★エラーとなる

・(ターゲットDB)マスタ暗号鍵のインポート


 ターゲットDBにて、ソースDBのPDBのマスタ暗号鍵をインポートする。インポートはコンテナDBに接続して実行する(PDBではインポートできない)。v$encryption_keysでインポートされていることを確認する。
[oracle@oradbvm2 ~]$ ll
total 12
-rw-r--r-- 1 oracle oinstall 2612 Nov 22 09:22 ewallet_oradbmv1_oradb.p12 ★

[oracle@oradbvm2 ~]$ sqlplus / as sysdba

SQL*Plus: Release 23.0.0.0.0 - Production on Wed Nov 22 09:26:27 2023
Version 23.3.0.23.09

Copyright (c) 1982, 2023, Oracle.  All rights reserved.


Connected to:
Oracle Database 23c EE High Perf Release 23.0.0.0.0 - Production
Version 23.3.0.23.09

SQL> administer key management import keys with secret "XXX" from '/home/oracle/ewallet_oradbmv1_oradb.p12' force keystore identified by "XXX" with backup;

keystore altered.

SQL> select key_id, creation_time, creator_dbname, activating_pdbname, con_id from v$encryption_keys;

KEY_ID                                                  CREATION_TIME                             CREATOR_DBNAME     ACTIVATING_PDBNAME       CON_ID
_______________________________________________________ _________________________________________ __________________ _____________________ _________
AXTVRRDA8U+uv9W09+9PuAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAA    19-APR-23 10.54.29.632286000 PM GMT       coradb_oradbvm1    ORADB                         3★PDBのマスタ暗号鍵
ARq4NDRE80+SvwxzekrKJbAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA    23-NOV-23 05.20.41.789397000 PM -07:00    coradb_oradbvm2    CDB$ROOT                      1
AYpmGS1/Gk9Ev1QZZYcswd4AAAAAAAAAAAAAAAAAAAAAAAAAAAAA    23-NOV-23 05.27.18.895760000 PM -07:00    coradb_oradbvm2    ORADB                         3
SQL>

・(ターゲットDB)データのインポート(暗号化)


 暗号化されたダンプファイルをインポートする。正常にインポートされたら、参照整合性制約を有効にする。
[oracle@oradbvm2 ~]$ impdp scott/XXX@oradb schemas=scott directory=expdir dumpfile=scotte.dmp logfile=impdpscotte.log content=data_only table_exists_action=truncate

Import: Release 23.0.0.0.0 - Production on Fri Nov 24 10:15:06 2023
Version 23.3.0.23.09

Copyright (c) 1982, 2023, Oracle and/or its affiliates.  All rights reserved.

Connected to: Oracle Database 23c EE High Perf Release 23.0.0.0.0 - Production
Master table "SCOTT"."SYS_IMPORT_SCHEMA_01" successfully loaded/unloaded
Starting "SCOTT"."SYS_IMPORT_SCHEMA_01":  scott/********@oradb schemas=scott directory=expdir dumpfile=scotte.dmp logfile=impdpscotte.log content=data_only table_exists_action=truncate
Processing object type SCHEMA_EXPORT/TABLE/TABLE_DATA
. . imported "SCOTT"."EMP"                               55.15 MB 1000000 rows
. . imported "SCOTT"."DEPT"                              3.619 MB  100000 rows
Job "SCOTT"."SYS_IMPORT_SCHEMA_01" successfully completed at Fri Nov 24 10:15:32 2023 elapsed 0 00:00:24 ★成功

[oracle@oradbvm2 ~]$ sqlplus scott/XXX@oradb
SQL> alter table emp enable constraint fk_deptno;

6.まとめ


 本稿では、OCI上で19cの環境からデータをexpdp/impdpで23cの環境へ移行する手順について記載した。
 実際問題、今回のケースのようにOCIの内部でダンプの暗号化までする必要はないだろう。しかし、実際はファイルサイズが大きくなると、オブジェクトストレージや媒体を介した転送が必要なケースもあり、そのような場合に暗号化が必要になることが考えられる。暗号化の方法にはパスワードを設定する方法(encryption_mode=password)もあるが、インポートの際に毎回パスワードを設定するのも煩わしい。OCIでEE High Performance以上を使っているなら、advanced securityのダンプファイルの暗号化機能が利用できるため、元のPDBのマスタ暗号鍵をターゲットとなるDBにインポートすることで、透過的に暗号化ダンプファイルのエクスポート・インポートが可能となる。

参考


[1]ベースデータベース コンソールを使用したDBシステムの作成
https://docs.public.oneportal.content.oci.oraclecloud.com/ja-jp/iaas/dbcs/doc/create-db-system-using-console.html#DBSCB-GUID-624EFC8F-CF53-4A88-B9AB-2F45F2B9A546

[2]検証用DDL
drop table emp cascade constraints;
drop table dept cascade constraints;
create table dept(  
  deptno     number(7,0),  
  dname      varchar2(14),  
  loc        varchar2(13),  
  constraint pk_dept primary key (deptno)  
);
create table emp(  
  empno    number(7,0),  
  ename    varchar2(10),  
  job      varchar2(9),  
  mgr      number(7,0),  
  hiredate date,  
  sal      number(7,2),  
  comm     number(7,2),  
  deptno   number(7,0),  
  constraint pk_emp primary key (empno),  
  constraint fk_deptno foreign key (deptno) references dept (deptno)  
);
create index idx1_emp on emp ( deptno );