Recuperando tabelas com o RMAN 12c

Por Alex Zaballa,
Postado em janeiro 2014

O que é o Oracle RMAN ?

RMAN (Recovery Manager) é uma ferramenta de backup e recover de banco de dados, criada e fornecida pela Oracle; a mesma oferece opções de backup, restauração e recuperação. Esta ferramenta é recomendada pela Oracle como seu método preferencial de backup e recuperação.

Recover Table

O comando RECOVER TABLE, introduzido na versão 12c, é utilizado para recuperação de tabelas/partições de um backup realizado pelo RMAN. Este comando, diminui o tempo e a complexidade da restauração, permitindo a recuperação “point-in-time” apenas da tabela/partição, ao invés de toda tablespace como era nas versões anteriores. É muito útil quando não temos informações suficientes no UNDO para utilizar o Flashback Table.

Como funciona:

- Uma instance auxiliar é inicializada, utilizando o control file primário, as tablespaces SYSTEM,SYSAUX, UNDO e a tablespace que contém a tabela que irá ser restaurada.

- É feito recover da instance até SCN/TEMPO desejados

- A tabela é importada para a instance principal via SQL Net.

Observações importantes:

- O banco deve estar no modo ARCHIVELOG

- As tabelas dependentes devem ser incluídas, baseando-se nas constraints de FOREIGN KEY

- Ter espaço em disco e recursos necessário para montar a instance auxiliar

- Ter pelo menos um backup full, com os archives necessários

- Caso a tabela não exista mais, será feita a recuperação dos índices, constraints e triggers

É possível utilizar os parâmetros NOTABLEIMPORT, DATAPUMP DESTINATION e DATAPUMP FILE para guardar e manipular o export gerado.

Também temos o REMAP TABLE e REMAP TABLESPACE para renomear a tabela e a tablespace no banco de destino.

Estado da tabela no dia 01/10/2013 09:33:39:


  
SQL> select to_char(sysdate,'dd/mm/rrrr hh24:mi:ss') data from dual;

DATA
-------------------
01/10/2013 09:33:39

SQL> select * from hr.regions order by 1;

 REGION_ID REGION_NAME
---------- -------------------------
	 1 Europe
	 2 Americas
	 3 Asia
	 4 Middle East and Africa
 
 

No dia 05/10/2013 ela sofreu uma inserção.


  
SQL> select to_char(sysdate,'dd/mm/rrrr hh24:mi:ss') data from dual;

DATA
-------------------
05/10/2013 21:32:31

SQL> insert into hr.regions values (5,'Teste');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from hr.regions order by 1;

 REGION_ID REGION_NAME
---------- -------------------------
	 1 Europe
	 2 Americas
	 3 Asia
	 4 Middle East and Africa
	 5 Teste
 
 

Agora iremos restaurar a tabela para o estado que ela estava em 01/10/2013 09:33:39 :


  
[oracle@oracle01 ~]$ rman target / nocatalog

Recovery Manager: Release 12.1.0.1.0 - Production on Thu Oct 10 11:53:24 2013

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

connected to target database: ORCL (DBID=1356220819)
using target database control file instead of recovery catalog

RMAN> RECOVER TABLE HR.REGIONS 
UNTIL TIME "TO_DATE('01/10/2013 09:33:39','DD/MM/RRRR HH24:MI:SS')"
AUXILIARY DESTINATION '/tmp/backups' 
2> 3> 4> ;

Starting recover at 10-OCT-13
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=33 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='zkwh'

initialization parameters used for automatic instance:
db_name=ORCL
db_unique_name=zkwh_pitr_ORCL
compatible=12.1.0.0.0
db_block_size=8192
db_files=200
sga_target=1G
processes=80
diagnostic_dest=/u01/app/oracle
db_create_file_dest=/tmp/backups
log_archive_dest_1='location=/tmp/backups'
#No auxiliary parameter file used


starting up automatic instance ORCL

Oracle instance started

Total System Global Area    1068937216 bytes

Fixed Size                     2296576 bytes
Variable Size                281019648 bytes
Database Buffers             780140544 bytes
Redo Buffers                   5480448 bytes
Automatic instance created

