Automatic Tablespace Point-in-Time Recovery(TSPIR) in Oracle 11gR2

Automatic tablespace point-in-time recovery is very useful in the following situations:

1.  You want to recover just one tablespace to the point in time other than rest of the tablespaces.

2. You want to recover dropped tablespace.

3. You want to recover data in table that was lost after changing structure of the table. Note that you are not able to use Flashback Table feature to recover data that was lost after changing table structure, it is not permitted.

TSPITR has a number of restrictions, a quick summary of which follows:

1.  You can’t restore tablespaces with objects owned by SYS. Any tablespace with replicated master tables cannot be recovered with TSPITR.

2. Tablespaces with snapshot logs are not supported.

3. You can’t restore tablespaces that contain rollback segments.

4. If an object within the tablespace to be recovered has one of the following types, then TSPITR is not supported:

    1. VARRAY
    2. Nested tables 
    3. External files

5. The current physical structure of the undo and rollback segments in the target database
must be unchanged between the point you wish to recover to and the current point.

6. Once you have completed TSPITR on a given tablespace, all previous backups of that tablespace are no longer usable for future TSPITR recoveries of that tablespace. So you should backup your tablespace after TSPITR in case you need to run another TSPITR.

So, let’s start automatic TSPITR…Note that we are not using recovery catalog.

1. Identify the time to which you want to recover your tablespace. Don’t misidentify the time or you will not be able to retry the recovery.(As we mentioned in the section 6 of TSPITR restrictions, all previous backups will no longer be usable for future TSPITR)

2. Make sure that objects in that tablespace are self-contained.

Let’s say we have the tablesapace called “TEST”.  It contains table “TEST_TABLE” but indexes on that table resides on another tablespace called “TEST_IDX”. In this case, if you want to recover “TEST” tablespace to some point-in-time , then you must also recover “TEST_IDX” tablespace to the same point-in-time(or you will loose indexes).

To identify dependent tablesapaces, run the following:

SQL> select obj1_owner
       ,obj1_name
       ,obj1_type
       ,ts2_name
       ,reason
from sys.ts_pitr_check
where ( ts1_name in (‘TEST’)
and ts2_name not in (‘TEST’) )
or ( ts1_name not in (‘TEST’)
and ts2_name in (‘TEST’) );

OBJ1_OWNER OBJ1_NAME  OBJ1_TYPE TS2_NAME REASON
TEST_USR       TEST_TABLE TABLE           TEST_IDX Tables and associated indexes not fully contained in the recovery set

This would return no rows if there were no dependencies. If there were any, you would see a
row describing each of them, as shown above.

In our case, there is an index associated with TEST_TABLE table and it resides in TEST_IDX tablespace. We should restore it also.

3. Identify what objects will be dropped after recovering tablespace.

Run the following,

SQL> select *
from ts_pitr_objects_to_be_dropped
where tablespace_name=’TEST’

OWNER NAME CREATION_TIME TABLESPACE_NAME
TEST_USR TEST_TABLE 4/19/2013 12:19:18 PM TEST

For example, if you are going to recover tablespace to 4/19/2013 12:00:00PM  then TEST_TABLE will be dropped.

4. Start test case.

Now we create 2 tablespaces TEST and TEST_IDX . Create TEST_USR user. Create TEST_TABLE table in TEST tablespace and TEST_INDEX in TEST_IDX. Fill table with data. Backup database. Before dropping table we save current SCN and then  drop table. We will do TBSPIR until the saved SCN.

Step 0 :  Clean the previous failed TSPIR

SQL> exec dbms_backup_restore.manageauxinstance (‘instance_name’,1) ;

instance_name will be generated automatically, and you should see it in RMAN output.

Step 1. Create TEST and TEST_IDX tablespaces.

SQL> CREATE TABLESPACE TEST DATAFILE
  ‘D:\APP\ORACLE\ORADATA\ORCL\TEST.DBF’ SIZE 104857600
  AUTOEXTEND ON NEXT 1048576 MAXSIZE 32767M;
 
  SQL> CREATE TABLESPACE TEST_IDX DATAFILE
  ‘D:\APP\ORACLE\ORADATA\ORCL\TEST_IDX.DBF’ SIZE 104857600
  AUTOEXTEND ON NEXT 1048576 MAXSIZE 32767M; 

