12c RMAN新特性之Recover Table

周玉其 云和恩墨南区技术顾问,曾在电信、物流等行业从事多年数据库开发、管理工作。

摘要

12c 的 rman 有了很大的增强,其中一个亮点就是可以进行表(或分区)级别的恢复,相比 TSPITR,recover table 更加快捷、灵活。

介绍

在 Oracle 12c 之前,如果误删一张表,常规的方法是 Flashback 闪回或 TSPITR。而开启 flashback 会消耗磁盘空间,且如果被 purge 或者经过 DDL,将无法 flashback。如果需要恢复的表空间过大,TSPITR 会耗时非常久。12c 中提供了Table Recovery 的新特性,可以针对单表(或单个表分区)基于时间点进行恢复,该特性用以直接从 RMAN 备份中恢复表的方法,该特性有以下特点:

  • 不影响数据库中的其他数据库对象
  • 减少时间和磁盘空间的消耗,和早期的 TSPIRT 工作流程类似
  • 从另一个磁盘路径恢复表空间
  • 导出想要的表数据
  • 导入到原数据库(可选)

recover table 可以将一个或多个表或表分区恢复到指定的时间点,而不会影响其余的数据库对象。可以使用以前创建的 RMAN 备份将表和表分区恢复到指定的时间点。

该表被恢复到一个辅助实例中,并且可以选择:

  • 使用 REMAP 选项将恢复的表导入新表或分区
  • 仅在恢复的表中创建 expdp dump 文件,以便在其他时间进行导入

先决条件

目标数据库必须处于读写模式。

目标数据库必须处于 ARCHIVELOG 模式。

您必须拥有表或表分区的 RMAN 备份,因为它们存在于要恢复这些对象的时间点。

要恢复单个表分区,目标数据库的 COMPATIBLE 初始化参数必须设置为 11.1.0 或更高。

请确保 AUXILIARY DESTINATION 有足够的空间来恢复 system、sysaux、undo 以及恢复表所需的表空间。

请注意,表恢复后,辅助目标中的这些文件将自动删除

必须使用 AUXILIARY DESTINATION 子句和后面其中一个子句来指定恢复的时间点:UNTIL TIME、UNTIL SCN 或 UNTIL SEQUENCE。

使用限制

  • 属于SYS模式的表和表分区无法恢复。
  • SYSTEM和SYSAUX表空间中的表和表分区无法恢复。
  • standby数据库上的表和表分区无法恢复。
  • 命名为NOT NULL约束的表不能用REMAP选项恢复。

使用场景

RMAN 的表级和表分区级恢复在以下场景中非常适用:

(1) 需要将非常少量的表恢复至特定的时间点。在这种情况下,TSPITR 不是最有效的解决方案,因为它会将表空间中的所有对象恢复到指定的时间点。 (2) 需要恢复已被逻辑损坏或已被 drop 和 purge 的表。 (3) flashback 不可用,比如需要的 undo 数据被覆盖。 (4) DDL 操作修改表结构后丢失的数据。flashback table 不能跨越 DDL(例如truncate)来回滚表。

在这Recover table 过程演示

创建测试表

SQL> create table t_recover_test as select * from dba_objects; Table created. SQL> select count(*) from t_recover_test; COUNT(*)---------- 72652

备份

[oracle@localhost ~]$ rman target / Recovery Manager: Release 12.2.0.1.0 - Production on Thu Jun 28 21:06:17 2018 Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved. connected to target database: PCC (DBID=3164878903) RMAN> backup database format '/home/oracle/bak/db_%U'; Starting backup at 28-JUN-18using target database control file instead of recovery catalogallocated channel: ORA_DISK_1channel ORA_DISK_1: SID=61 device type=DISKchannel ORA_DISK_1: starting full datafile backup setchannel ORA_DISK_1: specifying datafile(s) in backup setinput datafile file number=00001 name=/home/oracle/oradata/pcc/system01.dbfinput datafile file number=00003 name=/home/oracle/oradata/pcc/sysaux01.dbfinput datafile file number=00004 name=/home/oracle/oradata/pcc/undotbs01.dbfinput datafile file number=00005 name=/home/oracle/oradata/pcc/tbs_zyq01.dbfinput datafile file number=00007 name=/home/oracle/oradata/pcc/users01.dbfchannel ORA_DISK_1: starting piece 1 at 28-JUN-18channel ORA_DISK_1: finished piece 1 at 28-JUN-18piece handle=/home/oracle/bak/db_01t6k254_1_1 tag=TAG20180628T215236 comment=NONEchannel ORA_DISK_1: backup set complete, elapsed time: 00:00:15Finished backup at 28-JUN-18 Starting Control File and SPFILE Autobackup at 28-JUN-18piece handle=/u01/app/oracle/product/12.2.0.1/dbs/c-3164878903-20180628-00 comment=NONEFinished Control File and SPFILE Autobackup at 28-JUN-18

