In sweet memories of my ever loving brother "kutty thambi " ARUN KUMAR

Thursday, July 29, 2010

Recover a Dropped or Deleted Table Using RMAN Backup

this is not incomplete point in time recovery.

From 10g Onwards you can recover the dropped table from recyclebin, suppose the table is not available in recycle bin also flashback database in not enabled.

But you are having a recent RMAN backup containing that table.

you can recover the table by

1. create a duplicate database from production database using until log sequence

RMAN> RUN
2> {
3> SET UNTIL SEQUENCE 11 THREAD 1;
4> DUPLICATE TARGET DATABASE TO "clone" NOFILENAMECHECK;
5> }

2. export the users table from clone(duplicate) database.

[oracle@rac2 clone]$ exp rajesh/rajesh file=/u01/app/oracle/test_rcv.dmp tables=test rows=y

3. import the users table to the production database.
[oracle@rac1 ~]$ imp rajesh/rajesh file=/u01/app/oracle/test_rcv.dmp fromuser=rajesh touser=rajesh tables=test

Example:

1. creating a table and insert some rows , take rman backup and then drop the database.
switch logfiles for more change.



SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/product/10.2.0/db_1/dbs/arch
Oldest online log sequence 1
Next log sequence to archive 3
Current log sequence 3

SQL> create user rajesh identified by rajesh;

User created.

SQL> grant connect,resource to rajesh;

Grant succeeded.

SQL> conn rajesh/rajesh;
Connected.
SQL> create table test ( id number);

Table created.

SQL> insert into test values (100);

1 row created.

SQL> /

1 row created.

SQL> /

1 row created.

SQL> /

1 row created.

SQL> commit;

Commit complete.

SQL> set time on;
19:11:52 SQL> conn sys/oracle as sysdba;
Connected.
19:12:03 SQL> alter system switch logfile;

System altered.

19:12:13 SQL> /

System altered.

19:12:17 SQL>

[oracle@rac1 ~]$ . oraenv
ORACLE_SID = [oracle] ? cdbs
[oracle@rac1 ~]$ rman target /

Recovery Manager: Release 10.2.0.1.0 - Production on Wed Jul 28 19:13:01 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.

connected to target database: CDBS (DBID=3126697048)

RMAN> backup full database format '/u01/app/oracle/backup/%d_%U.bckp' plus archivelog format '/u01/app/oracle/backup/%d_%U.bckp';


Starting backup at 28-JUL-10
current log archived
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=136 devtype=DISK
channel ORA_DISK_1: starting archive log backupset
channel ORA_DISK_1: specifying archive log(s) in backup set
input archive log thread=1 sequence=3 recid=1 stamp=725569933
input archive log thread=1 sequence=4 recid=2 stamp=725569937
input archive log thread=1 sequence=5 recid=3 stamp=725570192
channel ORA_DISK_1: starting piece 1 at 28-JUL-10
channel ORA_DISK_1: finished piece 1 at 28-JUL-10
piece handle=/u01/app/oracle/backup/CDBS_01ljuklc_1_1.bckp tag=TAG20100728T191651 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:03
Finished backup at 28-JUL-10

Starting backup at 28-JUL-10
using channel ORA_DISK_1
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
input datafile fno=00001 name=/u01/app/oracle/oradata/cdbs/system01.dbf
input datafile fno=00003 name=/u01/app/oracle/oradata/cdbs/sysaux01.dbf
input datafile fno=00002 name=/u01/app/oracle/oradata/cdbs/undotbs01.dbf
input datafile fno=00004 name=/u01/app/oracle/oradata/cdbs/users01.dbf
channel ORA_DISK_1: starting piece 1 at 28-JUL-10
channel ORA_DISK_1: finished piece 1 at 28-JUL-10
piece handle=/u01/app/oracle/backup/CDBS_02ljuklm_1_1.bckp tag=TAG20100728T191707 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:01:57
channel ORA_DISK_1: starting full datafile backupset
channel ORA_DISK_1: specifying datafile(s) in backupset
including current control file in backupset
including current SPFILE in backupset
channel ORA_DISK_1: starting piece 1 at 28-JUL-10
channel ORA_DISK_1: finished piece 1 at 28-JUL-10
piece handle=/u01/app/oracle/backup/CDBS_03ljukpb_1_1.bckp tag=TAG20100728T191707 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:05
Finished backup at 28-JUL-10

