oracle 12c recover table恢复单表

在 Oracle 12c 之前,如果误删一张表,常规的方法是 Flashback 闪回或 TSPITR。如果需要恢复的表空间过大,TSPITR 会耗时非常久;而开启 flashback 会消耗磁盘空间

在 12C 中oracle提供一个新功能,即RECOVER TABLE 命令;它可以针对于表及表分区按时间点进行恢复。 表可以被恢复至辅助库中,并且可以有下面的选择:

1.使用 REMAP 选项将表恢复至一个新表或者分区中。

2.只生成一个需要被恢复表的 expdp 格式的 dump 文件,选择后期再进行恢复。

恢复前需要满足以下条件:

1目标数据库必须被置于读写模式。

2目标数据库必须被置于归档模式。

3如果要恢复表或者分区,你必须拥有这些表或者分区存在后的时间的备份。

4想要恢复单个表分区, COMPATIBLE 初始化参数所在的目标库必须设置为 11.1.0 或以上。

5请确保辅助库的所在目录有足够的磁盘空间来放置system, sysaux, undo及其恢复这个表需要的其它表空间。
请注意辅助库的所在目录里的文件会在表被恢复后自动删除

可以按下面3种方式恢复:

1 SCN  2Time  3归档序列号

由于 ROOT container SYSTEM 和 UNDO 表空间被恢复至辅助库,所以 RECOVER TABLE 需要在连接到 ROOT CDB 后运行。

下面提供一个按时间点恢复的例子:

一、全库备份

RMAN> backup database;

二、删除表

在pdb上新建一张表,并插入数据。之后将表drop掉。

三、恢复单表

注意:连接rman必须以sys用户连接,而不能以 / 代替,否则会报错权限不足;并且需要连接到ROOT CDB后运行:

rman target sys/oracle@ORCL

执行下面sql后就会自动恢复单表了:

(说明1:恢复可插拔数据库pdb1  下面的用户:xyz  下面的表:haha,恢复成功后新的表名为:test_xyz)

(说明2:请确保有一个在scn 2205985之前拍摄的备份,否则会报错rman-06024)

(说明3:如果实验时选择删除cdb某个用户下的表,在下面recover table语句中,需要将用户名和表名加上引号;本例中是恢复pdb中的表,不需要加引号)

RMAN> recover table xyz.haha of pluggable database pdb1 until scn 2205985  AUXILIARY DESTINATION '/u01/recover'  REMAP TABLE xyz.haha:test_xyz;

 

Starting recover at 17-MAY-19

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 PDB1:SYSTEM

Tablespace UNDOTBS1

Tablespace PDB1:UNDOTBS1

 

Creating automatic instance, with SID='dDix'

 

initialization parameters used for automatic instance:

db_name=ORCL

db_unique_name=dDix_pitr_wybpdb1_ORCL

compatible=12.2.0

db_block_size=8192

db_files=200

diagnostic_dest=/u01/app/oracle

_system_trig_enabled=FALSE

sga_target=1408M

processes=200

db_create_file_dest=/u01/recover

log_archive_dest_1='location=/u01/recover'

enable_pluggable_database=true

_clone_one_pdb_recovery=true

#No auxiliary parameter file used

 

 

starting up automatic instance ORCL

 

Oracle instance started

 

Total System Global Area    1476395008 bytes

 

Fixed Size                     8792920 bytes

Variable Size                385877160 bytes

Database Buffers            1073741824 bytes

Redo Buffers                   7983104 bytes

Automatic instance created

 

contents of Memory Script:

{

# set requested point in time

set until  scn 2205985;

# 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 17-MAY-19

allocated channel: ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: SID=35 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/product/12.2.0/dbhome_1/dbs/c-1520108916-20190517-00

channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/product/12.2.0/dbhome_1/dbs/c-1520108916-20190517-00 tag=TAG20190517T162610

channel ORA_AUX_DISK_1: restored backup piece 1

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:04

output file name=/u01/recover/ORCL/controlfile/o1_mf_gfwyny0r_.ctl

Finished restore at 17-MAY-19

 

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  scn 2205985;

# 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  11 to new;

set newname for clone datafile  3 to new;

set newname for clone datafile  10 to new;

set newname for clone tempfile  1 to new;

set newname for clone tempfile  3 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, 11, 3, 10;

 

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

 

executing command: SET NEWNAME

 

renamed tempfile 1 to /u01/recover/ORCL/datafile/o1_mf_temp_%u_.tmp in control file

renamed tempfile 3 to /u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_temp_%u_.tmp in control file

 

Starting restore at 17-MAY-19

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/recover/ORCL/datafile/o1_mf_system_%u_.dbf

channel ORA_AUX_DISK_1: restoring datafile 00004 to /u01/recover/ORCL/datafile/o1_mf_undotbs1_%u_.dbf

channel ORA_AUX_DISK_1: restoring datafile 00003 to /u01/recover/ORCL/datafile/o1_mf_sysaux_%u_.dbf

channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/product/12.2.0/dbhome_1/dbs/0fu1pic7_1_1

channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/product/12.2.0/dbhome_1/dbs/0fu1pic7_1_1 tag=TAG20190517T161918

channel ORA_AUX_DISK_1: restored backup piece 1

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:02:04

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 00009 to /u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_system_%u_.dbf

channel ORA_AUX_DISK_1: restoring datafile 00011 to /u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_undotbs1_%u_.dbf

channel ORA_AUX_DISK_1: restoring datafile 00010 to /u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_sysaux_%u_.dbf

channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/product/12.2.0/dbhome_1/dbs/0hu1pijd_1_1

channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/product/12.2.0/dbhome_1/dbs/0hu1pijd_1_1 tag=TAG20190517T161918

channel ORA_AUX_DISK_1: restored backup piece 1

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:55

Finished restore at 17-MAY-19

 

datafile 1 switched to datafile copy

input datafile copy RECID=10 STAMP=1008522849 file name=/u01/recover/ORCL/datafile/o1_mf_system_gfwyodkp_.dbf

datafile 9 switched to datafile copy

input datafile copy RECID=11 STAMP=1008522849 file name=/u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_system_gfwys9nb_.dbf

datafile 4 switched to datafile copy

input datafile copy RECID=12 STAMP=1008522849 file name=/u01/recover/ORCL/datafile/o1_mf_undotbs1_gfwyodoo_.dbf

datafile 11 switched to datafile copy

input datafile copy RECID=13 STAMP=1008522849 file name=/u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_undotbs1_gfwys9ol_.dbf

datafile 3 switched to datafile copy

input datafile copy RECID=14 STAMP=1008522849 file name=/u01/recover/ORCL/datafile/o1_mf_sysaux_gfwyodob_.dbf

datafile 10 switched to datafile copy

input datafile copy RECID=15 STAMP=1008522849 file name=/u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_sysaux_gfwys9ln_.dbf

 

contents of Memory Script:

{

# set requested point in time

set until  scn 2205985;

# online the datafiles restored or switched

sql clone "alter database datafile  1 online";

sql clone 'WYBPDB1' "alter database datafile

 9 online";

sql clone "alter database datafile  4 online";

sql clone 'WYBPDB1' "alter database datafile

 11 online";

sql clone "alter database datafile  3 online";

sql clone 'WYBPDB1' "alter database datafile

 10 online";

# recover and open database read only

recover clone database tablespace  "SYSTEM", "WYBPDB1":"SYSTEM", "UNDOTBS1", "WYBPDB1":"UNDOTBS1", "SYSAUX", "WYBPDB1":"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  11 online

 

sql statement: alter database datafile  3 online

 

sql statement: alter database datafile  10 online

 

Starting recover at 17-MAY-19

using channel ORA_AUX_DISK_1

 

starting media recovery

 

archived log for thread 1 with sequence 17 is already on disk as file /u01/app/oracle/product/12.2.0/dbhome_1/dbs/arch/1_17_992328185.dbf

archived log file name=/u01/app/oracle/product/12.2.0/dbhome_1/dbs/arch/1_17_992328185.dbf thread=1 sequence=17

media recovery complete, elapsed time: 00:00:15

Finished recover at 17-MAY-19

 

sql statement: alter database open read only

 

contents of Memory Script:

{

sql clone 'alter pluggable database  WYBPDB1 open read only';

}

executing Memory Script

 

sql statement: alter pluggable database  WYBPDB1 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/recover/ORCL/controlfile/o1_mf_gfwyny0r_.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    1476395008 bytes

 

Fixed Size                     8792920 bytes

Variable Size                385877160 bytes

Database Buffers            1073741824 bytes

Redo Buffers                   7983104 bytes

 

sql statement: alter system set  control_files =   ''/u01/recover/ORCL/controlfile/o1_mf_gfwyny0r_.ctl'' comment= ''RMAN set'' scope=spfile

 

Oracle instance shut down

 

connected to auxiliary database (not started)

Oracle instance started

 

Total System Global Area    1476395008 bytes

 

Fixed Size                     8792920 bytes

Variable Size                385877160 bytes

Database Buffers            1073741824 bytes

Redo Buffers                   7983104 bytes

 

sql statement: alter database mount clone database

 

contents of Memory Script:

{

# set requested point in time

set until  scn 2205985;

# set destinations for recovery set and auxiliary set datafiles

set newname for datafile  12 to new;

# restore the tablespaces in the recovery set and the auxiliary set

restore clone datafile  12;

 

switch clone datafile all;

}

executing Memory Script

 

executing command: SET until clause

 

executing command: SET NEWNAME

 

Starting restore at 17-MAY-19

allocated channel: ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: SID=35 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 00012 to /u01/recover/DDIX_PITR_WYBPDB1_ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_users_%u_.dbf

channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/product/12.2.0/dbhome_1/dbs/0hu1pijd_1_1

channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/product/12.2.0/dbhome_1/dbs/0hu1pijd_1_1 tag=TAG20190517T161918

channel ORA_AUX_DISK_1: restored backup piece 1

channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:07

Finished restore at 17-MAY-19

 

datafile 12 switched to datafile copy

input datafile copy RECID=17 STAMP=1008523004 file name=/u01/recover/DDIX_PITR_WYBPDB1_ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_users_gfwyzn6s_.dbf

 

contents of Memory Script:

{

# set requested point in time

set until  scn 2205985;

# online the datafiles restored or switched

sql clone 'WYBPDB1' "alter database datafile

 12 online";

# recover and open resetlogs

recover clone database tablespace  "WYBPDB1":"USERS", "SYSTEM", "WYBPDB1":"SYSTEM", "UNDOTBS1", "WYBPDB1":"UNDOTBS1", "SYSAUX", "WYBPDB1":"SYSAUX" delete archivelog;

alter clone database open resetlogs;

}

executing Memory Script

 

executing command: SET until clause

 

sql statement: alter database datafile  12 online

 

Starting recover at 17-MAY-19

using channel ORA_AUX_DISK_1

 

starting media recovery

 

archived log for thread 1 with sequence 17 is already on disk as file /u01/app/oracle/product/12.2.0/dbhome_1/dbs/arch/1_17_992328185.dbf

archived log file name=/u01/app/oracle/product/12.2.0/dbhome_1/dbs/arch/1_17_992328185.dbf thread=1 sequence=17

media recovery complete, elapsed time: 00:00:03

Finished recover at 17-MAY-19

 

database opened

 

contents of Memory Script:

{

sql clone 'alter pluggable database  WYBPDB1 open';

}

executing Memory Script

 

sql statement: alter pluggable database  WYBPDB1 open

 

contents of Memory Script:

{

# create directory for datapump import

sql 'WYBPDB1' "create or replace directory

TSPITR_DIROBJ_DPDIR as ''

/u01/recover''";

# create directory for datapump export

sql clone 'WYBPDB1' "create or replace directory

TSPITR_DIROBJ_DPDIR as ''

/u01/recover''";

}

executing Memory Script

 

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

 

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

 

Performing export of tables...

   EXPDP> Starting "SYS"."TSPITR_EXP_dDix_Eoqj": 

   EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA

   EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS

   EXPDP> Processing object type TABLE_EXPORT/TABLE/STATISTICS/MARKER

   EXPDP> Processing object type TABLE_EXPORT/TABLE/TABLE

   EXPDP> . . exported "XYZ"."HAHA"                                5.078 KB       3 rows

   EXPDP> Master table "SYS"."TSPITR_EXP_dDix_Eoqj" successfully loaded/unloaded

   EXPDP> ******************************************************************************

   EXPDP> Dump file set for SYS.TSPITR_EXP_dDix_Eoqj is:

   EXPDP>   /u01/recover/tspitr_dDix_71707.dmp

   EXPDP> Job "SYS"."TSPITR_EXP_dDix_Eoqj" successfully completed at Fri May 17 17:20:23 2019 elapsed 0 00:00:59

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_dDix_swoa" successfully loaded/unloaded

   IMPDP> Starting "SYS"."TSPITR_IMP_dDix_swoa": 

   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE

   IMPDP> Processing object type TABLE_EXPORT/TABLE/TABLE_DATA

   IMPDP> . . imported "XYZ"."TEST_XYZ"                            5.078 KB       3 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_dDix_swoa" successfully completed at Fri May 17 17:23:00 2019 elapsed 0 00:01:37

Import completed

 

 

Removing automatic instance

Automatic instance removed

auxiliary instance file /u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_temp_gfwywprk_.tmp deleted

auxiliary instance file /u01/recover/ORCL/datafile/o1_mf_temp_gfwyvw34_.tmp deleted

auxiliary instance file /u01/recover/DDIX_PITR_WYBPDB1_ORCL/onlinelog/o1_mf_3_gfwz16z7_.log deleted

auxiliary instance file /u01/recover/DDIX_PITR_WYBPDB1_ORCL/onlinelog/o1_mf_2_gfwz06h3_.log deleted

auxiliary instance file /u01/recover/DDIX_PITR_WYBPDB1_ORCL/onlinelog/o1_mf_1_gfwz06h0_.log deleted

auxiliary instance file /u01/recover/DDIX_PITR_WYBPDB1_ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_users_gfwyzn6s_.dbf deleted

auxiliary instance file /u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_sysaux_gfwys9ln_.dbf deleted

auxiliary instance file /u01/recover/ORCL/datafile/o1_mf_sysaux_gfwyodob_.dbf deleted

auxiliary instance file /u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_undotbs1_gfwys9ol_.dbf deleted

auxiliary instance file /u01/recover/ORCL/datafile/o1_mf_undotbs1_gfwyodoo_.dbf deleted

auxiliary instance file /u01/recover/ORCL/7ABD0C12921F11E2E0533238A8C01F5F/datafile/o1_mf_system_gfwys9nb_.dbf deleted

auxiliary instance file /u01/recover/ORCL/datafile/o1_mf_system_gfwyodkp_.dbf deleted

auxiliary instance file /u01/recover/ORCL/controlfile/o1_mf_gfwyny0r_.ctl deleted

auxiliary instance file tspitr_dDix_71707.dmp deleted

Finished recover at 17-MAY-19

至此单表恢复成功。

 ========请注意:如果你不想对这个表做import,只想做export,那么可以使用notableimport选项

参考下面sql:

run {RECOVER TABLE SMEDS."RECTEST" OF PLUGGABLE DATABASE T12CPDB1

UNTIL SEQUENCE 64 thread 1

AUXILIARY DESTINATION '/u01/abc/rectbl'

datapump destination '/u01/abc/rectb/dpump/'

dump file 'export.dmp'

notableimport; }

 

你可能感兴趣的:(oracle 12c recover table恢复单表)