Step 2. Create TEST_USR user, grant privileges.

SQL> CREATE USER TEST_USR IDENTIFIED BY TEST
DEFAULT TABLESPACE TEST;

SQL> GRANT CONNECT,RESOURCE TO TEST_USR;

Step 3. Create TEST_TABLE table.

SQL> CREATE TABLE TEST_USR.TEST_TABLE(A DATE);

Step 4. Create TEST_INDEX index.

SQL> CREATE INDEX TEST_USR.TEST_INDEX
ON  TEST_USR.TEST_TABLE(A)
TABLESPACE TEST_IDX;

Step 5. Fill data.

SQL> INSERT INTO TEST_USR.TEST_TABLE
VALUES(SYSDATE);

SQL> INSERT INTO TEST_USR.TEST_TABLE
VALUES(SYSDATE);

SQL> COMMIT;

Step 6.

SQL> SELECT *
FROM TEST_USR.TEST_TABLE

A
4/19/2013 12:20:16 PM
4/19/2013 12:20:16 PM

Step 7. Backup database.

RMAN> backup database plus archivelog;

Step 8.  Note the current SCN.

SQL> SELECT CURRENT_SCN
FROM V$DATABASE

CURRENT_SCN
2091595

Step 9. Drop table.

DROP TABLE TEST_USR.TEST_TABLE

Step 10. Create tables in SYSTEM and TEST_IDX tablespaces.

SQL> CREATE TABLE TEST_USR.TEST_USR_SYSTEM(A DATE)
TABLESPACE SYSTEM;

SQL> INSERT INTO TEST_USR.TEST_USR_SYSTEM VALUES(SYSDATE);

SQL> COMMIT;

SQL> CREATE TABLE TEST_USR.TEST_USR_TEST_IDX(A DATE)
TABLESPACE TEST_IDX;

SQL> INSERT INTO TEST_USR.TEST_USR_TEST_IDX VALUES(SYSDATE);

SQL> COMMIT;

This is just for to see that just TEST and TEST_IDX tablespaces are affected. And not SYSTEM or any other tablespaces. Smile

Step 11. Do TSPIR.

Note: Auxiliary destination must already exist and oracle software owner must be able to write into it.

!!! You should recover dependent tablespaces also , or data in these tablespaces will be lost.

For example: If you just recover TEST tablespace, then index on TEST_TABLE will be lost(resides in TEST_IDX tablespasce)… Note also that if you try to recover just TEST_IDX tablespace it will cause the following error:

The transportable set is not self-contained, violation list is Index TEST_USR.TEST_INDEX in tablespace TEST_IDX points to table TEST_USR.TEST_TABLE in tablespace TEST.

!!! Recoverable tablespaces must not contain SYS objects, or it will fail.

!!! Identify the exact point of time or after recovering tablespace, you will not be able to re-run TSPIR by existing backups.

After recovering tablespaces, if you run list backup command:

RMAN> list backup;

File LV Type Ckp SCN    Ckp Time            Name
—- — —- ———- ——————- —-
1       Full 2084320    19-04-2013 16:52:21 D:\APP\ORACLE\ORADATA\ORCL\SYSTEM01.DBF
2       Full 2084320    19-04-2013 16:52:21 D:\APP\ORACLE\ORADATA\ORCL\SYSAUX01.DBF
3       Full 2084320    19-04-2013 16:52:21 D:\APP\ORACLE\ORADATA\ORCL\UNDOTBS01.DBF
4       Full 2084320    19-04-2013 16:52:21 D:\APP\ORACLE\ORADATA\ORCL\USERS01.DBF
5       Full 2084320    19-04-2013 16:52:21
6       Full 2084320    19-04-2013 16:52:21

There are empty strings across recovered tablespaces.

So after TSPIR , you must re-take backup of the recovered tablespaces.

