使用RMAN对PDB执行按时间点恢复

对PDB执行按时间点恢复类似于执行数据库按时间点恢复。当对一个或多个PDB恢复到指定时间点时,CDB中的其它PDB不受影响。在恢复之后,PDB原来的保留的旧备份仍然有效可以在出现介质恢复时使用,不需要创建新的备份。当对使用共享UNDO的CDB中的一个或多个PDB执行数据库按时间点恢复时,对于包含被恢复PDB的CDB的root与CDB seed(PDB$SEES)需要有备份。从Oracle 12.2开始,如果compatible参数被设置为12.2,那么可以跨PDB闪回操作或PDB按时间点恢复来对CDB执行闪回数据库操作。在DG环境中,对于备库将跟随主库PDB会被恢复到指定的时间点,你可以闪回整个备库,恢复PDB或对PDB执行闪回。

对PDB执行按时间点恢复的操作步骤如下:
1.登录数据库记录当前SCN号,然后将表t1中的数据删除。

SQL> conn jy/jy@jypdb
Connected.
SQL> SELECT CURRENT_SCN   FROM V$DATABASE;

CURRENT_SCN
-----------
    6255735

SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

Session altered.

SQL> select sysdate from dual;

SYSDATE
-------------------
2017-12-20 16:52:31

SQL> select count(*) from t1;

  COUNT(*)
----------
        39

SQL> truncate table t1;

Table truncated.

SQL> select count(*) from t1;

  COUNT(*)
----------
         0

2.如果使用时间表达式来代替目标SCN,那么在调用RMAN之前设置时间格式环境变量

[oracle@jytest1 ~]$ export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'

3.使用RMAN连接到root容器

[oracle@jytest1 ~]$ rman target/ catalog rco/xxzx7817600@jypdb_173

Recovery Manager: Release 12.2.0.1.0 - Production on Wed Dec 20 16:53:26 2017

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

connected to target database: JY (DBID=979425723)
connected to recovery catalog database

4.将要执行恢复的PDB关闭,其它的PDB与CDB仍然处于open状态

RMAN> alter pluggable database jypdb close immediate;

starting full resync of recovery catalog
full resync complete
Statement processed
starting full resync of recovery catalog
full resync complete

5.使用RUN块来执行以下操作
a.对于数据库按时间点鶋,使用set until来指定恢复的目标时间,scn或日志序列号,或者使用set to来指定还原点。如果指定时间那么使用环境变量nls_lang与nls_date_format中所指定的日期格式。

b.如果RMAN没有配置自动通道,那么需要手动分配磁盘与磁带通道。

c.还原与恢复CDB

下面的命令将PDB(jypdb)恢复到SCN=6255735所在的状态

RMAN> run
2> {
3>    set until scn 6255735;
4>    restore pluggable database jypdb;
5>    recover pluggable database jypdb;
6> }

executing command: SET until clause

Starting restore at 2017-12-20 17:00:38
using channel ORA_DISK_1

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00010 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/system.271.962209649
channel ORA_DISK_1: restoring datafile 00011 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/sysaux.316.962209649
channel ORA_DISK_1: restoring datafile 00012 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/undotbs1.264.962209649
channel ORA_DISK_1: restoring datafile 00013 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/undo_2.268.962209649
channel ORA_DISK_1: restoring datafile 00014 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/users.278.962209649
channel ORA_DISK_1: restoring datafile 00015 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/test.275.962210609
channel ORA_DISK_1: reading from backup piece +TEST/rman_backup/jy_979425723_962563516_11slv3ds_1_1
channel ORA_DISK_1: piece handle=+TEST/rman_backup/jy_979425723_962563516_11slv3ds_1_1 tag=TAG20171212T184328
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:35
Finished restore at 2017-12-20 17:01:15

Starting recover at 2017-12-20 17:01:16
current log archived
using channel ORA_DISK_1


starting media recovery

