oracle 12c recover table恢复单表
在 Oracle?12c 之前,如果誤刪一張表,常規(guī)的方法是 Flashback 閃回或 TSPITR。如果需要恢復(fù)的表空間過(guò)大,TSPITR 會(huì)耗時(shí)非常久;而開(kāi)啟 flashback 會(huì)消耗磁盤空間
在 12C 中oracle提供一個(gè)新功能,即RECOVER TABLE 命令;它可以針對(duì)于表及表分區(qū)按時(shí)間點(diǎn)進(jìn)行恢復(fù)。 表可以被恢復(fù)至輔助庫(kù)中,并且可以有下面的選擇:
1.使用 REMAP 選項(xiàng)將表恢復(fù)至一個(gè)新表或者分區(qū)中。
2.只生成一個(gè)需要被恢復(fù)表的 expdp 格式的 dump 文件,選擇后期再進(jìn)行恢復(fù)。
恢復(fù)前需要滿足以下條件:
1目標(biāo)數(shù)據(jù)庫(kù)必須被置于讀寫模式。
2目標(biāo)數(shù)據(jù)庫(kù)必須被置于歸檔模式。
3如果要恢復(fù)表或者分區(qū),你必須擁有這些表或者分區(qū)存在后的時(shí)間的備份。
4想要恢復(fù)單個(gè)表分區(qū), COMPATIBLE 初始化參數(shù)所在的目標(biāo)庫(kù)必須設(shè)置為 11.1.0 或以上。
5請(qǐng)確保輔助庫(kù)的所在目錄有足夠的磁盤空間來(lái)放置system, sysaux, undo及其恢復(fù)這個(gè)表需要的其它表空間。
請(qǐng)注意輔助庫(kù)的所在目錄里的文件會(huì)在表被恢復(fù)后自動(dòng)刪除
可以按下面3種方式恢復(fù):
1 SCN? 2Time? 3歸檔序列號(hào)
由于 ROOT container SYSTEM 和 UNDO 表空間被恢復(fù)至輔助庫(kù),所以 RECOVER TABLE 需要在連接到 ROOT CDB 后運(yùn)行。
下面提供一個(gè)按時(shí)間點(diǎn)恢復(fù)的例子:
一、全庫(kù)備份
RMAN> backup database;
二、刪除表
在pdb上新建一張表,并插入數(shù)據(jù)。之后將表drop掉。
三、恢復(fù)單表
注意:連接rman必須以sys用戶連接,而不能以 / 代替,否則會(huì)報(bào)錯(cuò)權(quán)限不足;并且需要連接到ROOT CDB后運(yùn)行:
rman target sys/oracle@ORCL
執(zhí)行下面sql后就會(huì)自動(dòng)恢復(fù)單表了:
(說(shuō)明1:恢復(fù)可插拔數(shù)據(jù)庫(kù)pdb1? 下面的用戶:xyz? 下面的表:haha,恢復(fù)成功后新的表名為:test_xyz)
(說(shuō)明2:請(qǐng)確保有一個(gè)在scn 2205985之前拍攝的備份,否則會(huì)報(bào)錯(cuò)rman-06024)
(說(shuō)明3:如果實(shí)驗(yàn)時(shí)選擇刪除cdb某個(gè)用戶下的表,在下面recover table語(yǔ)句中,需要將用戶名和表名加上引號(hào);本例中是恢復(fù)pdb中的表,不需要加引號(hào))
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
至此單表恢復(fù)成功。
?========請(qǐng)注意:如果你不想對(duì)這個(gè)表做import,只想做export,那么可以使用notableimport選項(xiàng)
參考下面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; }
?
總結(jié)
以上是生活随笔為你收集整理的oracle 12c recover table恢复单表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: HTML (Hyper Text Mar
- 下一篇: 汽车安全带E-mark认证优秀的认证机构