12c特性——Recover Table(基于时间点对表的恢复)

简介

应用场景

1、You need to recover a very small number of tables to a particular point in time. In this situation, TSPITR is not the most effective solution because it moves all the objects in the tablespace to a specified point in time.
2、You need to recover tables that have been logically corrupted or have been dropped and purged.
3、Flashback Table is not possible because the desired point-in-time is older than available undo.
4、You want to recover data that is lost after a DDL operation modified the structure of tables. Using Flashback Table is not possible because a DDL was run on the tables between the desired point in time and the current time. Flashback Table cannot rewind tables through structural changes such as a truncate table operation.

简单来说就是这样的情况:

1.恢复一部分表到指定scn而不是整个表空间

2.表已经被删除或者逻辑损坏

3.闪回不可用,或者不能闪回到所需的时间点,如undo被覆盖或者闪回区被清空、所需时间点到当前时间被TRUNCATE的数据

限制

RECOVER TABLE限制:

  • 目标数据库必须被置于读写模式。

  • 目标数据库必须被置于归档模式。

  • SYS用户下的表或分区无法恢复。

  • 存储于SYSAUX和SYSTEM表空间下的表和分区无法恢复。

恢复原理

在12c之前对于用户因DDL(DROP TRUNCATE )等操作引起的数据丢失,一般通过数据库时间点不完全恢复、基于表空间的时间点恢复(TSPITR)、闪回技术。

12c推出了rman基于时间点对表的恢复。原理如下:

Recover Table新特性是利用创建辅助临时实例加数据泵工具来实现的。通常在进行Recover Table之前应该准备好两个目录:(AUXILIARY DESTINATION和DATAPUMP DESTINATION)

  • AUXILIARY DESTINATION用来临时存放辅助实例的数据文件
  • DATAPUMP DESTINATION用来临时存放数据泵导出的文件

优缺点:

操作命令变的更简单,但恢复时间还是会比较长

恢复示例

1.创建表空间和用户,并授权、建表

SQL> show pdbs;

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 ORCLPDB                        READ WRITE NO

SQL> alter session set container=orclpdb;

SQL> create tablespace abc datafile '/u01/app/oracle/oradata/ORCL/orclpdb/abc.dbf' size 50m;

SQL> create user abc identified by abc default tablespace abc;

SQL> grant resource,connect,dba to abc;

#建表
SQL> conn abc/abc@192.168.250.31:1521/orclpdb
Connected.
SQL> create table t1 as select * from all_objects;

Table created.

SQL> select count(*) from t1;

  COUNT(*)
----------
     67119

#刷日志
SQL> conn / as sysdba
Connected.
SQL> show user;
USER is "SYS"
SQL> alter system switch logfile;
SQL> alter system switch logfile;
SQL> alter system switch logfile;
SQL> alter system switch logfile;

SQL>  select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'YY
-------------------
2022-05-16 14:40:32



2.备份后删表

RMAN> backup database plus archivelog;

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'YY
-------------------
2022-05-16 14:43:27

SQL> conn abc/abc@192.168.250.31:1521/orclpdb
Connected.

SQL> drop table t1;
        
SQL> select count(*) from t1;
select count(*) from t1
                     *
ERROR at line 1:
ORA-00942: table or view does not exist

3.恢复表

#rman中恢复表
recover table abc.t1 of pluggable database orclpdb
until time "to_date('2022-05-16 14:43:27','yyyy-mm-dd hh24:mi:ss')"
auxiliary destination '/home/oracle'
datapump destination '/home/oracle';

#查看数据
SQL> select count(*) from t1;

  COUNT(*)
----------
     67119



#恢复时重命名
recover table abc.t1 of pluggable database orclpdb
until time "to_date('2022-05-16 14:43:27','yyyy-mm-dd hh24:mi:ss')"
auxiliary destination '/home/oracle'
datapump destination '/home/oracle'
remap table orclpdb.t1:t2;

4.日志详情

从日志可以看到整个恢复过程

recover table abc.t1 of pluggable database orclpdb
until time "to_date('2022-05-16 14:43:27','yyyy-mm-dd hh24:mi:ss')"
auxiliary destination '/home/oracle'
4> datapump destination '/home/oracle';

Starting recover at 16-MAY-22
using channel ORA_DISK_1
RMAN-05026: warning: presuming following set of tablespaces applies to specified point-in-time

List of tablespaces expected to have UNDO segments
Tablespace SYSTEM
Tablespace ORCLPDB:SYSTEM
Tablespace UNDOTBS1
Tablespace ORCLPDB:UNDOTBS1