Starting backup at 28-JUL-10
current log archived
using channel ORA_DISK_1
channel ORA_DISK_1: starting archive log backupset
channel ORA_DISK_1: specifying archive log(s) in backup set
input archive log thread=1 sequence=6 recid=4 stamp=725570353
channel ORA_DISK_1: starting piece 1 at 28-JUL-10
channel ORA_DISK_1: finished piece 1 at 28-JUL-10
piece handle=/u01/app/oracle/backup/CDBS_04ljukpk_1_1.bckp tag=TAG20100728T191914 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02
Finished backup at 28-JUL-10

RMAN> configure channel device type disk format '/u01/app/oracle/backup/%U';

using target database control file instead of recovery catalog
old RMAN configuration parameters:
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/u01/app/oracle/backup/%U';
new RMAN configuration parameters:
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '/u01/app/oracle/backup/%U';
new RMAN configuration parameters are successfully stored

RMAN> backup archivelog all;

Starting backup at 28-JUL-10
current log archived
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=138 devtype=DISK
channel ORA_DISK_1: starting archive log backupset
channel ORA_DISK_1: specifying archive log(s) in backup set
input archive log thread=1 sequence=3 recid=1 stamp=725569933
input archive log thread=1 sequence=4 recid=2 stamp=725569937
input archive log thread=1 sequence=5 recid=3 stamp=725570192
input archive log thread=1 sequence=6 recid=4 stamp=725570353
input archive log thread=1 sequence=7 recid=5 stamp=725570642
input archive log thread=1 sequence=8 recid=6 stamp=725570810
channel ORA_DISK_1: starting piece 1 at 28-JUL-10
channel ORA_DISK_1: finished piece 1 at 28-JUL-10
piece handle=/u01/app/oracle/backup/05ljul85_1_1 tag=TAG20100728T192658 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:02
Finished backup at 28-JUL-10


19:28:01 SQL> create table sample ( id number);

Table created.

19:28:22 SQL> insert into sample values(100);

1 row created.

19:28:34 SQL> /

1 row created.

19:28:35 SQL> /

1 row created.

19:28:36 SQL> commit;

Commit complete.

19:28:38 SQL> select * from test;

ID
----------
100
100
100
100


19:29:30 SQL> drop table test;

Table dropped.

19:30:13 SQL> conn sys/oracle as sysdba;
Connected.
19:33:47 SQL> alter system switch logfile;

System altered.

19:33:53 SQL> /

System altered.



19:33:54 SQL>

19:48:46 SQL> create pfile from spfile;

File created.

19:51:27 SQL>


2. create a duplicate database in test server



create a required password file, init file,
dump file and database files directories for clone database.
configure listener.ora and tnsnames.ora files properly.

[oracle@rac1 dbs]$ scp initcdbs.ora oracle@rac2:/u01/app/oracle/product/10.2.0/db_1/dbs/
initcdbs.ora 100% 892 0.9KB/s 00:00
[oracle@rac1 dbs]$ scp orapwcdbs oracle@rac2:/u01/app/oracle/product/10.2.0/db_1/dbs/
orapwcdbs 100% 1536 1.5KB/s 00:00
[oracle@rac1 dbs]$

tnsnames.ora file on both machines
CDBS =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip.localdomain)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = cdbs)
)
)
CLONE =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = rac2-vip.localdomain)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = clone)
)
)


[oracle@rac2 ~]$ . oraenv
ORACLE_SID = [clone] ?
[oracle@rac2 ~]$ sqlplus '/as sysdba'

SQL*Plus: Release 10.2.0.1.0 - Production on Thu Jul 29 14:29:43 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to an idle instance.

SQL> startup nomount
ORACLE instance started.

Total System Global Area 444596224 bytes
Fixed Size 1219904 bytes
Variable Size 130024128 bytes
Database Buffers 310378496 bytes
Redo Buffers 2973696 bytes
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, OLAP and Data Mining options
[oracle@rac2 ~]$ . oraenv
ORACLE_SID = [clone] ?
[oracle@rac2 ~]$ rman target=sys/oracle@cdbs auxiliary=/

Recovery Manager: Release 10.2.0.1.0 - Production on Thu Jul 29 14:30:26 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.

connected to target database: CDBS (DBID=3126697048)
connected to auxiliary database: CLONE (not mounted)

RMAN> RUN
2> {
3> SET UNTIL SEQUENCE 11 THREAD 1;
4> DUPLICATE TARGET DATABASE TO "clone" NOFILENAMECHECK;
5> }

executing command: SET until clause
using target database control file instead of recovery catalog

Starting Duplicate Db at 29-JUL-10
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=153 devtype=DISK