contents of Memory Script:
{
# set requested point in time
set until  time "TO_DATE('01/10/2013 09:33:39','DD/MM/RRRR 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 10-OCT-13
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=83 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 +FRA01/ORCL/BACKUPSET/
2013_10_10/ncsnn0_tag20131010t105304_0.264.828442589
channel ORA_AUX_DISK_1: piece handle=+FRA01/ORCL/BACKUPSET/2013_10_10/
ncsnn0_tag20131010t105304_0.264.828442589 tag=TAG20131010T105304
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:13
output file name=/tmp/backups/ORCL/controlfile/o1_mf_95f1v6h2_.ctl
Finished restore at 10-OCT-13

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('01/10/2013 09:33:39','DD/MM/RRRR 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 /tmp/backups/ORCL/datafile/
o1_mf_temp_%u_.tmp in control file

Starting restore at 10-OCT-13
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 /tmp/backups/
ORCL/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00004 to /tmp/backups/
ORCL/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00003 to /tmp/backups/
ORCL/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece +FRA01/ORCL/
BACKUPSET/2013_10_10/nnndn0_tag20131010t105304_0.263.828442385
channel ORA_AUX_DISK_1: piece handle=+FRA01/ORCL/BACKUPSET/2013_10_10/
nnndn0_tag20131010t105304_0.263.828442385 tag=TAG20131010T105304
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:46
Finished restore at 10-OCT-13

datafile 1 switched to datafile copy
input datafile copy RECID=4 STAMP=828446189 file name=/tmp/backups/ORCL/
datafile/o1_mf_system_95f1vmvk_.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=5 STAMP=828446189 file name=/tmp/backups/ORCL/
datafile/o1_mf_undotbs1_95f1vmy3_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=6 STAMP=828446189 file name=/tmp/backups/ORCL/
datafile/o1_mf_sysaux_95f1vmvp_.dbf

contents of Memory Script:
{
# set requested point in time
set until  time "TO_DATE('01/10/2013 09:33:39','DD/MM/RRRR 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 10-OCT-13
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 6 is already on disk as file 
+FRA01/ORCL/ARCHIVELOG/2013_10_10/thread_1_seq_6.265.828442595
archived log for thread 1 with sequence 7 is already on disk as file 
+FRA01/ORCL/ARCHIVELOG/2013_10_10/thread_1_seq_7.267.828444779
archived log file name=+FRA01/ORCL/ARCHIVELOG/2013_10_10/
thread_1_seq_6.265.828442595 thread=1 sequence=6
archived log file name=+FRA01/ORCL/ARCHIVELOG/2013_10_10/
thread_1_seq_7.267.828444779 thread=1 sequence=7
media recovery complete, elapsed time: 00:00:06
Finished recover at 10-OCT-13

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 = 
  ''/tmp/backups/ORCL/controlfile/o1_mf_95f1v6h2_.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    1068937216 bytes

Fixed Size                     2296576 bytes
Variable Size                285213952 bytes
Database Buffers             775946240 bytes
Redo Buffers                   5480448 bytes

sql statement: alter system set  control_files =  
 ''/tmp/backups/ORCL/controlfile/o1_mf_95f1v6h2_.ctl'' comment= ''RMAN set'' scope=spfile

Oracle instance shut down

connected to auxiliary database (not started)
Oracle instance started

Total System Global Area    1068937216 bytes

Fixed Size                     2296576 bytes
Variable Size                285213952 bytes
Database Buffers             775946240 bytes
Redo Buffers                   5480448 bytes

sql statement: alter database mount clone database

contents of Memory Script:
{
# set requested point in time
set until  time "TO_DATE('01/10/2013 09:33:39','DD/MM/RRRR HH24:MI:SS')";
# set destinations for recovery set and auxiliary set datafiles
set newname for datafile  2 to new;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  2;
switch clone datafile all;
}
executing Memory Script

executing command: SET until clause

executing command: SET NEWNAME

Starting restore at 10-OCT-13
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=87 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 00002 to /tmp/backups/ZKWH_PITR_ORCL/
datafile/o1_mf_example_%u_.dbf
channel ORA_AUX_DISK_1: reading from backup piece +FRA01/ORCL/BACKUPSET/2013_10_10/
nnndn0_tag20131010t105304_0.263.828442385
channel ORA_AUX_DISK_1: piece handle=+FRA01/ORCL/BACKUPSET/2013_10_10/
nnndn0_tag20131010t105304_0.263.828442385 tag=TAG20131010T105304
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:47
Finished restore at 10-OCT-13

datafile 2 switched to datafile copy
input datafile copy RECID=8 STAMP=828446366 file name=/tmp/backups/ZKWH_PITR_ORCL/
datafile/o1_mf_example_95f230g5_.dbf

contents of Memory Script:
{
# set requested point in time
set until  time "TO_DATE('01/10/2013 09:33:39','DD/MM/RRRR HH24:MI:SS')";
# online the datafiles restored or switched
sql clone "alter database datafile  2 online";
# recover and open resetlogs
recover clone database tablespace  "EXAMPLE", "SYSTEM", "UNDOTBS1", 
"SYSAUX" delete archivelog;
alter clone database open resetlogs;
}
executing Memory Script

executing command: SET until clause

sql statement: alter database datafile  2 online

Starting recover at 10-OCT-13
using channel ORA_AUX_DISK_1

starting media recovery

archived log for thread 1 with sequence 6 is already on disk as file +FRA01/ORCL/
ARCHIVELOG/2013_10_10/thread_1_seq_6.265.828442595
archived log for thread 1 with sequence 7 is already on disk as file +FRA01/ORCL/
ARCHIVELOG/2013_10_10/thread_1_seq_7.267.828444779
archived log file name=+FRA01/ORCL/ARCHIVELOG/2013_10_10/thread_1_seq_6.265.828442595 
thread=1 sequence=6
archived log file name=+FRA01/ORCL/ARCHIVELOG/2013_10_10/thread_1_seq_7.267.828444779 
thread=1 sequence=7
media recovery complete, elapsed time: 00:00:02
Finished recover at 10-OCT-13

database opened

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

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/tmp/backups''

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/tmp/backups''

Performing export of tables...
   EXPDP> Starting "SYS"."TSPITR_EXP_zkwh_Amet":  
   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/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 "HR"."REGIONS"                              5.523 KB       4 rows
   EXPDP> Master table "SYS"."TSPITR_EXP_zkwh_Amet" successfully loaded/unloaded
   EXPDP> ******************************************************************************
   EXPDP> Dump file set for SYS.TSPITR_EXP_zkwh_Amet is:
   EXPDP>   /tmp/backups/tspitr_zkwh_66635.dmp
   EXPDP> Job "SYS"."TSPITR_EXP_zkwh_Amet" successfully completed at 
   Thu Oct 10 12:01:56 2013 elapsed 0 00:00:51
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_zkwh_qekx" successfully loaded/unloaded
   IMPDP> Starting "SYS"."TSPITR_IMP_zkwh_qekx":  
   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE
   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA
   IMPDP> . . imported "HR"."REGIONS"                          5.523 KB       4 rows
   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_zkwh_qekx" successfully completed at 
   Thu Oct 10 12:02:38 2013 elapsed 0 00:00:30
Import completed


Removing automatic instance
Automatic instance removed
auxiliary instance file /tmp/backups/ORCL/datafile/
o1_mf_temp_95f1zkbh_.tmp deleted
auxiliary instance file /tmp/backups/ZKWH_PITR_ORCL/onlinelog/
o1_mf_3_95f24w5s_.log deleted
auxiliary instance file /tmp/backups/ZKWH_PITR_ORCL/onlinelog/
o1_mf_2_95f24tgn_.log deleted
auxiliary instance file /tmp/backups/ZKWH_PITR_ORCL/onlinelog/
o1_mf_1_95f24rz3_.log deleted
auxiliary instance file /tmp/backups/ZKWH_PITR_ORCL/datafile/
o1_mf_example_95f230g5_.dbf deleted
auxiliary instance file /tmp/backups/ORCL/datafile/
o1_mf_sysaux_95f1vmvp_.dbf deleted
auxiliary instance file /tmp/backups/ORCL/datafile/
o1_mf_undotbs1_95f1vmy3_.dbf deleted
auxiliary instance file /tmp/backups/ORCL/datafile/
o1_mf_system_95f1vmvk_.dbf deleted
auxiliary instance file /tmp/backups/ORCL/controlfile/
o1_mf_95f1v6h2_.ctl deleted
auxiliary instance file tspitr_zkwh_66635.dmp deleted
Finished recover at 10-OCT-13

RMAN>

SQL> select to_char(sysdate,'dd/mm/rrrr hh24:mi:ss') data from dual;

DATA
-------------------
10/10/2013 12:05:57

SQL> select * from hr.regions order by 1;

 REGION_ID REGION_NAME
---------- -------------------------
	 1 Europe
	 2 Americas
	 3 Asia
	 4 Middle East and Africa
 
 

Alex Zaballa é DBA e trabalha com Oracle há 14 anos, especialista em Banco de Dados Oracle e formado em Análise de Sistemas. Com sólidos conhecimentos em Banco de Dados e Sistemas operacionais, é certificado OCM Database 11G, bem como outras 110 certificações em produtos da Oracle.

Trabalha desde 2007 para a Jupiter em Angola, alocado em um projeto no Ministério das Finanças.