archived log for thread 1 with sequence 38 is already on disk as file +TEST/arch/1_38_961976319.dbf
archived log for thread 1 with sequence 39 is already on disk as file +TEST/arch/1_39_961976319.dbf
archived log for thread 1 with sequence 40 is already on disk as file +TEST/arch/1_40_961976319.dbf
archived log for thread 1 with sequence 41 is already on disk as file +TEST/arch/1_41_961976319.dbf
archived log for thread 1 with sequence 42 is already on disk as file +TEST/arch/1_42_961976319.dbf
archived log for thread 1 with sequence 43 is already on disk as file +TEST/arch/1_43_961976319.dbf
archived log for thread 1 with sequence 44 is already on disk as file +TEST/arch/1_44_961976319.dbf
archived log for thread 1 with sequence 45 is already on disk as file +TEST/arch/1_45_961976319.dbf
archived log for thread 1 with sequence 46 is already on disk as file +TEST/arch/1_46_961976319.dbf
archived log for thread 1 with sequence 47 is already on disk as file +TEST/arch/1_47_961976319.dbf
archived log for thread 1 with sequence 48 is already on disk as file +TEST/arch/1_48_961976319.dbf
archived log for thread 1 with sequence 49 is already on disk as file +TEST/arch/1_49_961976319.dbf
archived log for thread 1 with sequence 50 is already on disk as file +TEST/arch/1_50_961976319.dbf
archived log for thread 1 with sequence 51 is already on disk as file +TEST/arch/1_51_961976319.dbf
archived log for thread 1 with sequence 52 is already on disk as file +TEST/arch/1_52_961976319.dbf
archived log for thread 1 with sequence 53 is already on disk as file +TEST/arch/1_53_961976319.dbf
archived log for thread 1 with sequence 54 is already on disk as file +TEST/arch/1_54_961976319.dbf
archived log for thread 1 with sequence 55 is already on disk as file +TEST/arch/1_55_961976319.dbf
archived log for thread 1 with sequence 56 is already on disk as file +TEST/arch/1_56_961976319.dbf
archived log for thread 1 with sequence 57 is already on disk as file +TEST/arch/1_57_961976319.dbf
archived log for thread 2 with sequence 32 is already on disk as file +TEST/arch/2_32_961976319.dbf
archived log for thread 2 with sequence 33 is already on disk as file +TEST/arch/2_33_961976319.dbf
archived log for thread 2 with sequence 34 is already on disk as file +TEST/arch/2_34_961976319.dbf
archived log for thread 2 with sequence 35 is already on disk as file +TEST/arch/2_35_961976319.dbf
archived log for thread 2 with sequence 36 is already on disk as file +TEST/arch/2_36_961976319.dbf
archived log for thread 2 with sequence 37 is already on disk as file +TEST/arch/2_37_961976319.dbf
archived log for thread 2 with sequence 38 is already on disk as file +TEST/arch/2_38_961976319.dbf
archived log for thread 2 with sequence 39 is already on disk as file +TEST/arch/2_39_961976319.dbf
archived log for thread 2 with sequence 40 is already on disk as file +TEST/arch/2_40_961976319.dbf
archived log for thread 2 with sequence 41 is already on disk as file +TEST/arch/2_41_961976319.dbf
archived log for thread 2 with sequence 42 is already on disk as file +TEST/arch/2_42_961976319.dbf
archived log for thread 2 with sequence 43 is already on disk as file +TEST/arch/2_43_961976319.dbf
archived log for thread 2 with sequence 44 is already on disk as file +TEST/arch/2_44_961976319.dbf
archived log for thread 2 with sequence 45 is already on disk as file +TEST/arch/2_45_961976319.dbf
archived log for thread 2 with sequence 46 is already on disk as file +TEST/arch/2_46_961976319.dbf
archived log for thread 2 with sequence 47 is already on disk as file +TEST/arch/2_47_961976319.dbf
archived log for thread 2 with sequence 48 is already on disk as file +TEST/arch/2_48_961976319.dbf
archived log for thread 2 with sequence 49 is already on disk as file +TEST/arch/2_49_961976319.dbf
archived log for thread 2 with sequence 50 is already on disk as file +TEST/arch/2_50_961976319.dbf
archived log for thread 2 with sequence 51 is already on disk as file +TEST/arch/2_51_961976319.dbf
archived log for thread 2 with sequence 52 is already on disk as file +DATA/JY/ONLINELOG/group_4.262.961976705
archived log for thread 2 with sequence 53 is already on disk as file +DATA/JY/ONLINELOG/group_3.263.961976697
media recovery complete, elapsed time: 00:04:03
Finished recover at 2017-12-20 17:05:30
starting full resync of recovery catalog
full resync complete

6. 以读写方式打开PDB,放弃目标SCN之后的所有改变,执行以下命令

RMAN> alter pluggable database jypdb open resetlogs;

Statement processed
starting full resync of recovery catalog
full resync complete



SQL> conn jy/jy@jypdb
Connected.
SQL> select count(*) from t1;

  COUNT(*)
----------
        39

使用RMAN对CDB执行按时间点恢复

使用RMAN对CDB和PDB执行按时间点恢复
RMAN能够对CDB与PDB执行按时间点恢复操作。但是PDB只能使用RMAN来执行按时间点恢复。如是没有使用恢复目录数据库,建议启用自动控制文件备份。否则当对PDB执行按时间点恢复时,当RMAN需要增加与删除undo数据文件时不能有效的执行。

PDB按时间点恢复与快速恢复区
当对PDB执行数据库按时间点恢复时,对于这个PDB所有的数据文件都将被执行恢复操作。然而,为了将PDB恢复到指定的时间点,RMAN在恢复目标时间点也是需要有UNDO表空间存在的。因为undo表空间是被所有PDB所共享的,它不能被恢复。RMAN会将root中的undo,system与sysaux表空间还原到辅助实例中,然后使用undo信息来将pdb恢复到指定的时间点。如果配置了快速恢复区,Oracle将会使用它作为辅助实例的存储目录。如果快速恢复区没有被配置,那么必须使用auxiliary destination子句来指定辅助实例数据库文件的存储目录。确保在快速恢复区有足够的空间可以用来还原root表空间与undo表空间。如果快速恢复区没有足够的空间,可以通过使用auxiliary destination子句来指定其它的目录。

对CDB执行数据库按时间点恢复
1.登录数据库记录当前SCN号,然后将表t1中的数据删除。