Starting TSPIR…

RMAN> recover tablespace "TEST","TEST_IDX" until scn 2091595 auxiliary destination ‘D:\oracle’;

Starting recover at 19-04-2013 17:57:28
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=17 device type=DISK
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=’uecp’

initialization parameters used for automatic instance:
db_name=ORCL
db_unique_name=uecp_tspitr_ORCL
compatible=11.2.0.0.0
db_block_size=8192
db_files=200
sga_target=280M
processes=50
db_create_file_dest=D:\oracle
log_archive_dest_1=’location=D:\oracle’
#No auxiliary parameter file used

starting up automatic instance ORCL

Oracle instance started

Total System Global Area     292933632 bytes

Fixed Size                     1374164 bytes
Variable Size                100665388 bytes
Database Buffers             184549376 bytes
Redo Buffers                   6344704 bytes
Automatic instance created
Running TRANSPORT_SET_CHECK on recovery set tablespaces
TRANSPORT_SET_CHECK completed successfully

contents of Memory Script:
{
# set requested point in time
set until  scn 2091595;
# 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 19-04-2013 17:58:39
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=59 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 D:\APP\ORACLE\FLASH_RECOVERY_AREA\ORCL\BACKUPSET\2013_04_19\O1_MF_NCSNF_TAG20130419T175122_8Q2M0XGP_.BKP
channel ORA_AUX_DISK_1: piece handle=D:\APP\ORACLE\FLASH_RECOVERY_AREA\ORCL\BACKUPSET\2013_04_19\O1_MF_NCSNF_TAG20130419T175122_8Q2M0XGP_.BKP tag=TAG20130419T175122
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
output file name=D:\ORACLE\ORCL\CONTROLFILE\O1_MF_8Q2MDJ5K_.CTL
Finished restore at 19-04-2013 17:58:41

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 2091595;
plsql <<<– tspitr_2
declare
  sqlstatement       varchar2(512);
  offline_not_needed exception;
  pragma exception_init(offline_not_needed, -01539);
begin
  sqlstatement := ‘alter tablespace ‘||  ‘"TEST"’ ||’ offline immediate’;
  krmicd.writeMsg(6162, sqlstatement);
  krmicd.execSql(sqlstatement);
exception
  when offline_not_needed then
    null;
end; >>>;
plsql <<<– tspitr_2
declare
  sqlstatement       varchar2(512);
  offline_not_needed exception;
  pragma exception_init(offline_not_needed, -01539);
begin
  sqlstatement := ‘alter tablespace ‘||  ‘"TEST_IDX"’ ||’ offline immediate’;
  krmicd.writeMsg(6162, sqlstatement);
  krmicd.execSql(sqlstatement);
exception
  when offline_not_needed then
    null;
end; >>>;
# set destinations for recovery set and auxiliary set datafiles
set newname for clone datafile  1 to new;
set newname for clone datafile  3 to new;
set newname for clone datafile  2 to new;
set newname for clone tempfile  1 to new;
set newname for datafile  5 to
"D:\APP\ORACLE\ORADATA\ORCL\TEST.DBF";
set newname for datafile  6 to
"D:\APP\ORACLE\ORADATA\ORCL\TEST_IDX.DBF";
# switch all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  1, 3, 2, 5, 6;
switch clone datafile all;
}
executing Memory Script

executing command: SET until clause

sql statement: alter tablespace "TEST" offline immediate

sql statement: alter tablespace "TEST_IDX" offline immediate

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 D:\ORACLE\ORCL\DATAFILE\O1_MF_TEMP_%U_.TMP in control file