恢复表

[oracle@localhost ~]$ rman target / Recovery Manager: Release 12.2.0.1.0 - Production on Fri Jun 29 00:52:29 2018 Copyright (c) 1982, 2017, Oracle and/or its affiliates. All rights reserved. connected to target database: PCC (DBID=3164878903) RMAN> RECOVER TABLE zyq.t_recover_testuntil time "to_date('2018-06-28 23:56:06','yyyy-mm-dd hh24:mi:ss')"AUXILIARY DESTINATION '/data01'REMAP TABLE zyq.t_recover_test:t_recover_test_map;2> 3> 4> Starting recover at 29-JUN-18using target database control file instead of recovery catalogallocated channel: ORA_DISK_1channel ORA_DISK_1: SID=66 device type=DISKRMAN-05026: warning: presuming following set of tablespaces applies to specified point-in-time List of tablespaces expected to have UNDO segmentsTablespace SYSTEMTablespace UNDOTBS1 Creating automatic instance, with SID='hCed' initialization parameters used for automatic instance:db_name=PCCdb_unique_name=hCed_pitr_PCCcompatible=12.2.0db_block_size=8192db_files=200diagnostic_dest=/u01/app/oracle_system_trig_enabled=FALSEsga_target=732Mprocesses=200db_create_file_dest=/data01log_archive_dest_1='location=/data01'#No auxiliary parameter file used starting up automatic instance PCC Oracle instance started Total System Global Area 767557632 bytes Fixed Size 8625416 bytesVariable Size 213910264 bytesDatabase Buffers 541065216 bytesRedo Buffers 3956736 bytesAutomatic instance created contents of Memory Script:{# set requested point in timeset until time "to_date('2018-06-28 23:56:06','yyyy-mm-dd hh24:mi:ss')";# restore the controlfilerestore clone controlfile; # mount the controlfilesql 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 29-JUN-18allocated channel: ORA_AUX_DISK_1channel ORA_AUX_DISK_1: SID=35 device type=DISK channel ORA_AUX_DISK_1: starting datafile backup set restorechannel ORA_AUX_DISK_1: restoring control filechannel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/product/12.2.0.1/dbs/c-3164878903-20180628-01channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/product/12.2.0.1/dbs/c-3164878903-20180628-01 tag=TAG20180628T234914channel ORA_AUX_DISK_1: restored backup piece 1channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:02output file name=/data01/PCC/controlfile/o1_mf_fmcsc00p_.ctlFinished restore at 29-JUN-18 sql statement: alter database mount clone database sql statement: alter system archive log current contents of Memory Script:{# set requested point in timeset until time "to_date('2018-06-28 23:56:06','yyyy-mm-dd hh24:mi:ss')";# set destinations for recovery set and auxiliary set datafilesset newname for clone datafile 1 to new;set newname for clone datafile 4 to new;set newname for clone datafile 3 to new;set newname for clone tempfile 1 to new;# switch all tempfilesswitch clone tempfile all;# restore the tablespaces in the recovery set and the auxiliary setrestore clone datafile 1, 4, 3; 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 renamed tempfile 1 to /data01/PCC/datafile/o1_mf_temp_%u_.tmp in control file Starting restore at 29-JUN-18using channel ORA_AUX_DISK_1 channel ORA_AUX_DISK_1: starting datafile backup set restorechannel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup setchannel ORA_AUX_DISK_1: restoring datafile 00001 to /data01/PCC/datafile/o1_mf_system_%u_.dbfchannel ORA_AUX_DISK_1: restoring datafile 00004 to /data01/PCC/datafile/o1_mf_undotbs1_%u_.dbfchannel ORA_AUX_DISK_1: restoring datafile 00003 to /data01/PCC/datafile/o1_mf_sysaux_%u_.dbfchannel ORA_AUX_DISK_1: reading from backup piece /home/oracle/bak/db_01t6k254_1_1channel ORA_AUX_DISK_1: piece handle=/home/oracle/bak/db_01t6k254_1_1 tag=TAG20180628T215236channel ORA_AUX_DISK_1: restored backup piece 1channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:15Finished restore at 29-JUN-18 datafile 1 switched to datafile copyinput datafile copy RECID=4 STAMP=980038406 file name=/data01/PCC/datafile/o1_mf_system_fmcsc755_.dbfdatafile 4 switched to datafile copyinput datafile copy RECID=5 STAMP=980038406 file name=/data01/PCC/datafile/o1_mf_undotbs1_fmcsc75h_.dbfdatafile 3 switched to datafile copyinput datafile copy RECID=6 STAMP=980038406 file name=/data01/PCC/datafile/o1_mf_sysaux_fmcsc75c_.dbf contents of Memory Script:{# set requested point in timeset until time "to_date('2018-06-28 23:56:06','yyyy-mm-dd hh24:mi:ss')";# online the datafiles restored or switchedsql clone "alter database datafile 1 online";sql clone "alter database datafile 4 online";sql clone "alter database datafile 3 online";# recover and open database read onlyrecover clone database tablespace "SYSTEM", "UNDOTBS1", "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 4 online sql statement: alter database datafile 3 online Starting recover at 29-JUN-18using channel ORA_AUX_DISK_1 starting media recovery archived log for thread 1 with sequence 3 is already on disk as file /u01/app/oracle/product/12.2.0.1/dbs/arch1_3_979872889.dbfarchived log for thread 1 with sequence 4 is already on disk as file /u01/app/oracle/product/12.2.0.1/dbs/arch1_4_979872889.dbfarchived log file name=/u01/app/oracle/product/12.2.0.1/dbs/arch1_3_979872889.dbf thread=1 sequence=3archived log file name=/u01/app/oracle/product/12.2.0.1/dbs/arch1_4_979872889.dbf thread=1 sequence=4media recovery complete, elapsed time: 00:00:11Finished recover at 29-JUN-18 sql statement: alter database 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 = ''/data01/PCC/controlfile/o1_mf_fmcsc00p_.ctl'' comment=''RMAN set'' scope=spfile";shutdown clone immediate;startup clone nomount;# mount databasesql clone 'alter database mount clone database';}executing Memory Script sql statement: create spfile from memory database closeddatabase dismountedOracle instance shut down connected to auxiliary database (not started)Oracle instance started Total System Global Area 767557632 bytes Fixed Size 8625416 bytesVariable Size 213910264 bytesDatabase Buffers 541065216 bytesRedo Buffers 3956736 bytes sql statement: alter system set control_files = ''/data01/PCC/controlfile/o1_mf_fmcsc00p_.ctl'' comment= ''RMAN set'' scope=spfile Oracle instance shut down connected to auxiliary database (not started)Oracle instance started Total System Global Area 767557632 bytes Fixed Size 8625416 bytesVariable Size 213910264 bytesDatabase Buffers 541065216 bytesRedo Buffers 3956736 bytes sql statement: alter database mount clone database contents of Memory Script:{# set requested point in timeset until time "to_date('2018-06-28 23:56:06','yyyy-mm-dd hh24:mi:ss')";# set destinations for recovery set and auxiliary set datafilesset newname for datafile 5 to new;# restore the tablespaces in the recovery set and the auxiliary setrestore clone datafile 5; switch clone datafile all;}executing Memory Script executing command: SET until clause executing command: SET NEWNAME Starting restore at 29-JUN-18allocated channel: ORA_AUX_DISK_1channel ORA_AUX_DISK_1: SID=34 device type=DISK channel ORA_AUX_DISK_1: starting datafile backup set restorechannel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup setchannel ORA_AUX_DISK_1: restoring datafile 00005 to /data01/HCED_PITR_PCC/datafile/o1_mf_tbs_zyq_%u_.dbfchannel ORA_AUX_DISK_1: reading from backup piece /home/oracle/bak/db_01t6k254_1_1channel ORA_AUX_DISK_1: piece handle=/home/oracle/bak/db_01t6k254_1_1 tag=TAG20180628T215236channel ORA_AUX_DISK_1: restored backup piece 1channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:03Finished restore at 29-JUN-18 datafile 5 switched to datafile copyinput datafile copy RECID=8 STAMP=980038478 file name=/data01/HCED_PITR_PCC/datafile/o1_mf_tbs_zyq_fmcsfttn_.dbf contents of Memory Script:{# set requested point in timeset until time "to_date('2018-06-28 23:56:06','yyyy-mm-dd hh24:mi:ss')";# online the datafiles restored or switchedsql clone "alter database datafile 5 online";# recover and open resetlogsrecover clone database tablespace "TBS_ZYQ", "SYSTEM", "UNDOTBS1", "SYSAUX" delete archivelog;alter clone database open resetlogs;}executing Memory Script executing command: SET until clause sql statement: alter database datafile 5 online Starting recover at 29-JUN-18using channel ORA_AUX_DISK_1 starting media recovery archived log for thread 1 with sequence 3 is already on disk as file /u01/app/oracle/product/12.2.0.1/dbs/arch1_3_979872889.dbfarchived log for thread 1 with sequence 4 is already on disk as file /u01/app/oracle/product/12.2.0.1/dbs/arch1_4_979872889.dbfarchived log file name=/u01/app/oracle/product/12.2.0.1/dbs/arch1_3_979872889.dbf thread=1 sequence=3archived log file name=/u01/app/oracle/product/12.2.0.1/dbs/arch1_4_979872889.dbf thread=1 sequence=4media recovery complete, elapsed time: 00:00:01Finished recover at 29-JUN-18 database opened contents of Memory Script:{# create directory for datapump importsql "create or replace directory TSPITR_DIROBJ_DPDIR as ''/data01''";# create directory for datapump exportsql clone "create or replace directory TSPITR_DIROBJ_DPDIR as ''/data01''";}executing Memory Script sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/data01'' sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/data01'' Performing export of tables... EXPDP> Starting "SYS"."TSPITR_EXP_hCed_Dhxe": 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 "ZYQ"."T_RECOVER_TEST" 9.324 MB 72652 rows EXPDP> Master table "SYS"."TSPITR_EXP_hCed_Dhxe" successfully loaded/unloaded EXPDP> ****************************************************************************** EXPDP> Dump file set for SYS.TSPITR_EXP_hCed_Dhxe is: EXPDP> /data01/tspitr_hCed_71393.dmp EXPDP> Job "SYS"."TSPITR_EXP_hCed_Dhxe" successfully completed at Fri Jun 29 00:55:41 2018 elapsed 0 00:00:35Export completed contents of Memory Script:{# shutdown clone before importshutdown clone abort}executing Memory Script Oracle instance shut down Performing import of tables... IMPDP> Master table "SYS"."TSPITR_IMP_hCed_Arhm" successfully loaded/unloaded IMPDP> Starting "SYS"."TSPITR_IMP_hCed_Arhm": IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA IMPDP> . . imported "ZYQ"."T_RECOVER_TEST_MAP" 9.324 MB 72652 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_hCed_Arhm" successfully completed at Fri Jun 29 00:56:43 2018 elapsed 0 00:00:45Import completed Removing automatic instanceAutomatic instance removedauxiliary instance file /data01/PCC/datafile/o1_mf_temp_fmcsd5ny_.tmp deletedauxiliary instance file /data01/HCED_PITR_PCC/onlinelog/o1_mf_3_fmcsg3y6_.log deletedauxiliary instance file /data01/HCED_PITR_PCC/onlinelog/o1_mf_2_fmcsg07o_.log deletedauxiliary instance file /data01/HCED_PITR_PCC/onlinelog/o1_mf_1_fmcsfzr1_.log deletedauxiliary instance file /data01/HCED_PITR_PCC/datafile/o1_mf_tbs_zyq_fmcsfttn_.dbf deletedauxiliary instance file /data01/PCC/datafile/o1_mf_sysaux_fmcsc75c_.dbf deletedauxiliary instance file /data01/PCC/datafile/o1_mf_undotbs1_fmcsc75h_.dbf deletedauxiliary instance file /data01/PCC/datafile/o1_mf_system_fmcsc755_.dbf deletedauxiliary instance file /data01/PCC/controlfile/o1_mf_fmcsc00p_.ctl deletedauxiliary instance file tspitr_hCed_71393.dmp deletedFinished recover at 29-JUN-18 RMAN>