SQL> conn jy/jy@jypdb
Connected.
SQL> SELECT CURRENT_SCN   FROM V$DATABASE;

CURRENT_SCN
-----------
    6041183

SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

Session altered.

SQL> select sysdate from dual;

SYSDATE
-------------------
2017-12-18 18:28:30

SQL> select count(*) from t1;

  COUNT(*)
----------
        39

SQL> truncate table t1;

Table truncated.

SQL> select count(*) from t1;

  COUNT(*)
----------
         0

2.如果使用时间表达式来代替目标SCN,那么在调用RMAN之前设置时间格式环境变量

[oracle@jytest1 ~]$ export NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss'

3.使用RMAN连接到root容器

[oracle@jytest1 ~]$ rman target/ catalog rco/xxzx7817600@jypdb_173

Recovery Manager: Release 12.2.0.1.0 - Production on Mon Dec 18 18:32:00 2017

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

connected to target database: JY (DBID=979425723)
connected to recovery catalog database

4.将CDB重启到mount状态

RMAN> shutdown immediate

starting full resync of recovery catalog
full resync complete
database closed
database dismounted
Oracle instance shut down

RMAN> startup mount

connected to target database (not started)
Oracle instance started
database mounted

Total System Global Area    6442450944 bytes

Fixed Size                     8807168 bytes
Variable Size               1895828736 bytes
Database Buffers            4529848320 bytes
Redo Buffers                   7966720 bytes
starting full resync of recovery catalog
full resync complete

5.使用RUN块来执行以下操作
a.对于数据库按时间点鶋,使用set until来指定恢复的目标时间,scn或日志序列号,或者使用set to来指定还原点。如果指定时间那么使用环境变量nls_lang与nls_date_format中所指定的日期格式。

b.如果RMAN没有配置自动通道,那么需要手动分配磁盘与磁带通道。

c.还原与恢复CDB

下面的命令将CDB恢复到SCN=6041183所在的状态

RMAN> run
2> {
3>    set until scn 6041183;
4>    restore database;
5>    recover database;
6> }

executing command: SET until clause

Starting restore at 2017-12-18 18:46:50
flashing back control file to SCN 6041183
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=764 instance=jy1 device type=DISK

skipping datafile 5; already restored to file +DATA/JY/5F9AA264B21F3ED9E053AB828A0A6088/DATAFILE/system.256.962209675
skipping datafile 6; already restored to file +DATA/JY/5F9AA264B21F3ED9E053AB828A0A6088/DATAFILE/sysaux.270.962209675
skipping datafile 8; already restored to file +DATA/JY/5F9AA264B21F3ED9E053AB828A0A6088/DATAFILE/undotbs1.296.962209675
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00001 to +DATA/JY/DATAFILE/system.317.962209603
channel ORA_DISK_1: restoring datafile 00003 to +DATA/JY/DATAFILE/sysaux.298.962209605
channel ORA_DISK_1: restoring datafile 00004 to +DATA/JY/DATAFILE/undotbs1.277.962209605
channel ORA_DISK_1: restoring datafile 00007 to +DATA/JY/DATAFILE/users.301.962209605
channel ORA_DISK_1: restoring datafile 00009 to +DATA/JY/DATAFILE/undotbs2.312.962209605
channel ORA_DISK_1: reading from backup piece +TEST/rman_backup/jy_979425723_962563410_10slv3ai_1_1
channel ORA_DISK_1: piece handle=+TEST/rman_backup/jy_979425723_962563410_10slv3ai_1_1 tag=TAG20171212T184328
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:01:05
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00010 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/system.271.962209649
channel ORA_DISK_1: restoring datafile 00011 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/sysaux.316.962209649
channel ORA_DISK_1: restoring datafile 00012 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/undotbs1.264.962209649
channel ORA_DISK_1: restoring datafile 00013 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/undo_2.268.962209649
channel ORA_DISK_1: restoring datafile 00014 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/users.278.962209649
channel ORA_DISK_1: restoring datafile 00015 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/test.275.962210609
channel ORA_DISK_1: reading from backup piece +TEST/rman_backup/jy_979425723_962563516_11slv3ds_1_1
channel ORA_DISK_1: piece handle=+TEST/rman_backup/jy_979425723_962563516_11slv3ds_1_1 tag=TAG20171212T184328
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:35
channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00016 to +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/system.260.962469409
channel ORA_DISK_1: restoring datafile 00017 to +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/sysaux.259.962469409
channel ORA_DISK_1: restoring datafile 00018 to +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/undotbs1.265.962469409
channel ORA_DISK_1: restoring datafile 00019 to +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/undo_2.266.962469409
channel ORA_DISK_1: restoring datafile 00020 to +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/users.267.962469409
channel ORA_DISK_1: restoring datafile 00021 to +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/test.269.962469409
channel ORA_DISK_1: reading from backup piece +TEST/rman_backup/jy_979425723_962563551_12slv3ev_1_1
channel ORA_DISK_1: piece handle=+TEST/rman_backup/jy_979425723_962563551_12slv3ev_1_1 tag=TAG20171212T184328
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:35
Finished restore at 2017-12-18 18:49:09

