Recovering Tables with RMAN (12c)

Hello, everyone.
Today I will talk about of one the interesting features on oracle 12c, which is recovery a table with RMAN.
There are could be many reasons to restore a table, for example a data load failure, a huge insert data in wrong way, these types of restore could be dangerous.
On the other way you can create a second database and restore from the backup of the first database, only the system, sysaux and undo tablespace and additionally
individual tablespace that contains the data that you want to restore. You can use data pump for achieve this.
Well, let’s start:

To achieve this there are some prerequisites:
The database have to stay in archive log mode and remained in archivelog mode up until the pont in time that you want to restore the database to.
An RMAN backup of the system, sysaux and undo tablespace must available and this backups must have been taken before the point that you want to restore object(s).
All tablespaces in the restore set must be restored to the same point in time.

Some restrictions:
You cannot restore tables that belong to SYS schema also tables that are stored in the SYSTEM and SYSAUX tablespace.

On this post we will use the clause remap option, provide the ability to rename the table during recovery process.

At our pluggable database (salespdb) we have a schema called john.
John is owner of this tables:

SQL> select count(*) from CLIENTS;

COUNT(*)
———-
6

SQL> select count(*) from EMPLOYEES;

COUNT(*)
———-
3

SQL> select count(*) from PRODUCTS;

COUNT(*)
———-
4

SQL> select count(*) from CITY;

COUNT(*)
———-
4

This is the time, before the disaster, I am considering that you already have a backup from your database.

SQL> alter session set nls_date_format=’mm/dd/yyyy hh24:mi:ss’;
SQL> select sysdate, current_scn from v$database;

SYSDATE                   CURRENT_SCN
——————-             ———–
12/24/2015 06:45:47   3184080
Now occurs the disaster , all rows of the tables were deleted, your phone don’t stop to ring, your boss is in your desk.

This is the time from disaster:

SQL> select sysdate, current_scn from v$database;

SYSDATE                   CURRENT_SCN
——————-             ———–
12/24/2015 06:47:41   3184141

But you are the super DBA, that dominated Oracle Database 12c:

Run the following commands:

[oracle@oraclesrv ~]$ rman target /

Recovery Manager: Release 12.1.0.2.0 – Production on Thu Dec 24 06:48:58 2015

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

connected to target database: DB12C (DBID=1333448469)

RMAN> recover table john.city, john.employees, john.products, john.clients OF PLUGGABLE DATABASE SALESPDB until time “to_date(’12/24/2015 06:45:47′, ‘mm/dd/yyyy hh24:mi:ss’)”
auxiliary destination ‘/u01/app/oracle/aux’  remap table john.city:rest_city, john.employees:rest_employees,
john.products:rest_products, john.clients:rest_clients;

Starting recover at 24-DEC-15
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=56 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=’lAit’

initialization parameters used for automatic instance:
db_name=DB12C
db_unique_name=lAit_pitr_SALESPDB_DB12C
compatible=12.1.0.2.0
db_block_size=8192
db_files=200
diagnostic_dest=/u01/app/oracle
_system_trig_enabled=FALSE
sga_target=396M
processes=200
db_create_file_dest=/u01/app/oracle/aux
log_archive_dest_1=’location=/u01/app/oracle/aux’
enable_pluggable_database=true
_clone_one_pdb_recovery=true
#No auxiliary parameter file used
starting up automatic instance DB12C

Oracle instance started

Total System Global Area 415236096 bytes

Fixed Size 2925072 bytes
Variable Size 134221296 bytes
Database Buffers 272629760 bytes
Redo Buffers 5459968 bytes
Automatic instance created