Creating automatic instance, with SID='EuAi'

initialization parameters used for automatic instance:
db_name=ORCL
db_unique_name=EuAi_pitr_orclpdb_ORCL
compatible=19.0.0
db_block_size=8192
db_files=200
diagnostic_dest=/u01/app/oracle
_system_trig_enabled=FALSE
sga_target=4720M
processes=200
db_create_file_dest=/home/oracle
log_archive_dest_1='location=/home/oracle'
enable_pluggable_database=true
_clone_one_pdb_recovery=true
#No auxiliary parameter file used


starting up automatic instance ORCL

Oracle instance started

Total System Global Area    4949277056 bytes

Fixed Size                     9144704 bytes
Variable Size                922746880 bytes
Database Buffers            4009754624 bytes
Redo Buffers                   7630848 bytes
Automatic instance created

contents of Memory Script:
{
# set requested point in time
set until  time "to_date('2022-05-16 14:43:27','yyyy-mm-dd hh24:mi:ss')";
# restore the controlfile
restore clone controlfile;

# mount the controlfile
sql clone 'alter database mount clone database';

# archive current online log
sql 'alter system archive log current';
}
executing Memory Script

executing command: SET until clause

Starting restore at 16-MAY-22
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=169 device type=DISK

channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: restoring control file
channel ORA_AUX_DISK_1: reading from backup piece /u01/backups/bk_fr0tlarq_1_1
channel ORA_AUX_DISK_1: piece handle=/u01/backups/bk_fr0tlarq_1_1 tag=TAG20220516T144120
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:03
output file name=/home/oracle/ORCL/controlfile/o1_mf_k83xggco_.ctl
Finished restore at 16-MAY-22

sql statement: alter database mount clone database

sql statement: alter system archive log current

contents of Memory Script:
{
# set requested point in time
set until  time "to_date('2022-05-16 14:43:27','yyyy-mm-dd hh24:mi:ss')";
# set destinations for recovery set and auxiliary set datafiles
set newname for clone datafile  1 to new;
set newname for clone datafile  9 to new;
set newname for clone datafile  4 to new;
set newname for clone datafile  11 to new;
set newname for clone datafile  3 to new;
set newname for clone datafile  10 to new;
set newname for clone tempfile  1 to new;
set newname for clone tempfile  3 to new;
# switch all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  1, 9, 4, 11, 3, 10;

switch clone datafile all;
}
executing Memory Script

executing command: SET until clause

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

renamed tempfile 1 to /home/oracle/ORCL/datafile/o1_mf_temp_%u_.tmp in control file
renamed tempfile 3 to /home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 16-MAY-22
using channel ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_1: restoring datafile 00001 to /home/oracle/ORCL/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00004 to /home/oracle/ORCL/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /home/oracle/ORCL/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/backups/bk_fo0tlaog_1_1
channel ORA_AUX_DISK_1: piece handle=/u01/backups/bk_fo0tlaog_1_1 tag=TAG20220516T144120
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:04:55
channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_1: restoring datafile 00009 to /home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00011 to /home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00010 to /home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/backups/bk_fp0tlaq7_1_1
channel ORA_AUX_DISK_1: piece handle=/u01/backups/bk_fp0tlaq7_1_1 tag=TAG20220516T144120
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:16
Finished restore at 16-MAY-22

datafile 1 switched to datafile copy
input datafile copy RECID=14 STAMP=1104851019 file name=/home/oracle/ORCL/datafile/o1_mf_system_k83xgrv5_.dbf
datafile 9 switched to datafile copy
input datafile copy RECID=15 STAMP=1104851019 file name=/home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_system_k83xr05w_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=16 STAMP=1104851019 file name=/home/oracle/ORCL/datafile/o1_mf_undotbs1_k83xgrx9_.dbf
datafile 11 switched to datafile copy
input datafile copy RECID=17 STAMP=1104851019 file name=/home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_undotbs1_k83xr069_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=18 STAMP=1104851019 file name=/home/oracle/ORCL/datafile/o1_mf_sysaux_k83xgrw0_.dbf
datafile 10 switched to datafile copy
input datafile copy RECID=19 STAMP=1104851019 file name=/home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_sysaux_k83xr051_.dbf