验证

SQL> select count(*) from T_RECOVER_TEST_MAP; COUNT(*)---------- 72652

总结

(1) 根据为恢复指定的时间点确定哪个备份包含需要恢复的表或表分区。

(2) 确定目标主机上是否有足够的空间来创建将在表或分区恢复过程中使用的辅助实例。如果所需空间不可用,则 RMAN 会显示错误并退出恢复操作。

(3) 在目标主机上创建一个辅助数据库,并将指定的表或表分区恢复到此辅助数据库中,直到指定的时间点。您可以指定恢复的数据文件存储在辅助数据库中的目标主机上的位置。

(4) 创建包含恢复的表或表分区的数据泵导出转储文件。您可以指定用于存储恢复的表或表分区的元数据的数据泵导出转储文件的名称和位置。

(5) (可选)将 Data Pump 导出转储文件导入到目标实例中。您可以选择不将包含恢复的表或表分区的导出转储文件导入目标数据库。如果不将导出转储文件作为恢复过程的一部分导入,则必须稍后使用“数据泵导入”实用程序手动导入它。

注:如果在导入操作期间发生错误,RMAN 不会在表恢复结束时删除导出转储文件。这使您能够手动导入转储文件。

(6) (可选)重命名目标数据库中已恢复的表或表分区。您还可以将恢复的对象导入到与最初存在的表空间或模式不同的表空间或模式中。12.2 开始,schema 可不同