contents of Memory Script:
{
# set requested point in time
set until time “to_date(’12/24/2015 06:45:47′, ‘mm/dd/yyyy 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 24-DEC-15
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=22 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/DB12C/autobackup/2015_12_24/o1_mf_s_899275148_c7qpbxdv_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/DB12C/autobackup/2015_12_24/o1_mf_s_899275148_c7qpbxdv_.bkp tag=TAG20151224T063908
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/aux/DB12C/controlfile/o1_mf_c7qq01s9_.ctl
Finished restore at 24-DEC-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(’12/24/2015 06:45:47′, ‘mm/dd/yyyy 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 datafile 16 to new;
set newname for clone datafile 17 to new;
set newname for clone tempfile 1 to new;
set newname for clone tempfile 4 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, 16, 17;

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

renamed tempfile 1 to /u01/app/oracle/aux/DB12C/datafile/o1_mf_temp_%u_.tmp in control file
renamed tempfile 4 to /u01/app/oracle/aux/DB12C/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 24-DEC-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/app/oracle/aux/DB12C/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00004 to /u01/app/oracle/aux/DB12C/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /u01/app/oracle/aux/DB12C/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/fast_recovery_area/DB12C/backupset/2015_12_24/o1_mf_nnndf_TAG20151224T063414_c7qp1qrn_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/DB12C/backupset/2015_12_24/o1_mf_nnndf_TAG20151224T063414_c7qp1qrn_.bkp tag=TAG20151224T063414
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:36
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 00016 to /u01/app/oracle/aux/DB12C/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00017 to /u01/app/oracle/aux/DB12C/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/fast_recovery_area/DB12C/1CA86F469D8F09B3E0530F02000A0261/backupset/2015_12_24/o1_mf_nnndf_TAG20151224T063414_c7qp8m0k_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/DB12C/1CA86F469D8F09B3E0530F02000A0261/backupset/2015_12_24/o1_mf_nnndf_TAG20151224T063414_c7qp8m0k_.bkp tag=TAG20151224T063414
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:47
Finished restore at 24-DEC-15

datafile 1 switched to datafile copy
input datafile copy RECID=12 STAMP=899275981 file name=/u01/app/oracle/aux/DB12C/datafile/o1_mf_system_c7qq0d0d_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=13 STAMP=899275981 file name=/u01/app/oracle/aux/DB12C/datafile/o1_mf_undotbs1_c7qq0d0f_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=14 STAMP=899275981 file name=/u01/app/oracle/aux/DB12C/datafile/o1_mf_sysaux_c7qq0cyy_.dbf
datafile 16 switched to datafile copy
input datafile copy RECID=15 STAMP=899275981 file name=/u01/app/oracle/aux/DB12C/datafile/o1_mf_system_c7qq3df6_.dbf
datafile 17 switched to datafile copy
input datafile copy RECID=16 STAMP=899275981 file name=/u01/app/oracle/aux/DB12C/datafile/o1_mf_sysaux_c7qq3dd7_.dbf

contents of Memory Script:
{
# set requested point in time
set until time “to_date(’12/24/2015 06:45:47′, ‘mm/dd/yyyy 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”;
sql clone ‘SALESPDB’ “alter database datafile
16 online”;
sql clone ‘SALESPDB’ “alter database datafile
17 online”;
# recover and open database read only
recover clone database tablespace “SYSTEM”, “UNDOTBS1”, “SYSAUX”, “SALESPDB”:”SYSTEM”, “SALESPDB”:”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

sql statement: alter database datafile 16 online

sql statement: alter database datafile 17 online

Starting recover at 24-DEC-15
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 176 is already on disk as file /u01/app/oracle/fast_recovery_area/DB12C/archivelog/2015_12_24/o1_mf_1_176_c7qpbt2t_.arc
archived log for thread 1 with sequence 177 is already on disk as file /u01/app/oracle/fast_recovery_area/DB12C/archivelog/2015_12_24/o1_mf_1_177_c7qpqm9v_.arc
archived log file name=/u01/app/oracle/fast_recovery_area/DB12C/archivelog/2015_12_24/o1_mf_1_176_c7qpbt2t_.arc thread=1 sequence=176
archived log file name=/u01/app/oracle/fast_recovery_area/DB12C/archivelog/2015_12_24/o1_mf_1_177_c7qpqm9v_.arc thread=1 sequence=177
media recovery complete, elapsed time: 00:00:04
Finished recover at 24-DEC-15

sql statement: alter database open read only

contents of Memory Script:
{
sql clone ‘alter pluggable database SALESPDB open read only’;
}
executing Memory Script

sql statement: alter pluggable database SALESPDB 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/app/oracle/aux/DB12C/controlfile/o1_mf_c7qq01s9_.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 415236096 bytes

Fixed Size 2925072 bytes
Variable Size 138415600 bytes
Database Buffers 268435456 bytes
Redo Buffers 5459968 bytes

sql statement: alter system set control_files = ”/u01/app/oracle/aux/DB12C/controlfile/o1_mf_c7qq01s9_.ctl” comment= ”RMAN set” scope=spfile

Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area 415236096 bytes

Fixed Size 2925072 bytes
Variable Size 138415600 bytes
Database Buffers 268435456 bytes
Redo Buffers 5459968 bytes

sql statement: alter database mount clone database

contents of Memory Script:
{
# set requested point in time
set until time “to_date(’12/24/2015 06:45:47′, ‘mm/dd/yyyy hh24:mi:ss’)”;
# set destinations for recovery set and auxiliary set datafiles
set newname for datafile 19 to new;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile 19;

switch clone datafile all;
}
executing Memory Script

executing command: SET until clause

executing command: SET NEWNAME

Starting restore at 24-DEC-15
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=23 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 00019 to /u01/app/oracle/aux/LAIT_PITR_SALESPDB_DB12C/datafile/o1_mf_tbssales_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/fast_recovery_area/DB12C/1CA86F469D8F09B3E0530F02000A0261/backupset/2015_12_24/o1_mf_nnndf_TAG20151224T063414_c7qp8m0k_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/DB12C/1CA86F469D8F09B3E0530F02000A0261/backupset/2015_12_24/o1_mf_nnndf_TAG20151224T063414_c7qp8m0k_.bkp tag=TAG20151224T063414
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:08
Finished restore at 24-DEC-15

datafile 19 switched to datafile copy
input datafile copy RECID=18 STAMP=899276128 file name=/u01/app/oracle/aux/LAIT_PITR_SALESPDB_DB12C/datafile/o1_mf_tbssales_c7qq94h1_.dbf

contents of Memory Script:
{
# set requested point in time
set until time “to_date(’12/24/2015 06:45:47′, ‘mm/dd/yyyy hh24:mi:ss’)”;
# online the datafiles restored or switched
sql clone ‘SALESPDB’ “alter database datafile
19 online”;
# recover and open resetlogs
recover clone database tablespace “SALESPDB”:”TBSSALES”, “SYSTEM”, “UNDOTBS1”, “SYSAUX”, “SALESPDB”:”SYSTEM”, “SALESPDB”:”SYSAUX” delete archivelog;
alter clone database open resetlogs;
}
executing Memory Script

executing command: SET until clause

sql statement: alter database datafile 19 online

Starting recover at 24-DEC-15
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 176 is already on disk as file /u01/app/oracle/fast_recovery_area/DB12C/archivelog/2015_12_24/o1_mf_1_176_c7qpbt2t_.arc
archived log for thread 1 with sequence 177 is already on disk as file /u01/app/oracle/fast_recovery_area/DB12C/archivelog/2015_12_24/o1_mf_1_177_c7qpqm9v_.arc
archived log file name=/u01/app/oracle/fast_recovery_area/DB12C/archivelog/2015_12_24/o1_mf_1_176_c7qpbt2t_.arc thread=1 sequence=176
archived log file name=/u01/app/oracle/fast_recovery_area/DB12C/archivelog/2015_12_24/o1_mf_1_177_c7qpqm9v_.arc thread=1 sequence=177
media recovery complete, elapsed time: 00:00:02
Finished recover at 24-DEC-15

database opened

contents of Memory Script:
{
sql clone ‘alter pluggable database SALESPDB open’;
}
executing Memory Script

sql statement: alter pluggable database SALESPDB open

contents of Memory Script:
{
# create directory for datapump import
sql ‘SALESPDB’ “create or replace directory
TSPITR_DIROBJ_DPDIR as ”
/u01/app/oracle/aux””;
# create directory for datapump export
sql clone ‘SALESPDB’ “create or replace directory
TSPITR_DIROBJ_DPDIR as ”
/u01/app/oracle/aux””;
}
executing Memory Script

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ”/u01/app/oracle/aux”

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ”/u01/app/oracle/aux”

Performing export of tables…
EXPDP> Starting “SYS”.”TSPITR_EXP_lAit_jAue”:
EXPDP> Estimate in progress using BLOCKS method…
EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
EXPDP> Total estimation using BLOCKS method: 256 KB
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 “JOHN”.”CITY” 5.593 KB 4 rows
EXPDP> . . exported “JOHN”.”CLIENTS” 6.078 KB 6 rows
EXPDP> . . exported “JOHN”.”EMPLOYEES” 5.554 KB 3 rows
EXPDP> . . exported “JOHN”.”PRODUCTS” 5.578 KB 4 rows
EXPDP> Master table “SYS”.”TSPITR_EXP_lAit_jAue” successfully loaded/unloaded
EXPDP> ******************************************************************************
EXPDP> Dump file set for SYS.TSPITR_EXP_lAit_jAue is:
EXPDP> /u01/app/oracle/aux/tspitr_lAit_18711.dmp
EXPDP> Job “SYS”.”TSPITR_EXP_lAit_jAue” successfully completed at Thu Dec 24 07:02:19 2015 elapsed 0 00:03:30
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_lAit_zlur” successfully loaded/unloaded
IMPDP> Starting “SYS”.”TSPITR_IMP_lAit_zlur”:
IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
IMPDP> . . imported “JOHN”.”REST_CITY” 5.593 KB 4 rows
IMPDP> . . imported “JOHN”.”REST_CLIENTS” 6.078 KB 6 rows
IMPDP> . . imported “JOHN”.”REST_EMPLOYEES” 5.554 KB 3 rows
IMPDP> . . imported “JOHN”.”REST_PRODUCTS” 5.578 KB 4 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_lAit_zlur” successfully completed at Thu Dec 24 07:05:13 2015 elapsed 0 00:02:02
Import completed
Removing automatic instance
Automatic instance removed
auxiliary instance file /u01/app/oracle/aux/DB12C/datafile/o1_mf_temp_c7qq5y3r_.tmp deleted
auxiliary instance file /u01/app/oracle/aux/DB12C/datafile/o1_mf_temp_c7qq5d5y_.tmp deleted
auxiliary instance file /u01/app/oracle/aux/LAIT_PITR_SALESPDB_DB12C/onlinelog/o1_mf_3_c7qq9vtd_.log deleted
auxiliary instance file /u01/app/oracle/aux/LAIT_PITR_SALESPDB_DB12C/onlinelog/o1_mf_2_c7qq9vd6_.log deleted
auxiliary instance file /u01/app/oracle/aux/LAIT_PITR_SALESPDB_DB12C/onlinelog/o1_mf_1_c7qq9trd_.log deleted
auxiliary instance file /u01/app/oracle/aux/LAIT_PITR_SALESPDB_DB12C/datafile/o1_mf_tbssales_c7qq94h1_.dbf deleted
auxiliary instance file /u01/app/oracle/aux/DB12C/datafile/o1_mf_sysaux_c7qq3dd7_.dbf deleted
auxiliary instance file /u01/app/oracle/aux/DB12C/datafile/o1_mf_system_c7qq3df6_.dbf deleted
auxiliary instance file /u01/app/oracle/aux/DB12C/datafile/o1_mf_sysaux_c7qq0cyy_.dbf deleted
auxiliary instance file /u01/app/oracle/aux/DB12C/datafile/o1_mf_undotbs1_c7qq0d0f_.dbf deleted
auxiliary instance file /u01/app/oracle/aux/DB12C/datafile/o1_mf_system_c7qq0d0d_.dbf deleted
auxiliary instance file /u01/app/oracle/aux/DB12C/controlfile/o1_mf_c7qq01s9_.ctl deleted
auxiliary instance file tspitr_lAit_18711.dmp deleted
Finished recover at 24-DEC-15

Well done, check the tables on john’s schema:

SQL> select count(*) from REST_CLIENTS;

COUNT(*)
———-
6

SQL> select count(*) from REST_EMPLOYEES;

COUNT(*)
———-
3

SQL> select count(*) from REST_PRODUCTS;

COUNT(*)
———-
4

SQL> select count(*) from REST_CITY;

COUNT(*)
———-
4

I hope that you enjoy!

Thanks!

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