Starting recover at 2017-12-18 18:49:11
using channel ORA_DISK_1
applied offline range to datafile 00010
offline range RECID=80 STAMP=963072332
applied offline range to datafile 00011
offline range RECID=79 STAMP=963072332
applied offline range to datafile 00012
offline range RECID=78 STAMP=963072332
applied offline range to datafile 00013
offline range RECID=77 STAMP=963072332
applied offline range to datafile 00014
offline range RECID=76 STAMP=963072332
applied offline range to datafile 00015
offline range RECID=75 STAMP=963072332
applied offline range to datafile 00016
offline range RECID=86 STAMP=963072332
applied offline range to datafile 00017
offline range RECID=85 STAMP=963072332
applied offline range to datafile 00018
offline range RECID=84 STAMP=963072332
applied offline range to datafile 00019
offline range RECID=83 STAMP=963072332
applied offline range to datafile 00020
offline range RECID=82 STAMP=963072332
applied offline range to datafile 00021
offline range RECID=81 STAMP=963072332

starting media recovery

archived log for thread 1 with sequence 34 is already on disk as file +TEST/arch/1_34_961976319.dbf
archived log for thread 1 with sequence 35 is already on disk as file +TEST/arch/1_35_961976319.dbf
archived log for thread 1 with sequence 36 is already on disk as file +TEST/arch/1_36_961976319.dbf
archived log for thread 1 with sequence 37 is already on disk as file +TEST/arch/1_37_961976319.dbf
archived log for thread 1 with sequence 38 is already on disk as file +TEST/arch/1_38_961976319.dbf
archived log for thread 1 with sequence 39 is already on disk as file +TEST/arch/1_39_961976319.dbf
archived log for thread 1 with sequence 40 is already on disk as file +TEST/arch/1_40_961976319.dbf
archived log for thread 1 with sequence 41 is already on disk as file +TEST/arch/1_41_961976319.dbf
archived log for thread 1 with sequence 42 is already on disk as file +TEST/arch/1_42_961976319.dbf
archived log for thread 1 with sequence 43 is already on disk as file +TEST/arch/1_43_961976319.dbf
archived log for thread 1 with sequence 44 is already on disk as file +TEST/arch/1_44_961976319.dbf
archived log for thread 1 with sequence 45 is already on disk as file +TEST/arch/1_45_961976319.dbf
archived log for thread 1 with sequence 46 is already on disk as file +TEST/arch/1_46_961976319.dbf
archived log for thread 1 with sequence 47 is already on disk as file +TEST/arch/1_47_961976319.dbf
archived log for thread 1 with sequence 48 is already on disk as file +TEST/arch/1_48_961976319.dbf
archived log for thread 1 with sequence 49 is already on disk as file +TEST/arch/1_49_961976319.dbf
archived log for thread 1 with sequence 50 is already on disk as file +TEST/arch/1_50_961976319.dbf
archived log for thread 1 with sequence 51 is already on disk as file +TEST/arch/1_51_961976319.dbf
archived log for thread 1 with sequence 52 is already on disk as file +DATA/JY/ONLINELOG/group_2.302.961976321
archived log for thread 1 with sequence 53 is already on disk as file +DATA/JY/ONLINELOG/group_1.261.961976319
archived log for thread 2 with sequence 28 is already on disk as file +TEST/arch/2_28_961976319.dbf
archived log for thread 2 with sequence 29 is already on disk as file +TEST/arch/2_29_961976319.dbf
archived log for thread 2 with sequence 30 is already on disk as file +TEST/arch/2_30_961976319.dbf
archived log for thread 2 with sequence 31 is already on disk as file +TEST/arch/2_31_961976319.dbf
archived log for thread 2 with sequence 32 is already on disk as file +TEST/arch/2_32_961976319.dbf
archived log for thread 2 with sequence 33 is already on disk as file +TEST/arch/2_33_961976319.dbf
archived log for thread 2 with sequence 34 is already on disk as file +TEST/arch/2_34_961976319.dbf
archived log for thread 2 with sequence 35 is already on disk as file +TEST/arch/2_35_961976319.dbf
archived log for thread 2 with sequence 36 is already on disk as file +TEST/arch/2_36_961976319.dbf
archived log for thread 2 with sequence 37 is already on disk as file +TEST/arch/2_37_961976319.dbf
archived log for thread 2 with sequence 38 is already on disk as file +TEST/arch/2_38_961976319.dbf
archived log for thread 2 with sequence 39 is already on disk as file +TEST/arch/2_39_961976319.dbf
archived log for thread 2 with sequence 40 is already on disk as file +TEST/arch/2_40_961976319.dbf
archived log for thread 2 with sequence 41 is already on disk as file +TEST/arch/2_41_961976319.dbf
archived log for thread 2 with sequence 42 is already on disk as file +TEST/arch/2_42_961976319.dbf
archived log for thread 2 with sequence 43 is already on disk as file +TEST/arch/2_43_961976319.dbf
archived log for thread 2 with sequence 44 is already on disk as file +TEST/arch/2_44_961976319.dbf
archived log for thread 2 with sequence 45 is already on disk as file +TEST/arch/2_45_961976319.dbf
archived log for thread 2 with sequence 46 is already on disk as file +TEST/arch/2_46_961976319.dbf
archived log for thread 2 with sequence 47 is already on disk as file +TEST/arch/2_47_961976319.dbf
archived log for thread 2 with sequence 48 is already on disk as file +TEST/arch/2_48_961976319.dbf
archived log for thread 2 with sequence 49 is already on disk as file +TEST/arch/2_49_961976319.dbf
archived log for thread 2 with sequence 50 is already on disk as file +TEST/arch/2_50_961976319.dbf
archived log for thread 2 with sequence 51 is already on disk as file +TEST/arch/2_51_961976319.dbf
archived log for thread 2 with sequence 52 is already on disk as file +DATA/JY/ONLINELOG/group_4.262.961976705
archived log for thread 2 with sequence 53 is already on disk as file +DATA/JY/ONLINELOG/group_3.263.961976697
archived log file name=+TEST/arch/1_34_961976319.dbf thread=1 sequence=34
archived log file name=+TEST/arch/2_28_961976319.dbf thread=2 sequence=28
archived log file name=+TEST/arch/1_35_961976319.dbf thread=1 sequence=35
archived log file name=+TEST/arch/2_29_961976319.dbf thread=2 sequence=29
archived log file name=+TEST/arch/1_36_961976319.dbf thread=1 sequence=36
archived log file name=+TEST/arch/2_30_961976319.dbf thread=2 sequence=30
archived log file name=+TEST/arch/2_31_961976319.dbf thread=2 sequence=31
archived log file name=+TEST/arch/1_37_961976319.dbf thread=1 sequence=37
archived log file name=+TEST/arch/2_32_961976319.dbf thread=2 sequence=32
archived log file name=+TEST/arch/1_38_961976319.dbf thread=1 sequence=38
archived log file name=+TEST/arch/2_33_961976319.dbf thread=2 sequence=33
archived log file name=+TEST/arch/1_39_961976319.dbf thread=1 sequence=39
archived log file name=+TEST/arch/2_34_961976319.dbf thread=2 sequence=34
archived log file name=+TEST/arch/1_40_961976319.dbf thread=1 sequence=40
archived log file name=+TEST/arch/2_35_961976319.dbf thread=2 sequence=35
archived log file name=+TEST/arch/1_41_961976319.dbf thread=1 sequence=41
archived log file name=+TEST/arch/2_36_961976319.dbf thread=2 sequence=36
archived log file name=+TEST/arch/1_42_961976319.dbf thread=1 sequence=42
archived log file name=+TEST/arch/2_37_961976319.dbf thread=2 sequence=37
archived log file name=+TEST/arch/2_38_961976319.dbf thread=2 sequence=38
archived log file name=+TEST/arch/1_43_961976319.dbf thread=1 sequence=43
archived log file name=+TEST/arch/2_39_961976319.dbf thread=2 sequence=39
archived log file name=+TEST/arch/1_44_961976319.dbf thread=1 sequence=44
archived log file name=+TEST/arch/2_40_961976319.dbf thread=2 sequence=40
archived log file name=+TEST/arch/1_45_961976319.dbf thread=1 sequence=45
archived log file name=+TEST/arch/2_41_961976319.dbf thread=2 sequence=41
archived log file name=+TEST/arch/1_46_961976319.dbf thread=1 sequence=46
archived log file name=+TEST/arch/2_42_961976319.dbf thread=2 sequence=42
archived log file name=+TEST/arch/2_43_961976319.dbf thread=2 sequence=43
archived log file name=+TEST/arch/1_47_961976319.dbf thread=1 sequence=47
archived log file name=+TEST/arch/2_44_961976319.dbf thread=2 sequence=44
archived log file name=+TEST/arch/2_45_961976319.dbf thread=2 sequence=45
archived log file name=+TEST/arch/1_48_961976319.dbf thread=1 sequence=48
archived log file name=+TEST/arch/2_46_961976319.dbf thread=2 sequence=46
archived log file name=+TEST/arch/1_49_961976319.dbf thread=1 sequence=49
archived log file name=+TEST/arch/2_47_961976319.dbf thread=2 sequence=47
archived log file name=+TEST/arch/2_48_961976319.dbf thread=2 sequence=48
archived log file name=+TEST/arch/1_50_961976319.dbf thread=1 sequence=50
archived log file name=+TEST/arch/2_49_961976319.dbf thread=2 sequence=49
archived log file name=+TEST/arch/1_51_961976319.dbf thread=1 sequence=51
archived log file name=+TEST/arch/2_50_961976319.dbf thread=2 sequence=50
archived log file name=+TEST/arch/2_51_961976319.dbf thread=2 sequence=51
media recovery complete, elapsed time: 00:08:44
Finished recover at 2017-12-18 18:58:02

