基于TSPITR恢复表空间

使用场景

官方文档
Performing RMAN Tablespace Point-in-Time Recovery (TSPITR)

1、恢复更改表结构的数据定义语言(DDL)操作后丢失的数据。不能使用Flashback Table将表倒带到结构更改点(如截断表操作)之前。
2、使用PURGE选项在删除表后恢复该表。
3、从表的逻辑损坏中恢复。
4、恢复已删除的表空间。事实上,即使不使用恢复目录,RMAN也可以对丢弃的表空间执行TSPITR。

RMAN全备

[oracle@host01 ~]$ rman target sys/oracle@prodcdb

Recovery Manager: Release 12.1.0.2.0 - Production on Fri Aug 25 08:02:31 2023

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

connected to target database: PRODCDB (DBID=2891862819)

RMAN> backup as compressed backupset database INCLUDE CURRENT CONTROLFILE plus archivelog;

删除表空间

[oracle@host01 ~]$ sqlplus sys/oracle@pdbprod2 as sysdba
SYS@pdbprod2> select TABLESPACE_NAME,STATUS from dba_tablespaces;

TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
SYSAUX                         ONLINE
TEMP                           ONLINE
USERS                          ONLINE
PITR_TBS                       ONLINE
SYS@pdbprod2> drop tablespace PITR_TBS including contents and datafiles;
SYS@pdbprod2> select TABLESPACE_NAME,STATUS from dba_tablespaces;

TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
SYSAUX                         ONLINE
TEMP                           ONLINE
USERS                          ONLINE

查看备份

可以看到PDBPROD2备份时的scn=2907073,在全备过程中,备份完数据文件继续备份控制文件,所以控制文件scn号比数据文件的scn号要大。找到比数据文件scn号2907073大一点的控制文件scn号2907166,这个控制文件中记录了数据文件备份,比2907073小的scn=2870109控制文件中没有记录备份过的数据文件

1、查看PDBPROD2备份
RMAN> list backup of pluggable database PDBPROD2;

using target database control file instead of recovery catalog

List of Backup Sets
===================

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
36      Full    154.99M    DISK        00:00:30     25-AUG-23      
        BP Key: 36   Status: AVAILABLE  Compressed: YES  Tag: TAG20230825T061409
        Piece Name: /u01/app/oracle/fast_recovery_area/PRODCDB/22B37EB3400244D2E053650200C0FD0A/backupset/2023_08_25/o1_mf_nnndf_TAG20230825T061409_lghovqs4_.bkp
  List of Datafiles in backup set 36
  Container ID: 4, PDB Name: PDBPROD2
  File LV Type Ckp SCN    Ckp Time  Name
  ---- -- ---- ---------- --------- ----
  10      Full 2907073    25-AUG-23 /u01/app/oracle/oradata/PRODCDB/PDBPROD2/system01.dbf
  11      Full 2907073    25-AUG-23 /u01/app/oracle/oradata/PRODCDB/PDBPROD2/sysaux01.dbf
  12      Full 2907073    25-AUG-23 /u01/app/oracle/oradata/PRODCDB/PDBPROD2/PDBPROD2_users01.dbf
  30      Full 2907073    25-AUG-23 
2、查看控制文件备份
RMAN> list backup of controlfile;
List of Backup Sets
===================
BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
28      Full    17.48M     DISK        00:00:00     24-AUG-23      
        BP Key: 28   Status: AVAILABLE  Compressed: NO  Tag: TAG20230824T235415
        Piece Name: /u01/app/oracle/fast_recovery_area/PRODCDB/autobackup/2023_08_24/o1_mf_s_1145750055_lggz975g_.bkp
  Control File Included: Ckp SCN: 2865986      Ckp time: 24-AUG-23

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
29      Full    17.48M     DISK        00:00:02     25-AUG-23      
        BP Key: 29   Status: AVAILABLE  Compressed: NO  Tag: TAG20230825T001420
        Piece Name: /u01/app/oracle/fast_recovery_area/PRODCDB/autobackup/2023_08_25/o1_mf_s_1145751260_lgh0gy0l_.bkp
  Control File Included: Ckp SCN: 2870109      Ckp time: 25-AUG-23

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
38      Full    1.08M      DISK        00:00:04     25-AUG-23      
        BP Key: 38   Status: AVAILABLE  Compressed: YES  Tag: TAG20230825T061409
        Piece Name: /u01/app/oracle/fast_recovery_area/PRODCDB/backupset/2023_08_25/o1_mf_ncnnf_TAG20230825T061409_lghoyc07_.bkp
  Control File Included: Ckp SCN: 2907166      Ckp time: 25-AUG-23