Starting restore at 19-04-2013 17:58:58
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 D:\ORACLE\ORCL\DATAFILE\O1_MF_SYSTEM_%U_.DBF
channel ORA_AUX_DISK_1: restoring datafile 00003 to D:\ORACLE\ORCL\DATAFILE\O1_MF_UNDOTBS1_%U_.DBF
channel ORA_AUX_DISK_1: restoring datafile 00002 to D:\ORACLE\ORCL\DATAFILE\O1_MF_SYSAUX_%U_.DBF
channel ORA_AUX_DISK_1: restoring datafile 00005 to D:\APP\ORACLE\ORADATA\ORCL\TEST.DBF
channel ORA_AUX_DISK_1: restoring datafile 00006 to D:\APP\ORACLE\ORADATA\ORCL\TEST_IDX.DBF
channel ORA_AUX_DISK_1: reading from backup piece D:\APP\ORACLE\FLASH_RECOVERY_AREA\ORCL\BACKUPSET\2013_04_19\O1_MF_NNNDF_TAG20130419T175122_8Q2LYVQ3_.BKP
channel ORA_AUX_DISK_1: piece handle=D:\APP\ORACLE\FLASH_RECOVERY_AREA\ORCL\BACKUPSET\2013_04_19\O1_MF_NNNDF_TAG20130419T175122_8Q2LYVQ3_.BKP tag=TAG20130419T175122
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:05
Finished restore at 19-04-2013 18:00:04

datafile 1 switched to datafile copy
input datafile copy RECID=4 STAMP=813175204 file name=D:\ORACLE\ORCL\DATAFILE\O1_MF_SYSTEM_8Q2MF36L_.DBF
datafile 3 switched to datafile copy
input datafile copy RECID=5 STAMP=813175204 file name=D:\ORACLE\ORCL\DATAFILE\O1_MF_UNDOTBS1_8Q2MF3K3_.DBF
datafile 2 switched to datafile copy
input datafile copy RECID=6 STAMP=813175204 file name=D:\ORACLE\ORCL\DATAFILE\O1_MF_SYSAUX_8Q2MF3BV_.DBF

contents of Memory Script:
{
# set requested point in time
set until  scn 2091595;
# online the datafiles restored or switched
sql clone "alter database datafile  1 online";
sql clone "alter database datafile  3 online";
sql clone "alter database datafile  2 online";
sql clone "alter database datafile  5 online";
sql clone "alter database datafile  6 online";
# recover and open resetlogs
recover clone database tablespace  "TEST", "TEST_IDX", "SYSTEM", "UNDOTBS1", "SYSAUX" delete archivelog;
alter clone database open resetlogs;
}
executing Memory Script

executing command: SET until clause

sql statement: alter database datafile  1 online

sql statement: alter database datafile  3 online

sql statement: alter database datafile  2 online

sql statement: alter database datafile  5 online

sql statement: alter database datafile  6 online

Starting recover at 19-04-2013 18:00:06
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 15 is already on disk as file D:\APP\ORACLE\FLASH_RECOVERY_AREA\ORCL\ARCHIVELOG\2013_04_19\O1_MF_1_15_8Q2M11L9_.ARC
archived log for thread 1 with sequence 16 is already on disk as file D:\APP\ORACLE\FLASH_RECOVERY_AREA\ORCL\ARCHIVELOG\2013_04_19\O1_MF_1_16_8Q2MDSHL_.ARC
archived log file name=D:\APP\ORACLE\FLASH_RECOVERY_AREA\ORCL\ARCHIVELOG\2013_04_19\O1_MF_1_15_8Q2M11L9_.ARC thread=1 sequence=15
archived log file name=D:\APP\ORACLE\FLASH_RECOVERY_AREA\ORCL\ARCHIVELOG\2013_04_19\O1_MF_1_16_8Q2MDSHL_.ARC thread=1 sequence=16
media recovery complete, elapsed time: 00:00:02
Finished recover at 19-04-2013 18:00:10

database opened

contents of Memory Script:
{
# make read only the tablespace that will be exported
sql clone ‘alter tablespace  "TEST" read only’;
sql clone ‘alter tablespace  "TEST_IDX" read only’;
# create directory for datapump import
sql "create or replace directory TSPITR_DIROBJ_DPDIR as ”
D:\oracle”";
# create directory for datapump export
sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as ”
D:\oracle”";
}
executing Memory Script

sql statement: alter tablespace  "TEST" read only

sql statement: alter tablespace  "TEST_IDX" read only

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ”D:\oracle”

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ”D:\oracle”

