RECOVER TABLE USING RMAN
Drop a table as a matter of simulation
SQL> drop table test1.regions;
Table dropped.
SQL> select * from test1.regions;
select * from test1.regions
*
ERROR at line 1:
ORA-00942: table or view does not exist
Starting from oracle 12c,there is recover table feature which can recover the particular accidentally modified or dropped table to the previous point in time.
This feature is an alternative to flashback feature which has its own advantage and disadvantage
This feature needs backup to be taken and cannot be used in restoring the table to the recent changes which can be solved by flashback
Below is an example simulation of this feature
I purposefully pasted the whole output so that the steps performed by RMAN are important to understand
WARNING: This method requires sufficient space in the disk equal to the tablespace size.whichever table that you recover and the tablespace where the table exist both space should be considered.
check tablespace size where table reside
SQL> select bytes/1073741824 as SIZE_IN_GB,TABLESPACE_NAME from dba_data_files where TABLESPACE_NAME='USERS';
SIZE_IN_GB TABLESPACE_NAME
---------- ------------------------------
6.9934082 USERS
Find the tablespace of corrupted table
SQL> @colformat.sql
Procedure created.
Procedure created.
SQL> select table_name,tablespace_name from dba_tables where table_name='REGIONS';
TABLE_NAME TABLESPACE_NAME
--------------- ---------------
REGIONS USERS
check corrupted table size
SQL> select bytes/1024 as SIZE_IN_KB from dba_segments where segment_name='REGIONS';
SIZE_IN_KB
----------
64
Recover table command
RMAN> recover table test1.regions until time "to_date('25-FEB-21 19:20:00','dd-mon-yy hh24:mi:ss')" auxiliary destination '/home/oracle';
Starting recover at 25-FEB-21
using target database control file instead of recovery catalog
current log archived
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=62 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='sobu'
initialization parameters used for automatic instance:
db_name=ORCL19X
db_unique_name=sobu_pitr_ORCL19X
compatible=12.1.0.2.0
db_block_size=8192
db_files=200
diagnostic_dest=/apps01/base
_system_trig_enabled=FALSE
sga_target=1504M
processes=120
db_create_file_dest=/home/oracle
log_archive_dest_1='location=/home/oracle'
#No auxiliary parameter file used
starting up automatic instance ORCL19X
Oracle instance started
Total System Global Area 1577058304 bytes
Fixed Size 2924832 bytes
Variable Size 402656992 bytes
Database Buffers 1157627904 bytes
Redo Buffers 13848576 bytes
Automatic instance created
contents of Memory Script:
{
# set requested point in time
set until time "to_date('25-FEB-21 19:20:00','dd-mon-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 25-FEB-21
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=20 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 /apps01/product/12.1.0/dbhome_1/dbs/04vo2gsh_1_1
channel ORA_AUX_DISK_1: piece handle=/apps01/product/12.1.0/dbhome_1/dbs/04vo2gsh_1_1 tag=TAG20210225T102357
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:04
output file name=/home/oracle/ORCL19X/controlfile/o1_mf_j3hcc0dy_.ctl
Finished restore at 25-FEB-21
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('25-FEB-21 19:20:00','dd-mon-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 9 to new;
set newname for clone datafile 4 to new;
set newname for clone datafile 3 to new;
set newname for clone datafile 8 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, 9, 4, 3, 8;
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
renamed tempfile 1 to /home/oracle/ORCL19X/datafile/o1_mf_temp_%u_.tmp in control file
Starting restore at 25-FEB-21
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 /home/oracle/ORCL19X/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00009 to /home/oracle/ORCL19X/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00004 to /home/oracle/ORCL19X/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /home/oracle/ORCL19X/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00008 to /home/oracle/ORCL19X/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /apps01/product/12.1.0/dbhome_1/dbs/03vo2glu_1_1
channel ORA_AUX_DISK_1: piece handle=/apps01/product/12.1.0/dbhome_1/dbs/03vo2glu_1_1 tag=TAG20210225T102357
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:03:20
Finished restore at 25-FEB-21
datafile 1 switched to datafile copy
input datafile copy RECID=15 STAMP=1065468835 file name=/home/oracle/ORCL19X/datafile/o1_mf_system_j3hcd1lg_.dbf
datafile 9 switched to datafile copy
input datafile copy RECID=16 STAMP=1065468836 file name=/home/oracle/ORCL19X/datafile/o1_mf_system_j3hcd2bn_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=17 STAMP=1065468837 file name=/home/oracle/ORCL19X/datafile/o1_mf_undotbs1_j3hcd1yg_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=18 STAMP=1065468838 file name=/home/oracle/ORCL19X/datafile/o1_mf_sysaux_j3hcd146_.dbf
datafile 8 switched to datafile copy
input datafile copy RECID=19 STAMP=1065468839 file name=/home/oracle/ORCL19X/datafile/o1_mf_sysaux_j3hcd23d_.dbf
contents of Memory Script:
{
# set requested point in time
set until time "to_date('25-FEB-21 19:20:00','dd-mon-yy hh24:mi:ss')";
# online the datafiles restored or switched
sql clone "alter database datafile 1 online";
sql clone "alter database datafile 9 online";
sql clone "alter database datafile 4 online";
sql clone "alter database datafile 3 online";
sql clone "alter database datafile 8 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 9 online
sql statement: alter database datafile 4 online
sql statement: alter database datafile 3 online
sql statement: alter database datafile 8 online
Starting recover at 25-FEB-21
using channel ORA_AUX_DISK_1
starting media recovery
archived log for thread 1 with sequence 78 is already on disk as file /apps01/product/12.1.0/dbhome_1/dbs/arch1_78_1060279903.dbf
archived log for thread 1 with sequence 79 is already on disk as file /apps01/product/12.1.0/dbhome_1/dbs/arch1_79_1060279903.dbf
channel ORA_AUX_DISK_1: starting archived log restore to default destination
channel ORA_AUX_DISK_1: restoring archived log
archived log thread=1 sequence=77
channel ORA_AUX_DISK_1: reading from backup piece /apps01/product/12.1.0/dbhome_1/dbs/05vo2gsu_1_1
channel ORA_AUX_DISK_1: piece handle=/apps01/product/12.1.0/dbhome_1/dbs/05vo2gsu_1_1 tag=TAG20210225T102742
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:04
archived log file name=/home/oracle/1_77_1060279903.dbf thread=1 sequence=77
archived log file name=/apps01/product/12.1.0/dbhome_1/dbs/arch1_78_1060279903.dbf thread=1 sequence=78
archived log file name=/apps01/product/12.1.0/dbhome_1/dbs/arch1_79_1060279903.dbf thread=1 sequence=79
media recovery complete, elapsed time: 00:01:32
Finished recover at 25-FEB-21
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 =
''/home/oracle/ORCL19X/controlfile/o1_mf_j3hcc0dy_.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 1577058304 bytes
Fixed Size 2924832 bytes
Variable Size 402656992 bytes
Database Buffers 1157627904 bytes
Redo Buffers 13848576 bytes
sql statement: alter system set control_files = ''/home/oracle/ORCL19X/controlfile/o1_mf_j3hcc0dy_.ctl'' comment= ''RMAN set'' scope=spfile
Oracle instance shut down
connected to auxiliary database (not started)
Oracle instance started
Total System Global Area 1577058304 bytes
Fixed Size 2924832 bytes
Variable Size 402656992 bytes
Database Buffers 1157627904 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('25-FEB-21 19:20:00','dd-mon-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 25-FEB-21
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=21 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 /home/oracle/SOBU_PITR_ORCL19X/datafile/o1_mf_users_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece /apps01/product/12.1.0/dbhome_1/dbs/03vo2glu_1_1
channel ORA_AUX_DISK_1: piece handle=/apps01/product/12.1.0/dbhome_1/dbs/03vo2glu_1_1 tag=TAG20210225T102357
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:36
Finished restore at 25-FEB-21
datafile 6 switched to datafile copy
input datafile copy RECID=21 STAMP=1065469296 file name=/home/oracle/SOBU_PITR_ORCL19X/datafile/o1_mf_users_j3hcxq5k_.dbf
contents of Memory Script:
{
# set requested point in time
set until time "to_date('25-FEB-21 19:20:00','dd-mon-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 25-FEB-21
using channel ORA_AUX_DISK_1
starting media recovery
archived log for thread 1 with sequence 78 is already on disk as file /apps01/product/12.1.0/dbhome_1/dbs/arch1_78_1060279903.dbf
archived log for thread 1 with sequence 79 is already on disk as file /apps01/product/12.1.0/dbhome_1/dbs/arch1_79_1060279903.dbf
channel ORA_AUX_DISK_1: starting archived log restore to default destination
channel ORA_AUX_DISK_1: restoring archived log
archived log thread=1 sequence=77
channel ORA_AUX_DISK_1: reading from backup piece /apps01/product/12.1.0/dbhome_1/dbs/05vo2gsu_1_1
channel ORA_AUX_DISK_1: piece handle=/apps01/product/12.1.0/dbhome_1/dbs/05vo2gsu_1_1 tag=TAG20210225T102742
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
archived log file name=/home/oracle/1_77_1060279903.dbf thread=1 sequence=77
channel clone_default: deleting archived log(s)
archived log file name=/home/oracle/1_77_1060279903.dbf RECID=78 STAMP=1065469303
archived log file name=/apps01/product/12.1.0/dbhome_1/dbs/arch1_78_1060279903.dbf thread=1 sequence=78
archived log file name=/apps01/product/12.1.0/dbhome_1/dbs/arch1_79_1060279903.dbf thread=1 sequence=79
media recovery complete, elapsed time: 00:00:07
Finished recover at 25-FEB-21
database opened
contents of Memory Script:
{
# create directory for datapump import
sql "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/home/oracle''";
# create directory for datapump export
sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/home/oracle''";
}
executing Memory Script
sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle''
sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle''
Performing export of tables...
EXPDP> Starting "SYS"."TSPITR_EXP_sobu_DrmC":
EXPDP> Estimate in progress using BLOCKS method...
EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
EXPDP> Total estimation using BLOCKS method: 64 KB
EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
EXPDP> Processing object type TABLE_EXPORT/TABLE/COMMENT
EXPDP> Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
EXPDP> Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
EXPDP> Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
EXPDP> . . exported "TEST1"."REGIONS" 5.546 KB 4 rows
EXPDP> Master table "SYS"."TSPITR_EXP_sobu_DrmC" successfully loaded/unloaded
EXPDP> ******************************************************************************
EXPDP> Dump file set for SYS.TSPITR_EXP_sobu_DrmC is:
EXPDP> /home/oracle/tspitr_sobu_50421.dmp
EXPDP> Job "SYS"."TSPITR_EXP_sobu_DrmC" successfully completed at Thu Feb 25 19:43:49 2021 elapsed 0 00:00:50
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_sobu_pqnF" successfully loaded/unloaded
IMPDP> Starting "SYS"."TSPITR_IMP_sobu_pqnF":
IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
IMPDP> . . imported "TEST1"."REGIONS" 5.546 KB 4 rows
IMPDP> Processing object type TABLE_EXPORT/TABLE/COMMENT
IMPDP> Processing object type TABLE_EXPORT/TABLE/INDEX/INDEX
IMPDP> Processing object type TABLE_EXPORT/TABLE/CONSTRAINT/CONSTRAINT
IMPDP> Processing object type TABLE_EXPORT/TABLE/INDEX/STATISTICS/INDEX_STATISTICS
IMPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
IMPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER
IMPDP> Job "SYS"."TSPITR_IMP_sobu_pqnF" successfully completed at Thu Feb 25 19:45:30 2021 elapsed 0 00:00:47
Import completed
Removing automatic instance
Automatic instance removed
auxiliary instance file /home/oracle/ORCL19X/datafile/o1_mf_temp_j3hcp6y2_.tmp deleted
auxiliary instance file /home/oracle/SOBU_PITR_ORCL19X/onlinelog/o1_mf_3_j3hd1h0j_.log deleted
auxiliary instance file /home/oracle/SOBU_PITR_ORCL19X/onlinelog/o1_mf_2_j3hd1d8g_.log deleted
auxiliary instance file /home/oracle/SOBU_PITR_ORCL19X/onlinelog/o1_mf_1_j3hd1798_.log deleted
auxiliary instance file /home/oracle/SOBU_PITR_ORCL19X/datafile/o1_mf_users_j3hcxq5k_.dbf deleted
auxiliary instance file /home/oracle/ORCL19X/datafile/o1_mf_sysaux_j3hcd23d_.dbf deleted
auxiliary instance file /home/oracle/ORCL19X/datafile/o1_mf_sysaux_j3hcd146_.dbf deleted
auxiliary instance file /home/oracle/ORCL19X/datafile/o1_mf_undotbs1_j3hcd1yg_.dbf deleted
auxiliary instance file /home/oracle/ORCL19X/datafile/o1_mf_system_j3hcd2bn_.dbf deleted
auxiliary instance file /home/oracle/ORCL19X/datafile/o1_mf_system_j3hcd1lg_.dbf deleted
auxiliary instance file /home/oracle/ORCL19X/controlfile/o1_mf_j3hcc0dy_.ctl deleted
auxiliary instance file tspitr_sobu_50421.dmp deleted
Finished recover at 25-FEB-21
RMAN> exit
Recovery Manager complete.
My table is back now!
SQL> select count(*) from test1.regions;
COUNT(*)
----------
4
SQL> select * from test1.regions;
REGION_ID REGION_NAME
---------- -------------------------
1 Europe
2 Americas
3 Asia
4 Middle East and Africa