BS Key  Type LV Size       Device Type Elapsed Time Completion Time
------- ---- -- ---------- ----------- ------------ ---------------
41      Full    17.48M     DISK        00:00:00     25-AUG-23      
        BP Key: 41   Status: AVAILABLE  Compressed: NO  Tag: TAG20230825T062103
        Piece Name: /u01/app/oracle/fast_recovery_area/PRODCDB/autobackup/2023_08_25/o1_mf_s_1145773263_lghoyh5p_.bkp
  Control File Included: Ckp SCN: 2907188      Ckp time: 25-AUG-23

恢复失败

尝试使用比2907073小的scn=2870109控制文件恢复,恢复失败,因为控制文件中没有记录备份的数据文件
[oracle@host01 ~]$ mkdir -p /u01/app/oracle/auxiliary

RMAN> RECOVER TABLESPACE PDBPROD2:PITR_TBS UNTIL SCN 2870109 AUXILIARY DESTINATION '/u01/app/oracle/auxiliary';

Starting recover at 25-AUG-23
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 UNDOTBS1

Creating automatic instance, with SID='pvFe'

initialization parameters used for automatic instance:
db_name=PRODCDB
db_unique_name=pvFe_pitr_PDBPROD2_PRODCDB
compatible=12.1.0.2.0
db_block_size=8192
db_files=200
diagnostic_dest=/u01/app/oracle/product/12.1.0/dbhome_1
_system_trig_enabled=FALSE
db_domain=example.com
sga_target=800M
processes=200
db_create_file_dest=/u01/app/oracle/auxiliary
log_archive_dest_1='location=/u01/app/oracle/auxiliary'
enable_pluggable_database=true
_clone_one_pdb_recovery=true
#No auxiliary parameter file used


starting up automatic instance PRODCDB

Oracle instance started

Total System Global Area     838860800 bytes

Fixed Size                     2929936 bytes
Variable Size                230689520 bytes
Database Buffers             599785472 bytes
Redo Buffers                   5455872 bytes
Automatic instance created


List of tablespaces that have been dropped from the target database:
Tablespace PDBPROD2:PITR_TBS

contents of Memory Script:
{
# set requested point in time
set until  scn 2870109;
# 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';
# avoid unnecessary autobackups for structural changes during TSPITR
sql 'begin dbms_backup_restore.AutoBackupFlag(FALSE); end;';
}
executing Memory Script

executing command: SET until clause

Starting restore at 25-AUG-23
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=12 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/app/oracle/fast_recovery_area/PRODCDB/autobackup/2023_08_25/o1_mf_s_1145751260_lgh0gy0l_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/PRODCDB/autobackup/2023_08_25/o1_mf_s_1145751260_lgh0gy0l_.bkp tag=TAG20230825T001420
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/u01/app/oracle/auxiliary/PRODCDB/controlfile/o1_mf_lghprxrb_.ctl
Finished restore at 25-AUG-23

sql statement: alter database mount clone database

sql statement: alter system archive log current

sql statement: begin dbms_backup_restore.AutoBackupFlag(FALSE); end;