6.执行以下互斥操作
.以读写方式打开CDB,放弃目标SCN之后的所有改变。在这种情况下,你必须将CDB重启到mount状态后,然后执行以下命令

alter database open resetlogs

.使用Data Pump导出CDB中你所需要的对象。然后将CDB恢复到当前时间点并重新导入对象,因此可以将需要的对象进行恢复而不用放弃所有其它对象所发生的改变。

RMAN> alter database open read only;

Statement processed


RMAN> alter pluggable database all open read only;

Statement processed



SQL> conn jy/jy@jypdb
Connected.
SQL> select count(*) from t1;

  COUNT(*)
----------
        39

RMAN> shutdown immediate

database closed
database dismounted
Oracle instance shut down

RMAN> startup mount

connected to target database (not started)
Oracle instance started
database mounted

Total System Global Area    6442450944 bytes

Fixed Size                     8807168 bytes
Variable Size               1895828736 bytes
Database Buffers            4529848320 bytes
Redo Buffers                   7966720 bytes

RMAN> recover database;

Starting recover at 2017-12-18 22:26:55
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=10 instance=jy1 device type=DISK

starting media recovery
media recovery complete, elapsed time: 00:00:05

Finished recover at 2017-12-18 22:27:05

RMAN> alter database open;

Statement processed
starting full resync of recovery catalog
full resync complete