contents of Memory Script:
{
set until scn 487077;
set newname for datafile 1 to
"/u01/app/oracle/oradata/clone/system01.dbf";
set newname for datafile 2 to
"/u01/app/oracle/oradata/clone/undotbs01.dbf";
set newname for datafile 3 to
"/u01/app/oracle/oradata/clone/sysaux01.dbf";
set newname for datafile 4 to
"/u01/app/oracle/oradata/clone/users01.dbf";
restore
check readonly
clone database
;
}
executing Memory Script

executing command: SET until clause

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

Starting restore at 29-JUL-10
using channel ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /u01/app/oracle/oradata/clone/system01.dbf
restoring datafile 00002 to /u01/app/oracle/oradata/clone/undotbs01.dbf
restoring datafile 00003 to /u01/app/oracle/oradata/clone/sysaux01.dbf
restoring datafile 00004 to /u01/app/oracle/oradata/clone/users01.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/backup/CDBS_02ljuklm_1_1.bckp
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u01/app/oracle/backup/CDBS_02ljuklm_1_1.bckp tag=TAG20100728T191707
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:38
Finished restore at 29-JUL-10
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "CLONE" RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 ( '/u01/app/oracle/oradata/clone/redo01.log' ) SIZE 50 M REUSE,
GROUP 2 ( '/u01/app/oracle/oradata/clone/redo02.log' ) SIZE 50 M REUSE,
GROUP 3 ( '/u01/app/oracle/oradata/clone/redo03.log' ) SIZE 50 M REUSE
DATAFILE
'/u01/app/oracle/oradata/clone/system01.dbf'
CHARACTER SET WE8ISO8859P1


contents of Memory Script:
{
switch clone datafile all;
}
executing Memory Script

released channel: ORA_AUX_DISK_1
datafile 2 switched to datafile copy
input datafile copy recid=1 stamp=725639677 filename=/u01/app/oracle/oradata/clone/undotbs01.dbf
datafile 3 switched to datafile copy
input datafile copy recid=2 stamp=725639677 filename=/u01/app/oracle/oradata/clone/sysaux01.dbf
datafile 4 switched to datafile copy
input datafile copy recid=3 stamp=725639678 filename=/u01/app/oracle/oradata/clone/users01.dbf

contents of Memory Script:
{
set until scn 487077;
recover
clone database
delete archivelog
;
}
executing Memory Script

executing command: SET until clause

Starting recover at 29-JUL-10
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=153 devtype=DISK

starting media recovery

channel ORA_AUX_DISK_1: starting archive log restore to default destination
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=6
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=7
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=8
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=9
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=10
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/backup/07ljv2pt_1_1
ORA-19870: error reading backup piece /u01/app/oracle/backup/07ljv2pt_1_1
ORA-19505: failed to identify file "/u01/app/oracle/backup/07ljv2pt_1_1"
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3
channel ORA_AUX_DISK_1: starting archive log restore to default destination
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=6
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=7
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=8
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=9
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=10
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/backup/06ljul8o_1_1
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u01/app/oracle/backup/06ljul8o_1_1 tag=TAG20100728T192719
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:06
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_6_725548321.dbf thread=1 sequence=6
channel clone_default: deleting archive log(s)
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_6_725548321.dbf recid=2 stamp=725639705
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_7_725548321.dbf thread=1 sequence=7
channel clone_default: deleting archive log(s)
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_7_725548321.dbf recid=5 stamp=725639705
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_8_725548321.dbf thread=1 sequence=8
channel clone_default: deleting archive log(s)
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_8_725548321.dbf recid=1 stamp=725639705
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_9_725548321.dbf thread=1 sequence=9
channel clone_default: deleting archive log(s)
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_9_725548321.dbf recid=4 stamp=725639705
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_10_725548321.dbf thread=1 sequence=10
channel clone_default: deleting archive log(s)
archive log filename=/u01/app/oracle/product/10.2.0/db_1/dbs/arch1_10_725548321.dbf recid=3 stamp=725639705
media recovery complete, elapsed time: 00:00:07
Finished recover at 29-JUL-10

contents of Memory Script:
{
shutdown clone;
startup clone nomount ;
}
executing Memory Script

database dismounted
Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area 444596224 bytes

Fixed Size 1219904 bytes
Variable Size 130024128 bytes
Database Buffers 310378496 bytes
Redo Buffers 2973696 bytes
sql statement: CREATE CONTROLFILE REUSE SET DATABASE "CLONE" RESETLOGS ARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 ( '/u01/app/oracle/oradata/clone/redo01.log' ) SIZE 50 M REUSE,
GROUP 2 ( '/u01/app/oracle/oradata/clone/redo02.log' ) SIZE 50 M REUSE,
GROUP 3 ( '/u01/app/oracle/oradata/clone/redo03.log' ) SIZE 50 M REUSE
DATAFILE
'/u01/app/oracle/oradata/clone/system01.dbf'
CHARACTER SET WE8ISO8859P1