contents of Memory Script:
{
# set requested point in time
set until  scn 2870109;
# set destinations for recovery set and auxiliary set datafiles
set newname for clone datafile  1 to new;
set newname for clone datafile  5 to new;
set newname for clone datafile  3 to new;
set newname for clone datafile  10 to new;
set newname for clone datafile  11 to new;
set newname for clone tempfile  1 to new;
set newname for clone tempfile  4 to new;
set newname for datafile  30 to 
 "/u01/app/oracle/oradata/PRODCDB/PDBPROD2/PITR_TBS01.dbf";
# switch all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  1, 5, 3, 10, 11, 30;
 
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 /u01/app/oracle/auxiliary/PRODCDB/datafile/o1_mf_temp_%u_.tmp in control file
renamed tempfile 4 to /u01/app/oracle/auxiliary/PRODCDB/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 25-AUG-23
using channel ORA_AUX_DISK_1

One or more auxiliary set of datafiles could not be removed

Removing automatic instance
shutting down automatic instance 
Oracle instance shut down
Automatic instance removed
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/controlfile/o1_mf_lghprxrb_.ctl deleted
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 08/25/2023 06:35:16
RMAN-03015: error occurred in stored script Memory Script
RMAN-06026: some targets not found - aborting restore
RMAN-06023: no backup or copy of datafile 30 found to restore

恢复成功

RMAN> RECOVER TABLESPACE PDBPROD2:PITR_TBS UNTIL SCN 2907166 AUXILIARY DESTINATION '/u01/app/oracle/auxiliary';

Starting recover at 25-AUG-23
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 UNDOTBS1

Creating automatic instance, with SID='pvFe'

initialization parameters used for automatic instance:
db_name=PRODCDB
db_unique_name=pvFe_pitr_PDBPROD2_PRODCDB
compatible=12.1.0.2.0
db_block_size=8192
db_files=200
diagnostic_dest=/u01/app/oracle/product/12.1.0/dbhome_1
_system_trig_enabled=FALSE
db_domain=example.com
sga_target=800M
processes=200
db_create_file_dest=/u01/app/oracle/auxiliary
log_archive_dest_1='location=/u01/app/oracle/auxiliary'
enable_pluggable_database=true
_clone_one_pdb_recovery=true
#No auxiliary parameter file used


starting up automatic instance PRODCDB

Oracle instance started

Total System Global Area     838860800 bytes

Fixed Size                     2929936 bytes
Variable Size                230689520 bytes
Database Buffers             599785472 bytes
Redo Buffers                   5455872 bytes
Automatic instance created


List of tablespaces that have been dropped from the target database:
Tablespace PDBPROD2:PITR_TBS

contents of Memory Script:
{
# set requested point in time
set until  scn 2870109;
# 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';
# avoid unnecessary autobackups for structural changes during TSPITR
sql 'begin dbms_backup_restore.AutoBackupFlag(FALSE); end;';
}
executing Memory Script

executing command: SET until clause

Starting restore at 25-AUG-23
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=12 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/app/oracle/fast_recovery_area/PRODCDB/autobackup/2023_08_25/o1_mf_s_1145751260_lgh0gy0l_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/PRODCDB/autobackup/2023_08_25/o1_mf_s_1145751260_lgh0gy0l_.bkp tag=TAG20230825T001420
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/u01/app/oracle/auxiliary/PRODCDB/controlfile/o1_mf_lghprxrb_.ctl
Finished recover at 25-AUG-23

database opened

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

sql statement: alter pluggable database  PDBPROD2 open

contents of Memory Script:
{
# make read only the tablespace that will be exported
sql clone 'PDBPROD2' 'alter tablespace 
 PITR_TBS read only';
# create directory for datapump import
sql 'PDBPROD2' "create or replace directory 
TSPITR_DIROBJ_DPDIR as ''
/u01/app/oracle/auxiliary''";
# create directory for datapump export
sql clone 'PDBPROD2' "create or replace directory 
TSPITR_DIROBJ_DPDIR as ''
/u01/app/oracle/auxiliary''";
}
executing Memory Script

sql statement: alter tablespace  PITR_TBS read only

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/u01/app/oracle/auxiliary''

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/u01/app/oracle/auxiliary''

Performing export of metadata...
   EXPDP> Starting "SYS"."TSPITR_EXP_hfCz_qnmn":  
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/GRANT/OWNER_GRANT/OBJECT_GRANT
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE_STATISTICS
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/MARKER
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   EXPDP> Master table "SYS"."TSPITR_EXP_hfCz_qnmn" successfully loaded/unloaded
   EXPDP> ******************************************************************************
   EXPDP> Dump file set for SYS.TSPITR_EXP_hfCz_qnmn is:
   EXPDP>   /u01/app/oracle/auxiliary/tspitr_hfCz_87402.dmp
   EXPDP> ******************************************************************************
   EXPDP> Datafiles required for transportable tablespace PITR_TBS:
   EXPDP>   /u01/app/oracle/oradata/PRODCDB/PDBPROD2/PITR_TBS01.dbf
   EXPDP> Job "SYS"."TSPITR_EXP_hfCz_qnmn" successfully completed at Fri Aug 25 06:42:03 2023 elapsed 0 00:00:37
Export completed


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

Oracle instance shut down

Performing import of metadata...
   IMPDP> Master table "SYS"."TSPITR_IMP_hfCz_ekja" successfully loaded/unloaded
   IMPDP> Starting "SYS"."TSPITR_IMP_hfCz_ekja":  
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/GRANT/OWNER_GRANT/OBJECT_GRANT
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE_STATISTICS
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/STATISTICS/MARKER
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   IMPDP> Job "SYS"."TSPITR_IMP_hfCz_ekja" successfully completed at Fri Aug 25 06:42:16 2023 elapsed 0 00:00:05
Import completed


contents of Memory Script:
{
# make read write and offline the imported tablespaces
sql 'PDBPROD2' 'alter tablespace 
 PITR_TBS read write';
sql 'PDBPROD2' 'alter tablespace 
 PITR_TBS offline';
# enable autobackups after TSPITR is finished
sql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;';
}
executing Memory Script

sql statement: alter tablespace  PITR_TBS read write

sql statement: alter tablespace  PITR_TBS offline

sql statement: begin dbms_backup_restore.AutoBackupFlag(TRUE); end;

Removing automatic instance
Automatic instance removed
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/datafile/o1_mf_temp_lghq49fb_.tmp deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/datafile/o1_mf_temp_lghq453l_.tmp deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/onlinelog/o1_mf_3_lghq42kb_.log deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/onlinelog/o1_mf_2_lghq41y1_.log deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/onlinelog/o1_mf_1_lghq41f3_.log deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/datafile/o1_mf_sysaux_lghq2v3n_.dbf deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/datafile/o1_mf_system_lghq2v5s_.dbf deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/datafile/o1_mf_sysaux_lghpzklk_.dbf deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/datafile/o1_mf_undotbs1_lghpzkjg_.dbf deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/datafile/o1_mf_system_lghpzkl1_.dbf deleted
auxiliary instance file /u01/app/oracle/auxiliary/PRODCDB/controlfile/o1_mf_lghpz85x_.ctl deleted
auxiliary instance file tspitr_hfCz_87402.dmp deleted
Finished recover at 25-AUG-23

online表空间 

SYS@pdbprod2> select TABLESPACE_NAME,STATUS from dba_tablespaces;

TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
SYSAUX                         ONLINE
TEMP                           ONLINE
USERS                          ONLINE
PITR_TBS                       OFFLINE

SYS@pdbprod2> alter tablespace PITR_TBS online;

Tablespace altered.

SYS@pdbprod2> select TABLESPACE_NAME,STATUS from dba_tablespaces;

TABLESPACE_NAME                STATUS
------------------------------ ---------
SYSTEM                         ONLINE
SYSAUX                         ONLINE
TEMP                           ONLINE
USERS                          ONLINE
PITR_TBS                       ONLINE
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值