RMAN> alter pluggable database all open read write;

Statement processed
starting full resync of recovery catalog
full resync complete

使用RMAN对PDB执行闪回数据库操作

可以对多租户数据库中的单个PDB执行闪回操作。对特定的PDB执行闪回数据库操作只会修改与这个PDB相关文件中的数据。CDB中的其它PDB不受影响处于可读写状态。如果使用还原点,在执行闪回数据库操作时可以使用CDB还原点,PDB还原点,PDB清晰还原点或PDB受保证的还原点。对PDB执行闪回数据库操作的步骤如下:
1.使用有sysdba或sysbackup权限的公共用户连接到root容器

SQL> conn / as sysdba
Connected.

2.确保CDB处于open状态

SQL> SELECT open_mode from V$DATABASE;

OPEN_MODE
--------------------
READ WRITE

3.登录PDB(jypdb)记录当前SCN号,然后删除表t1中的数据

SQL> conn jy/jy@jypdb
Connected.
SQL> SELECT CURRENT_SCN   FROM V$DATABASE;

CURRENT_SCN
-----------
    6024220

SQL> select count(*) from t1;

  COUNT(*)
----------
        39

SQL> delete from t1;

39 rows deleted.

SQL> commit;

Commit complete.

SQL> select count(*) from t1;

  COUNT(*)
----------
         0

4.确保要执行闪回数据库的PDB(jypdb)处于close状态。

SQL> conn / as sysdba
Connected.
SQL> alter pluggable database jypdb close immediate;

Pluggable database altered.

SQL> select name,open_mode from v$pdbs;

NAME                                                                                                                             OPEN_MODE
-------------------------------------------------------------------------------------------------------------------------------- ----------
PDB$SEED                                                                                                                         READ ONLY
JYPDB                                                                                                                            MOUNTED
TESTPDB                                                                                                                          READ WRITE

5.将PDB(jypdb)闪回到scn=6024220所在的状态

RMAN> flashback pluggable database jypdb to scn 6024220;

Starting flashback at 18-DEC-17
starting full resync of recovery catalog
full resync complete
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=11 instance=jy1 device type=DISK


starting media recovery
media recovery complete, elapsed time: 00:00:03

Finished flashback at 18-DEC-17

6. 将CDB设置为只读状态

RMAN> alter pluggable database jypdb open read only;

Statement processed

7.检查闪回数据库操作的结果是否满足要求

SQL> conn jy/jy@jypdb
Connected.
SQL> select count(*) from t1;

  COUNT(*)
----------
        39

8.如果闪回操作后满足你所要求的结果,那么可以执行以下两个相互排斥的选项:
.使用resetlogs选项将数据库打开。如果当前数据库为只读状态,那么使用SQL*Plus执行以下命令:

alter pluggable database jypdb close immediate;
alter pluggable database jypdb open resetlogs;

.使用Oracle Data Pump导出你所需要的对象,然后使用RMAN来恢复数据库到当前时间,通过对数据库重新应用重做日志中的所有改变来将
数据库恢复到当前时间点从而撤消闪回数据库所做的改变。

RMAN> recover pluggable database jypdb ;

Starting recover at 18-DEC-17
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=11 instance=jy1 device type=DISK

starting media recovery
media recovery complete, elapsed time: 00:00:01

Finished recover at 18-DEC-17

RMAN> alter pluggable database jypdb open read write;

Statement processed
starting full resync of recovery catalog
full resync complete

使用RMAN对CDB执行闪回数据库操作

对CDB执行闪回数据库与对non-CDB执行闪回数据库操作是类似的,具体操作步骤如下:
1.使用SQL*Plus连接到目标数据库来判断闪回操作的的目标SCN,还原点或时间点

SQL> SELECT OLDEST_FLASHBACK_SCN, OLDEST_FLASHBACK_TIME   FROM V$FLASHBACK_DATABASE_LOG;

OLDEST_FLASHBACK_SCN OLDEST_FLASH
-------------------- ------------
             5090113 17-DEC-17



SQL> SELECT CURRENT_SCN   FROM V$DATABASE;

CURRENT_SCN
-----------
    6003920

2.删除表t1中的数据

SQL> select count(*) from t1;

  COUNT(*)
----------
        39

SQL> delete from t1;

39 rows deleted.

SQL> commit;

Commit complete.

