DBMS_BACKUP_RESTORE.DBMS_BACKUP_RESTORE用於特殊情況下的恢復

不一樣的天空w發表於2018-12-18

一般如下:系統崩潰,rman使用控制檔案,沒有使用控制檔案自動備份,現在僅有最後一次全備(備份中包括控制檔案),以及其增量備份,規檔備份.
通常這種情況下不能使用常規RMAN來恢復,因為此全備份中備份的控制檔案中沒有包含本次的備份資訊,rman使用控制檔案備份的時候是先備份控制檔案
後備份其它資訊

背景知識
在Oracle 816 以後的版本中,Oracle提供了一個包:DBMS_BACKUP_RESTORE.DBMS_BACKUP_RESTORE 包是由dbmsbkrs.sql 和 prvtbkrs.plb 這兩個指令碼建立的.catproc.sql 指令碼執行後會呼叫這兩個包.所以是每個資料庫都有的這個包是Oracle伺服器和作業系統之間IO操作的介面.由恢復管理器直接呼叫。而且據說這兩個指令碼的功能是內建到Oracle的一些庫檔案中的.

由此可見,我們可以在資料庫 nomount 情況下呼叫這些package ,來達到我們的恢復目的。在dbmsbkrs.sql 和prvtbkrs.plb 這兩個指令碼中有詳細的說明文件

關鍵的內容有:

FUNCTION  deviceAllocate(
       type IN varchar2 default NULL
      ,name IN varchar2 default NULL
      ,ident IN varchar2 default NULL
      ,noio IN boolean default FALSE
      ,params IN varchar2 default NULL )
RETURN varchar2;


PROCEDURE restoreControlfileTo(cfname IN varchar2);

PROCEDURE restoreDataFileTo( dfnumber IN binary_integer
,toname IN varchar2 default NULL);

SQL>startup force nomount;
SQL>
DECLARE
devtype varchar2(256);
done boolean;
BEGIN
--分配一個device channel,如果使用的作業系統檔案,type就為空,如果是從磁帶上恢復要用 "sbt_tape"; 
devtype:=sys.dbms_backup_restore.deviceAllocate(type=>'',ident=>'t1'); 
--指明開始restore
sys.dbms_backup_restore.restoreSetDatafile;
--指出待恢復檔案目標儲存位置;
sys.dbms_backup_restore.restoreControlfileTo(cfname=>'D:ORACLEORADATAFENETCONTROL01.CTL');
--sys.dbms_backup_restore.restoreControlfileTo(cfname=>'D:ORACLEORADATAFENETCONTROL02.CTL');
--sys.dbms_backup_restore.restoreControlfileTo(cfname=>'D:ORACLEORADATAFENETCONTROL03.CTL');
--指定備份集的位置
sys.dbms_backup_restore.restoreBackupPiece(done=>done,handle=>'D:ORA9I6095222264.RMAN', params=>null);
--釋放通道
sys.dbms_backup_restore.deviceDeallocate;
END;

可以透過該語句得到file#和name的對應關係
select 'sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>' || file# ||
',toname=>' ||chr(39)|| name ||chr(39) || ');',
'sys.dbms_backup_restore.applySetDatafile(dfnumber=>' || file# ||
',toname=>' ||chr(39)|| name ||chr(39) || ');'
from v$datafile; 

在nomount狀態下執行以下語句
恢復0級備份的語句
DECLARE
devtype varchar2(256);
done boolean;
BEGIN
devtype:=sys.dbms_backup_restore.deviceAllocate (type=>'',ident=>'t1');
sys.dbms_backup_restore.restoreSetDatafile;
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>1,toname=>'D:ORACLEORADATAFENETSYSTEM01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>2,toname=>'D:ORACLEORADATAFENETUNDOTBS01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>3,toname=>'D:ORACLEORADATAFENETCWMLITE01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>4,toname=>'D:ORACLEORADATAFENETDRSYS01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>5,toname=>'D:ORACLEORADATAFENETEXAMPLE01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>6,toname=>'D:ORACLEORADATAFENETINDX01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>7,toname=>'D:ORACLEORADATAFENETODM01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>8,toname=>'D:ORACLEORADATAFENETTOOLS01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>9,toname=>'D:ORACLEORADATAFENETUSERS01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>10,toname=>'D:ORACLEORADATAFENETXDB01.DBF');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>11,toname=>'D:ORACLEORADATAFENETBJIC.ORA');
sys.dbms_backup_restore.restoreDatafileTo(dfnumber=>12,toname=>'D:ORACLEORADATAFENETPM_USERS.DBF');
sys.dbms_backup_restore.restoreBackupPiece(done=>done,handle=>'D:ORA9I6095222264.RMAN', params=>null);
sys.dbms_backup_restore.deviceDeallocate;
END;
恢復增量備份的語句
DECLARE
devtype varchar2(256);
done boolean;
BEGIN
devtype:=sys.dbms_backup_restore.deviceAllocate (type=>'',ident=>'t1');
sys.dbms_backup_restore.applySetDatafile;
sys.dbms_backup_restore.applySetDatafile(dfnumber=>1,toname=>'D:ORACLEORADATAFENETSYSTEM01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>2,toname=>'D:ORACLEORADATAFENETUNDOTBS01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>3,toname=>'D:ORACLEORADATAFENETCWMLITE01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>4,toname=>'D:ORACLEORADATAFENETDRSYS01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>5,toname=>'D:ORACLEORADATAFENETEXAMPLE01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>6,toname=>'D:ORACLEORADATAFENETINDX01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>7,toname=>'D:ORACLEORADATAFENETODM01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>8,toname=>'D:ORACLEORADATAFENETTOOLS01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>9,toname=>'D:ORACLEORADATAFENETUSERS01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>10,toname=>'D:ORACLEORADATAFENETXDB01.DBF');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>11,toname=>'D:ORACLEORADATAFENETBJIC.ORA');
sys.dbms_backup_restore.applySetDatafile(dfnumber=>12,toname=>'D:ORACLEORADATAFENETPM_USERS.DBF');
sys.dbms_backup_restore.applyBackupPiece(done=>done,handle=>'D:ORA9I6095222264.L1', params=>null);
sys.dbms_backup_restore.deviceDeallocate
END;

恢復歸檔日誌archive log檔案

SQL>DECLARE
devtype varchar2(256);
done boolean;
BEGIN
devtype:=sys.dbms_backup_restore.deviceAllocate(type=>'',ident=>'T1');
sys.dbms_backup_restore.restoreSetArchivedLog;
sys.dbms_backup_restore.restoreArchivedLogRange;
sys.dbms_backup_restore.restoreBackupPiece(done=>done,handle=>'D:ORA9I6095222264.arc',params=>null);
sys.dbms_backup_restore.deviceDeallocate;
END

mount資料庫
SQL> alter database mount;
恢復資料庫到某一時間點
SQL> >recover database until time '2006-12-14 10:00:00';
啟動資料庫
SQL> alter database open resetlogs;


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31397003/viewspace-2285529/,如需轉載,請註明出處,否則將追究法律責任。

相關文章