oracle 12c 多租戶 pdb 恢復(單個pdb資料檔案、非系統pdb表空間、整個pdb資料庫)

張衝andy發表於2017-05-11
環境:資料庫版本 Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

實驗準備:
1.-- 資料庫歸檔模式
SQL> select name,log_mode from v$database;
NAME  LOG_MODE
--------- ------------
ANDYCDB   ARCHIVELOG   > 資料庫歸檔模式

2.-- 登入 PDB 中建立實驗表空間 與 使用者
[oracle@12c ~]$ sqlplus sys/oracle@10.219.24.16:1521/pdb01 as sysdba
SQL> 
create tablespace bbb logging  datafile '/home/oracle/app/oracle/oradata/andycdb/pdb01/bbb.dbf' 
size 1m  autoextend on  next 1m maxsize 2m  extent management local;
Tablespace created.
SQL>alter tablespace bbb add  datafile '/home/oracle/app/oracle/oradata/andycdb/pdb01/bbb02.dbf'size 1M  
autoextend on  next 1m maxsize 2m  ;
Tablespace altered.

create user andy identified by andy default tablespace users;
create user bbb identified by bbb default tablespace bbb;
grant dba to andy;
grant dba to bbb;

3.-- 備份整個CDB及其下面的所有PDB
[oracle@12c ~]$ mkdir /home/oracle/rmanback/
[oracle@12c01 ~]$ rman target /

run{
alter system archive log current;
allocate channel ch1 type disk;
allocate channel ch2 type disk;
backup incremental level 0 database plus archivelog delete input
format '/home/oracle/rmanback/db_%d_%U'
tag=db_inc_0;
release channel ch1;
release channel ch2;
}
Finished backup at 12-MAY-17

一、  恢復 PDB 資料檔案

資料庫在open的時候,會對當前的資料的所有資料檔案進行檢查。 
對於system,sysaux和undo表空間的資料檔案,如果有問題,資料庫無法open。如果是PDB中某個普通的資料檔案出現丟失,我們可以先用offline方式跳過,然後再打資料庫,稍後再對資料檔案做恢復: 
[oracle@12c ~]$ sqlplus sys/oracle@10.219.24.16:1521/pdb01 as sysdba
SQL> show con_name
CON_NAME
------------------------------
PDB01
SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/home/oracle/app/oracle/oradata/andycdb/pdb01/system01.dbf
/home/oracle/app/oracle/oradata/andycdb/pdb01/sysaux01.dbf
/home/oracle/app/oracle/oradata/andycdb/pdb01/undotbs01.dbf
/home/oracle/app/oracle/oradata/andycdb/pdb01/users01.dbf
/home/oracle/app/oracle/oradata/andycdb/pdb01/bbb.dbf
/home/oracle/app/oracle/oradata/andycdb/pdb01/bbb02.dbf
6 rows selected.

摸擬pdb資料檔案 user 刪除
[oracle@12c01 ~]$ cd /home/oracle/app/oracle/oradata/andycdb/pdb01/
[oracle@12c01 ~]$ mv users01.dbf users01.dbf.bak
-- 模擬業務
C:\Users\andy>sqlplus andy/andy@10.219.24.16:1521/pdb01
SQL> create table andy as select * from all_objects;
create table andy as select * from all_objects                                  *
ERROR at line 1:
ORA-01116: error in opening database file 12
ORA-01110: data file 12:
'/home/oracle/app/oracle/oradata/andycdb/pdb01/users01.dbf'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
SQL> alter system checkpoint;
System altered.
-- alert_andycdb.log 日誌檢視報錯:
ORA-01110: data file 12: '/home/oracle/app/oracle/oradata/andycdb/pdb01/users01.dbf'
ORA-01565: error in identifying file '/home/oracle/app/oracle/oradata/andycdb/pdb01/users01.dbf'
-- 進入 PDB 檢視資料檔案狀態
SQL> 
col name for a60;
select file#,status,name from v$datafile;
     FILE# STATUS  NAME
---------- ------- ------------------------------------------------------------
9 SYSTEM  /home/oracle/app/oracle/oradata/andycdb/pdb01/system01.dbf
10 ONLINE  /home/oracle/app/oracle/oradata/andycdb/pdb01/sysaux01.dbf
11 ONLINE  /home/oracle/app/oracle/oradata/andycdb/pdb01/undotbs01.dbf
12 ONLINE  /home/oracle/app/oracle/oradata/andycdb/pdb01/users01.dbf
13 ONLINE  /home/oracle/app/oracle/oradata/andycdb/pdb01/bbb.dbf
14 ONLINE  /home/oracle/app/oracle/oradata/andycdb/pdb01/bbb02.dbf
6 rows selected.

說明: 12c r1版本,如果pdb資料檔案損壞,會造成 cdb 一起當機。 12c r2版本 pdb資料檔案損壞,cdb 與 pdb 都沒有當機。
-- 使用 rman 恢復
[oracle@12c pdb01]$ rman target sys/oracle@10.219.24.16:1521/pdb01 
RMAN> recover datafile 12;
RMAN-06094: datafile 12 must be restored 
RMAN> alter database datafile 12 offline;
RMAN> alter database datafile 12 offline;
RMAN> restore datafile 12; 
RMAN> recover datafile 12; 
Finished recover at 12-MAY-17
然後對資料檔案進行online處理 
RMAN> alter database datafile 12 online; 
Statement processed
-- 模擬業務   成功!
SQL> create table andy as select * from all_objects;
Table created.