注:当您使用 REMAP 选项时,不会导入任何指定的约束和索引。这是为了避免与现有表格的名称冲突。

如果不希望表被导入,只需要恢复出 dmp 文件,可以使用 notableimport 选项,命令如下:

RECOVER TABLE SCOTT.EMP, SCOTT.DEPT UNTIL TIME 'SYSDATE-1' AUXILIARY DESTINATION '/tmp/oracle/recover' DATAPUMP DESTINATION '/tmp/recover/dumpfiles' DUMP FILE 'emp_dept_exp_dump.dat' NOTABLEIMPORT;

如果需要将表恢复至另外的 schema,命令如下:

RECOVER TABLE HR.DEPARTMENTS, SH.CHANNELSUNTIL TIME 'SYSDATE – 1' AUXILIARY DESTINATION '/tmp/auxdest'REMAP TABLE hr.departments:example.new_departments, sh.channels:example.new_channels;

原创:周玉其 。

投稿:有投稿意向技术人请在公众号对话框留言。

转载:意向文章下方留言。

原文发布于微信公众号 - 数据和云(OraNews)

原文发表时间:2018-08-14

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏乐沙弥的世界

Oracle 基于 RMAN 的不完全恢复(incomplete recovery by RMAN)

      Oracle 数据库可以实现数据库不完全恢复与完全恢复。完全恢复是将数据库恢复到最新时刻,也就是无损恢复,保证数据库无丢失的恢复。而不完全恢复则是根...