contents of Memory Script:
{
# set requested point in time
set until  time "to_date('2022-05-16 14:43:27','yyyy-mm-dd hh24:mi:ss')";
# online the datafiles restored or switched
sql clone "alter database datafile  1 online";
sql clone 'ORCLPDB' "alter database datafile
 9 online";
sql clone "alter database datafile  4 online";
sql clone 'ORCLPDB' "alter database datafile
 11 online";
sql clone "alter database datafile  3 online";
sql clone 'ORCLPDB' "alter database datafile
 10 online";
# recover and open database read only
recover clone database tablespace  "SYSTEM", "ORCLPDB":"SYSTEM", "UNDOTBS1", "ORCLPDB":"UNDOTBS1", "SYSAUX", "ORCLPDB":"SYSAUX";
sql clone 'alter database open read only';
}
executing Memory Script

executing command: SET until clause

sql statement: alter database datafile  1 online

sql statement: alter database datafile  9 online

sql statement: alter database datafile  4 online

sql statement: alter database datafile  11 online

sql statement: alter database datafile  3 online

sql statement: alter database datafile  10 online

Starting recover at 16-MAY-22
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 28 is already on disk as file /u01/app/oracle/product/19.3/dbhome_1/dbs/arch1_28_1104416121.dbf
archived log for thread 1 with sequence 29 is already on disk as file /u01/app/oracle/product/19.3/dbhome_1/dbs/arch1_29_1104416121.dbf
archived log file name=/u01/app/oracle/product/19.3/dbhome_1/dbs/arch1_28_1104416121.dbf thread=1 sequence=28
archived log file name=/u01/app/oracle/product/19.3/dbhome_1/dbs/arch1_29_1104416121.dbf thread=1 sequence=29
media recovery complete, elapsed time: 00:00:01
Finished recover at 16-MAY-22

sql statement: alter database open read only

contents of Memory Script:
{
sql clone 'alter pluggable database  ORCLPDB open read only';
}
executing Memory Script

sql statement: alter pluggable database  ORCLPDB open read only

contents of Memory Script:
{
   sql clone "create spfile from memory";
   shutdown clone immediate;
   startup clone nomount;
   sql clone "alter system set  control_files =
  ''/home/oracle/ORCL/controlfile/o1_mf_k83xggco_.ctl'' comment=
 ''RMAN set'' scope=spfile";
   shutdown clone immediate;
   startup clone nomount;
# mount database
sql clone 'alter database mount clone database';
}
executing Memory Script

sql statement: create spfile from memory

database closed
database dismounted
Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area    4949277056 bytes

Fixed Size                     9144704 bytes
Variable Size                922746880 bytes
Database Buffers            4009754624 bytes
Redo Buffers                   7630848 bytes

sql statement: alter system set  control_files =   ''/home/oracle/ORCL/controlfile/o1_mf_k83xggco_.ctl'' comment= ''RMAN set'' scope=spfile

Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area    4949277056 bytes

Fixed Size                     9144704 bytes
Variable Size                922746880 bytes
Database Buffers            4009754624 bytes
Redo Buffers                   7630848 bytes

sql statement: alter database mount clone database

contents of Memory Script:
{
# set requested point in time
set until  time "to_date('2022-05-16 14:43:27','yyyy-mm-dd hh24:mi:ss')";
# set destinations for recovery set and auxiliary set datafiles
set newname for datafile  18 to new;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  18;

switch clone datafile all;
}
executing Memory Script

executing command: SET until clause

executing command: SET NEWNAME

Starting restore at 16-MAY-22
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=129 device type=DISK

channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_1: restoring datafile 00018 to /home/oracle/EUAI_PITR_ORCLPDB_ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_abc_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/backups/bk_fp0tlaq7_1_1
channel ORA_AUX_DISK_1: piece handle=/u01/backups/bk_fp0tlaq7_1_1 tag=TAG20220516T144120
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:07
Finished restore at 16-MAY-22

datafile 18 switched to datafile copy
input datafile copy RECID=21 STAMP=1104851117 file name=/home/oracle/EUAI_PITR_ORCLPDB_ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_abc_k83xx68p_.dbf

contents of Memory Script:
{
# set requested point in time
set until  time "to_date('2022-05-16 14:43:27','yyyy-mm-dd hh24:mi:ss')";
# online the datafiles restored or switched
sql clone 'ORCLPDB' "alter database datafile
 18 online";
# recover and open resetlogs
recover clone database tablespace  "ORCLPDB":"ABC", "SYSTEM", "ORCLPDB":"SYSTEM", "UNDOTBS1", "ORCLPDB":"UNDOTBS1", "SYSAUX", "ORCLPDB":"SYSAUX" delete archivelog;
alter clone database open resetlogs;
}
executing Memory Script

executing command: SET until clause

sql statement: alter database datafile  18 online