contents of Memory Script:
{
set newname for tempfile 1 to
"/u01/app/oracle/oradata/clone/temp01.dbf";
switch clone tempfile all;
catalog clone datafilecopy "/u01/app/oracle/oradata/clone/undotbs01.dbf";
catalog clone datafilecopy "/u01/app/oracle/oradata/clone/sysaux01.dbf";
catalog clone datafilecopy "/u01/app/oracle/oradata/clone/users01.dbf";
switch clone datafile all;
}
executing Memory Script

executing command: SET NEWNAME

renamed temporary file 1 to /u01/app/oracle/oradata/clone/temp01.dbf in control file

cataloged datafile copy
datafile copy filename=/u01/app/oracle/oradata/clone/undotbs01.dbf recid=1 stamp=725639761

cataloged datafile copy
datafile copy filename=/u01/app/oracle/oradata/clone/sysaux01.dbf recid=2 stamp=725639762

cataloged datafile copy
datafile copy filename=/u01/app/oracle/oradata/clone/users01.dbf recid=3 stamp=725639763

datafile 2 switched to datafile copy
input datafile copy recid=1 stamp=725639761 filename=/u01/app/oracle/oradata/clone/undotbs01.dbf
datafile 3 switched to datafile copy
input datafile copy recid=2 stamp=725639762 filename=/u01/app/oracle/oradata/clone/sysaux01.dbf
datafile 4 switched to datafile copy
input datafile copy recid=3 stamp=725639763 filename=/u01/app/oracle/oradata/clone/users01.dbf

contents of Memory Script:
{
Alter clone database open resetlogs;
}
executing Memory Script

database opened
Finished Duplicate Db at 29-JUL-10

RMAN>


3. export the table from test database


[oracle@rac2 clone]$ . oraenv
ORACLE_SID = [oracle] ? clone
[oracle@rac2 clone]$ exp rajesh/rajesh file=/u01/app/oracle/test_rcv.dmp tables=test rows=y

Export: Release 10.2.0.1.0 - Production on Thu Jul 29 15:07:22 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, OLAP and Data Mining options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8ISO8859P1 character set (possible charset conversion)

About to export specified tables via Conventional Path ...
. . exporting table TEST 4 rows exported
Export terminated successfully without warnings.


[oracle@rac2 oracle]$ ls
admin backup oradata oraInventory product test_rcv.dmp
[oracle@rac2 oracle]$ scp test_rcv.dmp oracle@rac1:/u01/app/oracle/
test_rcv.dmp 100% 16KB 16.0KB/s 00:00
[oracle@rac2 oracle]$


4. Import the table into the production database and verify the table.


[oracle@rac1 ~]$ imp rajesh/rajesh file=/u01/app/oracle/test_rcv.dmp fromuser=rajesh touser=rajesh tables=test

Import: Release 10.2.0.1.0 - Production on Thu Jul 29 15:11:54 2010

Copyright (c) 1982, 2005, Oracle. All rights reserved.


Connected to: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, Real Application Clusters, OLAP and Data Mining options

Export file created by EXPORT:V10.02.01 via conventional path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses WE8ISO8859P1 character set (possible charset conversion)
. importing RAJESH's objects into RAJESH
. . importing table "TEST" 4 rows imported
Import terminated successfully without warnings.
[oracle@rac1 ~]$

SQL> conn rajesh/rajesh;
Connected.
SQL> select * from tab;

TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
SAMPLE TABLE
TEST TABLE

SQL> select * from test;

ID
----------
100
100
100
100

SQL>

table is recovered.


DONE !

hope, this helps you.

4 comments:

Kartik said...

Hi Rajesh,
it will be very helpful if could let me know this ....

suppose I have level 0 full backup every sunday and rest of days its level 1 bkup... and suppose if a table is dropped on thursday 11.00 am ... then how would i recover that .. should it wb like firs twe should apply level 0 bkup and then apply level 1 bkup ? if so the ncould you let me know the steps for same, please??

Anand said...

you have to do TSPITR.

ashish said...

Hey Rajesh,
it is neccessary to create duplicate database to recover a dropped table.

Foued said...

Thank you for sharing this post.
Foued

free counters
 
Share/Bookmark