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恢复单表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTML (Hyper Text Mar
- 下一篇: 汽车安全带E-mark认证优秀的认证机构