二、 恢復 PDB 非系統表空間

實驗: 模擬 PDB 非系統表空間 bbb 損壞。
[oracle@12c ~]$ sqlplus sys/oracle@10.219.24.16:1521/pdb01 as sysdba
--查詢表空間情況
SQL> select tablespace_name,status from dba_tablespaces;
TABLESPACE_NAME       STATUS
------------------------------ ---------
SYSTEM       ONLINE
SYSAUX       ONLINE
UNDOTBS1       ONLINE
TEMP       ONLINE
USERS       ONLINE
BBB       ONLINE
SQL>
col tablespace_name for a15
col file_name for a55
SELECT tablespace_name, file_id, file_name, round(bytes / (1024 * 1024), 0) total_space
FROM dba_data_files where tablespace_name='BBB' ORDER BY tablespace_name;
TABLESPACE_NAME    FILE_ID FILE_NAME                                               TOTAL_SPACE
--------------- ---------- ------------------------------------------------------- -----------
BBB                     13 /home/oracle/app/oracle/oradata/andycdb/pdb01/bbb.dbf             1
BBB                     14 /home/oracle/app/oracle/oradata/andycdb/pdb01/bbb02.dbf           1
--模擬表空間損壞
[oracle@12c pdb01]$ pwd
/home/oracle/app/oracle/oradata/andycdb/pdb01
[oracle@12c pdb01]$ ll bbb*
-rw-r-----. 1 oracle oinstall 1056768 May 12 06:42 bbb02.dbf
-rw-r-----. 1 oracle oinstall 1056768 May 12 06:42 bbb.dbf
[oracle@12c pdb01]$ rm -rf bbb*
--模擬業務
C:\Users\andy>sqlplus bbb/bbb@10.219.24.16:1521/pdb01
SQL> create table andy as select * from all_objects;
ERROR at line 1:
ORA-01116: error in opening database file 13
ORA-01110: data file 13:
'/home/oracle/app/oracle/oradata/andycdb/pdb01/bbb.dbf'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
-- alert_andycdb.log 日誌檢視報錯:
ORA-01110: data file 13: '/home/oracle/app/oracle/oradata/andycdb/pdb01/bbb.dbf'
ORA-01565: error in identifying file '/home/oracle/app/oracle/oradata/andycdb/pdb01/bbb.dbf'
ORA-27037: unable to obtain file status
ORA-01110: data file 14: '/home/oracle/app/oracle/oradata/andycdb/pdb01/bbb02.dbf'
SQL> alter system checkpoint;
System altered.
SQL> select tablespace_name,status from dba_tablespaces;
TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
SYSAUX                         ONLINE
UNDOTBS1                       ONLINE
TEMP                           ONLINE
USERS                          ONLINE
BBB                            ONLINE
-- 使用 rman 恢復
[oracle@12c pdb01]$ rman target sys/oracle@10.219.24.16:1521/pdb01
RMAN> alter tablespace bbb offline;
ORA-27041: unable to open file
RMAN> alter tablespace bbb offline immediate;
Statement processed
RMAN> recover tablespace bbb;
RMAN-06094: datafile 13 must be restored
RMAN> restore tablespace bbb;
Finished restore at 12-MAY-17
RMAN> recover tablespace bbb;
Finished recover at 12-MAY-17
RMAN> alter tablespace bbb online;
Statement processed
-- 模擬業務 成功!
SQL> create table andy as select * from all_objects where rownum>2;
Table created.

三、  單個PDB資料庫恢復
恢復單個PDB的前提是CDB已經能夠正常啟動,在CDB啟動的情況下在RMAN中採用restore pluggable database pdb名稱指定單個PDB資料庫進行恢復,如下
SQL> show con_name
CON_NAME
------------------------------
PDB01
SQL> select name from v$datafile;
NAME
--------------------------------------------------------------------------------
/home/oracle/app/oracle/oradata/ANDYCDB/4ECF8621E3DA38EEE0531019640AA598/datafil
e/o1_mf_system_dk5wmv0s_.dbf
/home/oracle/app/oracle/oradata/ANDYCDB/4ECF8621E3DA38EEE0531019640AA598/datafil
e/o1_mf_sysaux_dk5wmv11_.dbf
/home/oracle/app/oracle/oradata/ANDYCDB/4ECF8621E3DA38EEE0531019640AA598/datafil
e/o1_mf_users_dk6wky42_.dbf
/home/oracle/app/oracle/oradata/ANDYCDB/4ECF8621E3DA38EEE0531019640AA598/datafil
e/o1_mf_undo_1_dk5wmv13_.dbf
-- 模擬整個 pdb 損壞
[oracle@12c01 datafile]$ cd /home/oracle/app/oracle/oradata/ANDYCDB/4ECF8621E3DA38EEE0531019640AA598
[oracle@12c01 4ECF8621E3DA38EEE0531019640AA598]$ rm -rf datafile/
SQL> alter system checkpoint;
System altered.
SQL> show pdbs
    CON_ID CON_NAME  OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED  READ ONLY  NO
3 PDB01  MOUNTED
RMAN>restore pluggable database pdb01; 
Finished restore at 11-MAY-17
RMAN>recover pluggable database pdb01; 
Finished recover at 11-MAY-17
[oracle@12c01 ~]$ sqlplus / as sysdba
SQL> alter pluggable database pdb01 open;
Pluggable database altered.

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

相關文章