Starting recover at 16-MAY-22
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 28 is already on disk as file /u01/app/oracle/product/19.3/dbhome_1/dbs/arch1_28_1104416121.dbf
archived log for thread 1 with sequence 29 is already on disk as file /u01/app/oracle/product/19.3/dbhome_1/dbs/arch1_29_1104416121.dbf
archived log file name=/u01/app/oracle/product/19.3/dbhome_1/dbs/arch1_28_1104416121.dbf thread=1 sequence=28
archived log file name=/u01/app/oracle/product/19.3/dbhome_1/dbs/arch1_29_1104416121.dbf thread=1 sequence=29
media recovery complete, elapsed time: 00:00:01
Finished recover at 16-MAY-22

database opened

contents of Memory Script:
{
sql clone 'alter pluggable database  ORCLPDB open';
}
executing Memory Script

sql statement: alter pluggable database  ORCLPDB open

contents of Memory Script:
{
# create directory for datapump import
sql 'ORCLPDB' "create or replace directory
TSPITR_DIROBJ_DPDIR as ''
/home/oracle''";
# create directory for datapump export
sql clone 'ORCLPDB' "create or replace directory
TSPITR_DIROBJ_DPDIR as ''
/home/oracle''";
}
executing Memory Script

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle''

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle''

Performing export of tables...
   EXPDP> Starting "SYS"."TSPITR_EXP_EuAi_zqcD":
   EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
   EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
   EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
   EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
   EXPDP> . . exported "ABC"."T1"                                  8.942 MB   67119 rows
   EXPDP> Master table "SYS"."TSPITR_EXP_EuAi_zqcD" successfully loaded/unloaded
   EXPDP> ******************************************************************************
   EXPDP> Dump file set for SYS.TSPITR_EXP_EuAi_zqcD is:
   EXPDP>   /home/oracle/tspitr_EuAi_55365.dmp
   EXPDP> Job "SYS"."TSPITR_EXP_EuAi_zqcD" successfully completed at Mon May 16 15:07:28 2022 elapsed 0 00:00:35
Export completed


contents of Memory Script:
{
# shutdown clone before import
shutdown clone abort
}
executing Memory Script

Oracle instance shut down

Performing import of tables...
   IMPDP> Master table "SYS"."TSPITR_IMP_EuAi_nnFr" successfully loaded/unloaded
   IMPDP> Starting "SYS"."TSPITR_IMP_EuAi_nnFr":
   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
   IMPDP> . . imported "ABC"."T1"                                  8.942 MB   67119 rows
   IMPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
   IMPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
   IMPDP> Job "SYS"."TSPITR_IMP_EuAi_nnFr" successfully completed at Mon May 16 15:08:16 2022 elapsed 0 00:00:35
Import completed


Removing automatic instance
Automatic instance removed
auxiliary instance file /home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_temp_k83xtp1v_.tmp deleted
auxiliary instance file /home/oracle/ORCL/datafile/o1_mf_temp_k83xtkfk_.tmp deleted
auxiliary instance file /home/oracle/EUAI_PITR_ORCLPDB_ORCL/onlinelog/o1_mf_3_k83xxkr5_.log deleted
auxiliary instance file /home/oracle/EUAI_PITR_ORCLPDB_ORCL/onlinelog/o1_mf_2_k83xxkql_.log deleted
auxiliary instance file /home/oracle/EUAI_PITR_ORCLPDB_ORCL/onlinelog/o1_mf_1_k83xxkpz_.log deleted
auxiliary instance file /home/oracle/EUAI_PITR_ORCLPDB_ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_abc_k83xx68p_.dbf deleted
auxiliary instance file /home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_sysaux_k83xr051_.dbf deleted
auxiliary instance file /home/oracle/ORCL/datafile/o1_mf_sysaux_k83xgrw0_.dbf deleted
auxiliary instance file /home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_undotbs1_k83xr069_.dbf deleted
auxiliary instance file /home/oracle/ORCL/datafile/o1_mf_undotbs1_k83xgrx9_.dbf deleted
auxiliary instance file /home/oracle/ORCL/DE1D9FEADEF27D90E05343720B0ACA14/datafile/o1_mf_system_k83xr05w_.dbf deleted
auxiliary instance file /home/oracle/ORCL/datafile/o1_mf_system_k83xgrv5_.dbf deleted
auxiliary instance file /home/oracle/ORCL/controlfile/o1_mf_k83xggco_.ctl deleted
auxiliary instance file tspitr_EuAi_55365.dmp deleted
Finished recover at 16-MAY-22

posted @ 2022-05-16 15:04  EverEternity  阅读(251)  评论(0编辑  收藏  举报