15410
来自专栏乐沙弥的世界

ORA-39126 KUPW$WORKER.PUT_DDLS [TABLE_STATISTICS]错误

--======================================================= -- ORA-39126 KUPW$WOR...

17840
来自专栏程序猿

Oracle_12C的新特性

这里我们来领略下Tom眼中的12个特性增强: ? #1 Even better PL/SQL from SQL, 直接在SQL中嵌入PL/SQL对象并运行,猜测...

31490
来自专栏杨建荣的学习笔记

sed+awk模拟简单sql查询(26天)

经常需要用sqlplus去查询一些数据字典类型的数据,这些数据量不大,而且需要环境之间都是一样的,所以就想使用离线查询的方式,把那些数据以平面文件的形式存放,...

371120
来自专栏tiane12

DB2备份恢复流程

13710
来自专栏大内老A

WCF版的PetShop之二:模块中的层次划分[提供源代码下载]

上一篇文章主要讨论的是PetShop的模块划分,在这一篇文章中我们来讨论在一个模块中如何进行层次划分。模块划分应该是基于功能的,一个模块可以看成是服务于某项功能...

258100
来自专栏杨建荣的学习笔记

一个MySQL死锁问题的复现

很久之前有一个同事问我一个关于死锁的问题,一直在拖这个事情,总算找了空来看看。 这个环境的事务隔离级别是RR,仔细看了下问题描述和背景,发现还真不是一...

45590
来自专栏数据和云

续:跨平台版本迁移之 XTTS 方案操作指南

运行数据库对比脚本,通过创建 dblink,运行相关的数据库对象比对脚本。这里我们主要比对了存储过程,函数,触发器,试图,索引,表等等。

23440
来自专栏乐沙弥的世界

Linux/Unix shell 自动导入Oracle数据库

      使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份...

25320
来自专栏乐沙弥的世界

ORA-02409:超时:分布式事务处理等待锁定ORA-02063

ORA-02409:超时:分布式事务处理等待锁定ORA-02063 一、错误现象与环境     前端应用程序运行时出现下面的错误提示: 事件添加失败:O...

13120

扫码关注云+社区

领取腾讯云代金券