3.关闭数据库,并确保没有任何实例打开数据库,然后启动到mount状态

SQL> shutdown  immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area 6442450944 bytes
Fixed Size                  8807168 bytes
Variable Size            1895828736 bytes
Database Buffers         4529848320 bytes
Redo Buffers                7966720 bytes
Database mounted.

4.运行flashback database命令将整个CDB闪回到指定的时间点

SQL> flashback database to scn 6003920;

Flashback complete.

5.在SQL*Plus命令窗口中将CDB设置为只读状态

SQL> alter database open read only;

Database altered.

6.如果闪回操作后满足你所要求的结果,那么可以执行以下两个相互排斥的选项:
.使用resetlogs选项将数据库打开。如果当前数据库为只读状态,那么使用SQL*Plus执行以下命令:

shutdown immediate
startup mount
alter database open resetlogs

.使用Oracle Data Pump导出你所需要的对象,然后使用RMAN来恢复数据库到当前时间,通过对数据库重新应用重做日志中的所有改变来将
数据库恢复到当前时间点从而撤消闪回数据库所做的改变。

RMAN> recover database;

Starting recover at 18-DEC-17
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=11 instance=jy1 device type=DISK

starting media recovery
media recovery complete, elapsed time: 00:00:02

Finished recover at 18-DEC-17

Media recovery complete.
SQL> alter database open;

Database altered.

SQL> alter pluggable database all open read write;

Pluggable database altered.

使用RMAN对PDB中的表空间或数据文件执行完全恢复

因为不同PDB中的表空间可以有相同的名字,为了消除这种混淆你必须直接连接到PDB来恢复PDB的表空间。相反,因为数据文件号与路径名是跨CDB唯一标识的,所以可以连接到root容器或PDB来恢复PDB的数据文件。如果连接到CDB的root容器,那么可以使用单个命令来恢复多个PDB中的数据文件。如果连接到PDB,只能恢复PDB中的数据文件。

还原与恢复PDB中的non-SYSTEM表空间
1.启动RMAN并连接到目标数据库

[oracle@jytest1 ~]$ rman target sys/xxxxx@jypdb

Recovery Manager: Release 12.2.0.1.0 - Production on Mon Dec 11 17:59:27 2017

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

connected to target database: JY:JYPDB (DBID=4203494064)

2.如果数据库的状态为open,那么将要恢复的表空间置于脱机状态

SQL> alter tablespace test offline immediate;

Tablespace altered.

3.使用show命令来查看是否配置了预先设置的通道。如果需要的设置类型与通道被配置,那么不需要执行任何操作。否则可以使用configure命令来配置自动通道,或都使用包含allocate channel命令的run块

RMAN> show all;

RMAN configuration parameters for database with db_unique_name JY are:
CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP ON; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '+test/rman_backup/%F';
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE ARCHIVELOG BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
CONFIGURE MAXSETSIZE TO UNLIMITED; # default
CONFIGURE ENCRYPTION FOR DATABASE OFF; # default
CONFIGURE ENCRYPTION ALGORITHM 'AES128'; # default
CONFIGURE COMPRESSION ALGORITHM 'BASIC' AS OF RELEASE 'DEFAULT' OPTIMIZE FOR LOAD TRUE ; # default
CONFIGURE RMAN OUTPUT TO KEEP FOR 7 DAYS; # default
CONFIGURE ARCHIVELOG DELETION POLICY TO NONE; # default
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '+test/jy/snapcf_jy.f';

4.还原与恢复表空间
.如果将数据文件还原到原始位置,那么执行restore tablespace与recover tablespace命令

.如果将数据文件还原到新目录,那么在run命令块中执行restore tablespace与recover tablespace命令,并且使用set newname命令来重命名数据文件。

RMAN> restore tablespace 'TEST';

Starting restore at 11-DEC-17
using channel ORA_DISK_1

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00015 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/test.275.962210609
channel ORA_DISK_1: reading from backup piece +TEST/rman_backup/jy_979425723_20171211_0osls830_1_1
channel ORA_DISK_1: piece handle=+TEST/rman_backup/jy_979425723_20171211_0osls830_1_1 tag=TAG20171211T164446
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
Finished restore at 11-DEC-17

RMAN> recover tablespace 'TEST';

Starting recover at 11-DEC-17
using channel ORA_DISK_1

starting media recovery
media recovery complete, elapsed time: 00:00:01

Finished recover at 11-DEC-17

5.检查输出结果查看是否恢复成功。如果恢复成功将表空间设置为联机状态

RMAN> alter tablespace test online;

Statement processed

还原与恢复PDB中的SYSTEM表空间
1.启动RMAN并使用有sysdba或sysbackup权限的公共用户连接到root容器。

[oracle@jytest1 ~]$ rman target/ catalog rco/xxzx7817600@jypdb_173

Recovery Manager: Release 12.2.0.1.0 - Production on Mon Dec 11 18:11:55 2017

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

connected to target database: JY (DBID=979425723)
connected to recovery catalog database

2.关闭CDB并重启到mount状态

SQL> conn / as sysdba
Connected.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.