Performing export of metadata…
   EXPDP> Starting "SYS"."TSPITR_EXP_uecp":
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   EXPDP> Master table "SYS"."TSPITR_EXP_uecp" successfully loaded/unloaded
   EXPDP> ******************************************************************************
   EXPDP> Dump file set for SYS.TSPITR_EXP_uecp is:
   EXPDP>   D:\ORACLE\TSPITR_UECP_36510.DMP
   EXPDP> ******************************************************************************
   EXPDP> Datafiles required for transportable tablespace TEST:
   EXPDP>   D:\APP\ORACLE\ORADATA\ORCL\TEST.DBF
   EXPDP> Datafiles required for transportable tablespace TEST_IDX:
   EXPDP>   D:\APP\ORACLE\ORADATA\ORCL\TEST_IDX.DBF
   EXPDP> Job "SYS"."TSPITR_EXP_uecp" successfully completed at 18:01:30
Export completed

contents of Memory Script:
{
# shutdown clone before import
shutdown clone immediate
# drop target tablespaces before importing them back
sql ‘drop tablespace  "TEST" including contents keep datafiles’;
sql ‘drop tablespace  "TEST_IDX" including contents keep datafiles’;
}
executing Memory Script

database closed
database dismounted
Oracle instance shut down

sql statement: drop tablespace  "TEST" including contents keep datafiles

sql statement: drop tablespace  "TEST_IDX" including contents keep datafiles

Performing import of metadata…
   IMPDP> Master table "SYS"."TSPITR_IMP_uecp" successfully loaded/unloaded
   IMPDP> Starting "SYS"."TSPITR_IMP_uecp":
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   IMPDP> Job "SYS"."TSPITR_IMP_uecp" successfully completed at 18:01:57
Import completed

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

sql statement: alter tablespace  "TEST" read write

sql statement: alter tablespace  "TEST" offline

sql statement: alter tablespace  "TEST_IDX" read write

sql statement: alter tablespace  "TEST_IDX" offline

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

Removing automatic instance
Automatic instance removed
auxiliary instance file D:\ORACLE\ORCL\DATAFILE\O1_MF_TEMP_8Q2MHOX7_.TMP deleted
auxiliary instance file D:\ORACLE\ORCL\ONLINELOG\O1_MF_3_8Q2MHJJJ_.LOG deleted
auxiliary instance file D:\ORACLE\ORCL\ONLINELOG\O1_MF_2_8Q2MHGMY_.LOG deleted
auxiliary instance file D:\ORACLE\ORCL\ONLINELOG\O1_MF_1_8Q2MHC0J_.LOG deleted
auxiliary instance file D:\ORACLE\ORCL\DATAFILE\O1_MF_SYSAUX_8Q2MF3BV_.DBF deleted
auxiliary instance file D:\ORACLE\ORCL\DATAFILE\O1_MF_UNDOTBS1_8Q2MF3K3_.DBF deleted
auxiliary instance file D:\ORACLE\ORCL\DATAFILE\O1_MF_SYSTEM_8Q2MF36L_.DBF deleted
auxiliary instance file D:\ORACLE\ORCL\CONTROLFILE\O1_MF_8Q2MDJ5K_.CTL deleted
Finished recover at 19-04-2013 18:02:05

RMAN>

Step 12. Make TEST and TEST_IDX tablespaces online.

ALTER TABLESPACE TEST ONLINE;

ALTER TABLESPACE TEST_IDX ONLINE;

Step 13.  Check what was and wasn’t recovered.

SQL> select *
from TEST_USR.TEST_USR_SYSTEM;

A
4/19/2013 5:53:01 PM

SQL> select *
from TEST_USR.TEST_USR_TEST_IDX;

ORA-00942: table or view does not exist

SQL> select *
from TEST_USR.TEST_TABLE;

A
4/19/2013 5:50:44 PM
4/19/2013 5:50:44 PM

 

So just “TEST” and “TEST_IDX” tablespaces are affected, nice Smile

Good Luck!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: