Tuesday, February 24, 2015

Table Point in Time Recovery using RMAN

Prior to 12c, in case a table or partition is dropped accidentally, there are certain ways to perform the table/partition recovery which includes Flashback database, database point in time recovery and tablespace point in time recovery. 
Starting 12c we can perform point-in-time recovery for a single table
or partition; using RMAN. This new feature enables us to avoid any full database or tablespace point-in-time recovery to recover a dropped table. We need less time, space and resources to perform table recovery using this method.

One point to note is that in case of tablespace point in time recovery, it can be done only if tablepsace is self-contained - and in case of a single table/partition point-in-time recovery, obviously this restriction does not apply.

Flashback drop table may not work if recycle bin has been purged.

I will explain a scenario of a table drop and then its recovery in the following

Create a new user and a table in the schema with some data
SQL> create user c##salman identified by salman;

User created.

SQL> grant dba to c##salman;

Grant succeeded.

Enter user-name: c##salman/salman
Last Successful login time: Thu Jan 15 2015 15:49:25 +08:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, Automatic Storage Management, OLAP, Advanced Analytics
and Real Application Testing options

SQL> create table test as select * from dba_objects;

Table created.

SQL> alter table test move tablespace users;

Table altered.

SQL> insert into test select * from test;

91103 rows created.

SQL> commit;

Commit complete.

SQL> select count(*) from test;

  COUNT(*)
----------
    182206

SQL>

Take Backup
Assuming database is in archivelog mode. Take a backup of tablespace USERS which has this table in it. Alternatively a full database backup can also be taken.
RMAN> BACKUP FORMAT '/u01/bakcup/back_%u.bak' TABLESPACE USERS;

Drop the Table
Drop the table and note the time. Current SCN can also be noted as we can perform table point in time recovery based on time or SCN. In real time scenario, exact time of table drop may not be known so we would need to be careful in guessing the time.
SQL> drop table test;

Table dropped.

SQL> select sysdate from dual;

SYSDATE
------------------
16-JAN-15 12:25:02

Perform the recovery using RMAN.
RMAN> RECOVER TABLE 'C##SALMAN'.'TEST'  until time  "to_date('16-01-15 12:23:00','DD-MM-YY HH24:MI:SS')" auxiliary destination '/u01/backup';

Starting recover at 16-JAN-15
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='iasB'

initialization parameters used for automatic instance:
db_name=CDB
db_unique_name=iasB_pitr_CDB
compatible=12.1.0.2.0
db_block_size=8192
db_files=200
diagnostic_dest=/u02/oracle
_system_trig_enabled=FALSE
sga_target=1584M
processes=200
db_create_file_dest=/u01/backup
log_archive_dest_1='location=/u01/backup'
enable_pluggable_database=true
_clone_one_pdb_recovery=true
#No auxiliary parameter file used


starting up automatic instance CDB

Oracle instance started

Total System Global Area    1660944384 bytes

Fixed Size                     2925072 bytes
Variable Size                419433968 bytes
Database Buffers            1224736768 bytes
Redo Buffers                  13848576 bytes
Automatic instance created

contents of Memory Script:
{
# set requested point in time
set until  time "to_date('16-01-15 12:23:00','DD-MM-YY 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-JAN-15
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 +DATA/CDB/AUTOBACKUP/2015_01_16/s_869142027.312.869142029
channel ORA_AUX_DISK_1: piece handle=+DATA/CDB/AUTOBACKUP/2015_01_16/s_869142027.312.869142029 tag=TAG20150116T122027
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:12
output file name=/u01/backup/CDB/controlfile/o1_mf_bckg857l_.ctl
Finished restore at 16-JAN-15

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('16-01-15 12:23:00','DD-MM-YY 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  4 to new;
set newname for clone datafile  3 to new;
set newname for clone tempfile  1 to new;
# switch all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set and the auxiliary set
restore 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 /u01/backup/CDB/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 16-JAN-15
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 /u01/backup/CDB/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00004 to /u01/backup/CDB/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /u01/backup/CDB/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/backup/db_0vpss33u_1_1.bak
channel ORA_AUX_DISK_1: piece handle=/u01/backup/db_0vpss33u_1_1.bak tag=TAG20150116T121350
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:04:16
Finished restore at 16-JAN-15

datafile 1 switched to datafile copy
input datafile copy RECID=6 STAMP=869152403 file name=/u01/backup/CDB/datafile/o1_mf_system_bckg8n3n_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=7 STAMP=869152403 file name=/u01/backup/CDB/datafile/o1_mf_undotbs1_bckg8n49_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=8 STAMP=869152403 file name=/u01/backup/CDB/datafile/o1_mf_sysaux_bckg8n44_.dbf

contents of Memory Script:
{
# set requested point in time
set until  time "to_date('16-01-15 12:23:00','DD-MM-YY HH24:MI:SS')";
# online the datafiles restored or switched
sql 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 only
recover 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 16-JAN-15
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 43 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_43.313.869142101
archived log for thread 1 with sequence 44 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_44.306.869142107
archived log for thread 1 with sequence 45 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_45.314.869142107
archived log for thread 1 with sequence 46 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_46.315.869142107
archived log for thread 1 with sequence 47 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_47.316.869151219
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_43.313.869142101 thread=1 sequence=43
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_44.306.869142107 thread=1 sequence=44
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_45.314.869142107 thread=1 sequence=45
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_46.315.869142107 thread=1 sequence=46
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_47.316.869151219 thread=1 sequence=47
media recovery complete, elapsed time: 00:00:03
Finished recover at 16-JAN-15

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 =
  ''/u01/backup/CDB/controlfile/o1_mf_bckg857l_.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    1660944384 bytes

Fixed Size                     2925072 bytes
Variable Size                419433968 bytes
Database Buffers            1224736768 bytes
Redo Buffers                  13848576 bytes

sql statement: alter system set  control_files =   ''/u01/backup/CDB/controlfile/o1_mf_bckg857l_.ctl'' comment= ''RMAN set'' scope=spfile

Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area    1660944384 bytes

Fixed Size                     2925072 bytes
Variable Size                419433968 bytes
Database Buffers            1224736768 bytes
Redo Buffers                  13848576 bytes

sql statement: alter database mount clone database

contents of Memory Script:
{
# set requested point in time
set until  time "to_date('16-01-15 12:23:00','DD-MM-YY HH24:MI:SS')";
# set destinations for recovery set and auxiliary set datafiles
set newname for datafile  6 to new;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  6;

switch clone datafile all;
}
executing Memory Script

executing command: SET until clause

executing command: SET NEWNAME

Starting restore at 16-JAN-15
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=178 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 00006 to /u01/backup/IASB_PITR_CDB/datafile/o1_mf_users_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/backup/db_0vpss33u_1_1.bak
channel ORA_AUX_DISK_1: piece handle=/u01/backup/db_0vpss33u_1_1.bak tag=TAG20150116T121350
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-JAN-15

datafile 6 switched to datafile copy
input datafile copy RECID=10 STAMP=869152483 file name=/u01/backup/IASB_PITR_CDB/datafile/o1_mf_users_bckglvqp_.dbf

contents of Memory Script:
{
# set requested point in time
set until  time "to_date('16-01-15 12:23:00','DD-MM-YY HH24:MI:SS')";
# online the datafiles restored or switched
sql clone "alter database datafile  6 online";
# recover and open resetlogs
recover clone database tablespace  "USERS", "SYSTEM", "UNDOTBS1", "SYSAUX" delete archivelog;
alter clone database open resetlogs;
}
executing Memory Script

executing command: SET until clause

sql statement: alter database datafile  6 online

Starting recover at 16-JAN-15
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 43 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_43.313.869142101
archived log for thread 1 with sequence 44 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_44.306.869142107
archived log for thread 1 with sequence 45 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_45.314.869142107
archived log for thread 1 with sequence 46 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_46.315.869142107
archived log for thread 1 with sequence 47 is already on disk as file +DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_47.316.869151219
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_43.313.869142101 thread=1 sequence=43
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_44.306.869142107 thread=1 sequence=44
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_45.314.869142107 thread=1 sequence=45
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_46.315.869142107 thread=1 sequence=46
archived log file name=+DATA/CDB/ARCHIVELOG/2015_01_16/thread_1_seq_47.316.869151219 thread=1 sequence=47
media recovery complete, elapsed time: 00:00:06
Finished recover at 16-JAN-15

database opened

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

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/u01/backup''

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/u01/backup''

Performing export of tables...
   EXPDP>
WARNING: Oracle Data Pump operations are not typically needed when connected to the root or seed of a container database.

   EXPDP> Starting "SYS"."TSPITR_EXP_iasB_Fvns":
   EXPDP> Estimate in progress using BLOCKS method...
   EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
   EXPDP> Total estimation using BLOCKS method: 49 MB
   EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
   EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
   EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
   EXPDP> . . exported "C##SALMAN"."TEST"                          41.53 MB  364412 rows
   EXPDP> Master table "SYS"."TSPITR_EXP_iasB_Fvns" successfully loaded/unloaded
   EXPDP> ******************************************************************************
   EXPDP> Dump file set for SYS.TSPITR_EXP_iasB_Fvns is:
   EXPDP>   /u01/backup/tspitr_iasB_97417.dmp
   EXPDP> Job "SYS"."TSPITR_EXP_iasB_Fvns" successfully completed at Fri Jan 16 15:16:30 2015 elapsed 0 00:00:41
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>
WARNING: Oracle Data Pump operations are not typically needed when connected to the root or seed of a container database.

   IMPDP> Master table "SYS"."TSPITR_IMP_iasB_zEmF" successfully loaded/unloaded
   IMPDP> Starting "SYS"."TSPITR_IMP_iasB_zEmF":
   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
   IMPDP> . . imported "C##SALMAN"."TEST"                          41.53 MB  364412 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_iasB_zEmF" successfully completed at Fri Jan 16 15:18:41 2015 elapsed 0 00:01:30
Import completed


Removing automatic instance
Automatic instance removed
auxiliary instance file /u01/backup/CDB/datafile/o1_mf_temp_bckgjvpn_.tmp deleted
auxiliary instance file /u01/backup/IASB_PITR_CDB/onlinelog/o1_mf_3_bckgmrjw_.log deleted
auxiliary instance file /u01/backup/IASB_PITR_CDB/onlinelog/o1_mf_2_bckgmkjx_.log deleted
auxiliary instance file /u01/backup/IASB_PITR_CDB/onlinelog/o1_mf_1_bckgmckt_.log deleted
auxiliary instance file /u01/backup/IASB_PITR_CDB/datafile/o1_mf_users_bckglvqp_.dbf deleted
auxiliary instance file /u01/backup/CDB/datafile/o1_mf_sysaux_bckg8n44_.dbf deleted
auxiliary instance file /u01/backup/CDB/datafile/o1_mf_undotbs1_bckg8n49_.dbf deleted
auxiliary instance file /u01/backup/CDB/datafile/o1_mf_system_bckg8n3n_.dbf deleted
auxiliary instance file /u01/backup/CDB/controlfile/o1_mf_bckg857l_.ctl deleted
auxiliary instance file tspitr_iasB_97417.dmp deleted
Finished recover at 16-JAN-15

“AUXILIARY DESTINATION” clause is used to mention the directory where restoration from the backup will be done for auxiliary database, and it should have enough space available to house the database files. In the following we can see that during the recovery process, datafiles were restored at this location

[oracle@salman1 backup]$pwd
/u01/backup
[oracle@salman1 backup]$ ls -ltr
total 2434800
-rw-r----- 1 oracle asmadmin 1232535552 Jan 16 12:16 db_0vpss33u_1_1.bak
-rw-r----- 1 oracle asmadmin  637501440 Jan 16 12:17 db_10pss39o_1_1.bak
-rw-r----- 1 oracle asmadmin  623181824 Jan 16 12:19 db_11pss3d1_1_1.bak
drwxr-x--- 3 oracle asmadmin       4096 Jan 16 14:55 CDB
[oracle@salman1 backup]$ cd CDB
[oracle@salman1 CDB]$ ls -ltr
total 4
drwxr-x--- 2 oracle asmadmin 4096 Jan 16 14:55 controlfile
[oracle@salman1 CDB]$ ls -ltr
total 4
drwxr-x--- 2 oracle asmadmin 4096 Jan 16 14:57 controlfile
[oracle@salman1 CDB]$ ls -ltr
total 12
drwxr-x--- 2 oracle asmadmin 4096 Jan 16 14:57 controlfile
drwxr-x--- 2 oracle asmadmin 4096 Jan 16 14:57 onlinelog
drwxr-x--- 2 oracle asmadmin 4096 Jan 16 14:57 datafile

Check if table has been recovered
SQL> conn c##salman/salman
Connected.
SQL> select count(*) from test;

  COUNT(*)
----------
    182206

SQL>

Following Oracle document also explains some more parameters which can be used in recovery command
http://docs.oracle.com/database/121/BRADV/rcmresind.htm#BRADV699

If table belongs to a pluggable database, recovery command would look like following
RMAN> RECOVER TABLE HR.PDB_EMP OF PLUGGABLE DATABASE HR_PDB
UNTIL TIME 'SYSDATE-4'
AUXILIARY DESTINATION '/tmp/backups'
REMAP TABLE 'HR'.'PDB_EMP':'EMP_RECVR';

DUMP FILE and DATAPUMP DESTINATION
Specifies the name of the export dump file containing recovered tables or table partitions and the location in which it must be stored. This dump file can later be used to import the table

NOTABLEIMPORT
Indicates that the recovered tables or table partitions must not be imported into the target database. Later we can use export dump file created during recovery(by specifying DUMPFILE clause) to import the table.

REMAP TABLE
Renames the recovered tables or table partitions in the target database.

REMAP TABLESPACE
Recovers the tables or table partitions into a tablespace that is different from the one in which these objects originally existed. 

No comments:

Post a Comment

Popular Posts - All Times