Total System Global Area 6442450944 bytes
Fixed Size                  8807168 bytes
Variable Size            1895828736 bytes
Database Buffers         4529848320 bytes
Redo Buffers                7966720 bytes
Database mounted.

3.还原与恢复PDB中SYSTEM表空间的数据文件

[oracle@jytest1 ~]$ rman target/ catalog rco/xxzx7817600@jypdb_173

Recovery Manager: Release 12.2.0.1.0 - Production on Mon Dec 11 18:20:30 2017

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

connected to target database: JY (DBID=979425723, not open)
connected to recovery catalog database

RMAN> report schema;

starting full resync of recovery catalog
full resync complete
Report of database schema for database with db_unique_name JY

List of Permanent Datafiles
===========================
File Size(MB) Tablespace           RB segs Datafile Name
---- -------- -------------------- ------- ------------------------
1    830      SYSTEM               YES     +DATA/JY/DATAFILE/system.317.962209603
3    1230     SYSAUX               NO      +DATA/JY/DATAFILE/sysaux.298.962209605
4    75       UNDOTBS1             YES     +DATA/JY/DATAFILE/undotbs1.277.962209605
5    250      PDB$SEED:SYSTEM      NO      +DATA/JY/5F9AA264B21F3ED9E053AB828A0A6088/DATAFILE/system.256.962209675
6    340      PDB$SEED:SYSAUX      NO      +DATA/JY/5F9AA264B21F3ED9E053AB828A0A6088/DATAFILE/sysaux.270.962209675
7    5        USERS                NO      +DATA/JY/DATAFILE/users.301.962209605
8    100      PDB$SEED:UNDOTBS1    NO      +DATA/JY/5F9AA264B21F3ED9E053AB828A0A6088/DATAFILE/undotbs1.296.962209675
9    50       UNDOTBS2             YES     +DATA/JY/DATAFILE/undotbs2.312.962209605
10   260      JYPDB:SYSTEM         YES     +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/system.271.962209649
11   460      JYPDB:SYSAUX         NO      +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/sysaux.316.962209649
12   100      JYPDB:UNDOTBS1       YES     +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/undotbs1.264.962209649
13   100      JYPDB:UNDO_2         YES     +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/undo_2.268.962209649
14   5        JYPDB:USERS          NO      +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/users.278.962209649
15   100      JYPDB:TEST           NO      +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/test.275.962210609
16   260      TESTPDB:SYSTEM       YES     +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/system.260.962469409
17   460      TESTPDB:SYSAUX       NO      +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/sysaux.259.962469409
18   100      TESTPDB:UNDOTBS1     YES     +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/undotbs1.265.962469409
19   100      TESTPDB:UNDO_2       YES     +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/undo_2.266.962469409
20   5        TESTPDB:USERS        NO      +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/users.267.962469409
21   100      TESTPDB:TEST         NO      +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/DATAFILE/test.269.962469409

List of Temporary Files
=======================
File Size(MB) Tablespace           Maxsize(MB) Tempfile Name
---- -------- -------------------- ----------- --------------------
1    142      TEMP                 32767       +DATA/JY/TEMPFILE/temp.299.961976339
2    64       PDB$SEED:TEMP        32767       +DATA/JY/5F9AA264B21F3ED9E053AB828A0A6088/TEMPFILE/temp.297.962209865
3    135      JYPDB:TEMP           32767       +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/TEMPFILE/temp.276.962210519
4    135      TESTPDB:TEMP         32767       +DATA/JY/600D6F56DEB669CCE053AB828A0AAB7E/TEMPFILE/temp.258.962469435

RMAN> restore tablespace 'JYPDB:SYSTEM';

Starting restore at 11-DEC-17
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=10 instance=jy1 device type=DISK
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 12/11/2017 18:21:09
RMAN-06004: ORACLE error from recovery catalog database: RMAN-20202: Tablespace not found in the recovery catalog
RMAN-06019: could not translate tablespace name "JYPDB:SYSTEM"

这里在还原SYSTEM表空间时使用表空间名时会报错,如是使用restore datafile与recover datafile方法来还原与恢复SYSTEM表空间

RMAN> restore datafile 10;

Starting restore at 11-DEC-17
using channel ORA_DISK_1

channel ORA_DISK_1: starting datafile backup set restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_DISK_1: restoring datafile 00010 to +DATA/JY/5F9AC6865E87549FE053AB828A0ADE94/DATAFILE/system.271.962209649
channel ORA_DISK_1: reading from backup piece +TEST/rman_backup/jy_979425723_20171211_0osls830_1_1
channel ORA_DISK_1: piece handle=+TEST/rman_backup/jy_979425723_20171211_0osls830_1_1 tag=TAG20171211T164446
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:15
Finished restore at 11-DEC-17

RMAN> recover datafile 10;

Starting recover at 11-DEC-17
using channel ORA_DISK_1

starting media recovery
media recovery complete, elapsed time: 00:00:01

Finished recover at 11-DEC-17

4.打开CDB中的所有PDB

RMAN> alter database open;

Statement processed
starting full resync of recovery catalog
full resync complete

RMAN> alter pluggable database all open read write;

Statement processed
starting full resync of recovery catalog
full resync complete

Proudly powered by WordPress | Indrajeet by Sus Hill.