RMAN恢復操作-轉自Robinson_0612的專欄

shilei1發表於2011-07-28
恢復操作       
    1.非系統表空間的恢復步驟
        a. alter datafile n offline immediate; | alter tablespace tbs_name offline immediate;
        b. restore
        c. recover
        d. alter datafile n online; | alter tablespace tbs_name online
       
        --刪除非系統表空間users的資料檔案(資料庫位於open 狀態) 
            [oracle@oradb orcl]$ pwd
            /u01/oracle/oradata/orcl
            [oracle@oradb orcl]$ rm users01.dbf    
 
            > select * from tb2;
            select * from tb2
                          *
            ERROR at line 1:
            ORA-01116: error in opening database file 4
            ORA-01110: data file 4: '/u01/oracle/oradata/orcl/users01.dbf'
            ORA-27041: unable to open file
            Linux Error: 2: No such file or directory
            Additional information: 3
 
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> sql " alter tablespace users offline immediate "; 
            4> restore tablespace users;
            5> recover tablespace users;
            6> sql " alter tablespace users online ";
            7> }
 
            > select * from tb2;
 
                    ID NAME
            ---------- ---------------
                     1 Robinson
 
            也可以使用下面的恢復方式來完成恢復     
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> sql " alter database datafile 4 offline ";
            4> restore datafile 4;
            5> recover datafile 4;
            6> sql " alter database datafile 4 online ";
            7> }
 
        如果介質恢復時,需要用的日誌尚未歸檔,需要指定聯機重做日誌檔案所在的位置       
        注意
        表空間幾種不同的離線方式
            offline normal :
                表空間離線的預設方式,將位於SGA中且與該表空間所有的資料檔案相關的資料塊(blocks)寫入到資料檔案之後再進行離線
                再次聯機時不需要做介質恢復。
            offline temporary :
                實施檢查點程式,即同樣將SGA中且與該表空間所有的資料檔案相關的資料塊(blocks)寫入到資料檔案之後再進行離線
                不保證所有的資料能夠寫入到資料檔案。再次聯機時要做介質恢復。
            offline immediate :
                不保證表空間的資料可用,也不實施檢查點程式而直接離線,再次聯機時要做介質恢復。
               
            offline temporary ,offline immediate 離線方式不適用於資料檔案
       
            > alter database datfile 4 offline immediate;
            alter database datfile 4 offline immediate
                                   *
            ERROR at line 1:
            ORA-02231: missing or invalid option to ALTER DATABASE
 
 
            > alter database datfile 4 offline temporary;
            alter database datfile 4 offline temporary
                                   *
            ERROR at line 1:
            ORA-02231: missing or invalid option to ALTER DATABAS
       
            更多關於表空間與資料檔案的管理,請參考:Oracle 表空間與資料檔案
        對於表空間存在多個資料檔案的情況,而單個或較少的資料檔案受損,應儘可能使用第二種方式來恢復.即resotre & recover datafile n
 
    2.UNDO表空間的恢復
        關於UNDO表空間的管理請參考:Oracle 回滾(ROLLBACK)和撤銷(UNDO)
        恢復步驟(undo丟失後)
            alter database datafile 2 online;
            alter database datafile 2 offline;
            select * from v$recover_file;
            restore datafile 2;   --用RMAN 完成
            
            recover datafile 2 ; 
            alter database datafile 2 online;
           
           
        --首先做一些操作,將資料填充到undo表空間
            > select * from tb2;
 
                    ID NAME
            ---------- ---------------
                     1 Jack
 
            > insert into tb2 select 2,'Jackson' from dual;
 
            > commit;
 
            > delete from tb2 where id=1;
 
            > select * from tb2;
 
                    ID NAME
            ---------- ---------------
                     2 Jackson
                    
            > ho rm $ORACLE_BASE/oradata/orcl/undotbs01.dbf
 
        注意:undo表空間不能被offline,也不能被readonly
            使用alter database datafile 2 online | offline強制執行該步驟,以告知oracle undo表空間被損壞,執行後會話被終止,如下
 
            > alter database datafile 2 online;
            alter database datafile 2 online
            *
            ERROR at line 1:
            ORA-01116: error in opening database file 2
            ORA-01110: data file 2: '/u01/oracle/oradata/orcl/undotbs01.dbf'
            ORA-27041: unable to open file
            Linux Error: 2: No such file or directory
            Additional information: 3
 
            > alter database datafile 2 offline;
            ERROR:
            ORA-03114: not connected to ORACLE
 
            alter database datafile 2 offline
            *
            ERROR at line 1:
            ORA-00603: ORACLE server session terminated by fatal error
   
        --接下面再來進行恢復
 
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore datafile 2;
            4> recover datafile 2;}
 
            starting media recovery            --還原被成功執行,介質恢復失敗,且會話被終止
            media recovery failed
            ORA-00603: ORACLE server session terminated by fatal error
 
        --重新登入到資料庫伺服器並檢視v$recover_file檢視以及進行介質恢復
            > select * from v$recover_file;
 
                 FILE# ONLINE  ONLINE_ ERROR              CHANGE# TIME
            ---------- ------- ------- --------------- ---------- ---------
                     2 OFFLINE OFFLINE UNKNOWN ERROR       493982 23-NOV-10
        
            > recover datafile 2;    --進行介質恢復,恢復後可以看到提示回話被終止
            ORA-00603: ORACLE server session terminated by fatal error
 
            > conn / as sysdba   --再次連線
            Connected.
            > select name,status from v$datafile where name like '%undo%';  --undo表空間已經處於offline狀態
 
            NAME                                          STATUS
            --------------------------------------------- -------
            /u01/oracle/oradata/orcl/undotbs01.dbf        OFFLINE
 
            > alter database datafile 2 online;
 
            Database altered.
 
            > select * from lion.tb2;
                    ID NAME
            ---------- ---------------------------------------------
                     2 Jackson
 
        可以看到表中ID為的記錄已經丟失,事實上在undo的資料檔案丟失前,該事務並沒有提交,由此可以推斷,該事務進行了隱式提交.
                    
    3.系統表空間的恢復(system ,sysaux)
        系統表空間只能在Mount狀態下來完成恢復,步驟如下
            startup mount;
            restore datafile 1;
            recover datafile 1;
            alter database open;
        --建立新表tb3,並插入記錄。其資料字典資訊則位於system表空間,資料內容位於users表空間
            > create table tb3 tablespace users as select * from tb2;
 
            > insert into tb3 select 1,'Johnson' from dual;
 
            > commit;
 
            > select * from tb3 order by id;
 
                    ID NAME
            ---------- ---------------------------------------------
                     1 Johnson
                     2 Jackson
        --刪除表空間system01.dbf,  sysaux01.dbf           
            > ho rm $ORACLE_BASE/oradata/orcl/system01.dbf
            > ho rm $ORACLE_BASE/oradata/orcl/sysaux01.dbf     
            > startup mount force; 
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore  datafile 1,3;
            4> recover  datafile 1,3;
            5> alter database open;
            6> release channel ch1;}
 
            > select * from lion.tb3 order by id;  --成功恢復後表tb3也被恢復
 
                    ID NAME
            ---------- ---------------------------------------------
                     1 Johnson
                     2 Jackson
                    
    4.控制檔案的恢復
        步驟
            connect to target db and catalog(nocatalog) db
            startup nomount
            restore controlfile [from autobackup]
            alter database mount
            recover database
            alter database open resetlogs
           
        由於控制檔案採取了自動備份策略,因此在每次備份或重大系統結果發生變化時,控制檔案將被自動備份
           
            > ho rm $ORACLE_BASE/oradata/orcl/*.ctl                --刪除所有的控制檔案*/
           
            > select file#,status from v$datafile;   --檢視v$datafile檢視時,系統已檢測到錯誤發生
            select file#,status from v$datafile
                                     *
            ERROR at line 1:
            ORA-00210: cannot open the specified control file
            ORA-00202: control file: '/u01/oracle/oradata/orcl/control01.ctl'
            ORA-27041: unable to open file
            Linux Error: 2: No such file or directory
            Additional information: 3
           
            > shutdown abort;
           
            --重新連線到RMAN,注意連線target時使用/,否則提示TNS無法解析
            [oracle@oradb dbs]$ uniread rman target / catalog   
 
            connected to target database: orcl (not started)
            connected to recovery catalog database     
           
            RMAN> startup nomount;
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore controlfile;
            4> sql " alter database mount ";
            5> recover database;
            6> sql " alter database open resetlogs ";
            7> release channel ch1;}
            
        注:在此處有可能不要介質恢復,如果提示需要介質恢復,直接在RMAN或SQLPlus下執行recover database即可
            使用open resetlogs之後,一個新的incarnation將被生成,再此建議立即全備資料庫。
       
        更多關於控制檔案
            Oracle 控制檔案(CONTROLFILE)
            Oracle 控制檔案的備份與恢復
   
    5.聯機重做日誌檔案的恢復(online redo log )
        當資料庫置為mount狀態,且將要轉換為open狀態時,資料檔案,聯機日誌檔案被開啟,因此聯機日誌的丟失可以在mount狀態完成
        恢復步驟
            a. 啟動到mount狀態(startup mount force)
            b. 還原資料庫(restore database)
            c. 恢復資料庫(recover database)
       
        下面對刪除日誌並進行恢復
            > select * from tb2;
 
                    ID NAME
            ---------- ---------------
                     2 Jackson
 
            > select current_scn from v$database;   --檢視資料庫當前的SCN
 
            CURRENT_SCN
            -----------
                1020638
 
            > insert into tb2 select 1,'Johnson' from dual;   --為表tb2新增一條記錄
 
            > commit;
 
            > select current_scn from v$database;             --資料庫當前的SCN發生了變化為
 
            CURRENT_SCN
            -----------
                1020685
               
            > select file#, checkpoint_change# from v$datafile_header;  --資料檔案頭部的checkpoint_change
 
                 FILE# CHECKPOINT_CHANGE#
            ---------- ------------------
                     1            1020368
                     2            1020368
                     3            1020368
                     4            1020368
                     5            1020368
                     6            1020368      
                    
            > ho rm -f $ORACLE_BASE/oradata/orcl/*.log      --刪除所有的日誌檔案  */
                       
            > insert into tb2 select 2,'wilson' from dual;    --為表插入新記錄
 
            > commit;                                        
 
            > select current_scn from v$database;            --資料庫當前的SCN發生了變化為
 
            CURRENT_SCN
            -----------
                1020708        
 
            > alter system archive log current;              --對日誌進行歸檔時提示錯誤發生
            alter system archive log current
            *
            ERROR at line 1:
            ORA-16038: log 1 sequence# 1 cannot be archived
            ORA-00312: online log 1 thread 1: '/u01/oracle/oradata/orcl/redo01.log'
               
            > conn / as sysdba
 
            > startup mount force;
 
            [oracle@oradb ~]$ uniread rman target / catalog     --退出RMAN後並重新連線
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore database;
            4> recover database;
            5> release channel ch1;}
 
            RMAN-06054: media recovery requesting unknown log: thread 1 seq 1 lowscn 1020365
 
            > recover database until cancel;                          --回到SQLPlus直接使用until cancel來進行恢復
 
            > alter database open resetlogs;                          --執行opensetlogs開啟資料庫
 
            > select * from lion.tb2;                          --在日誌未完成自動歸檔前,刪除日誌的後資料全部丟失
 
                    ID NAME
            ---------- ---------------
                     2 Jackson
                    
        關於單個日誌檔案丟失或日誌檔案組受損,請參考:Oracle 聯機重做日誌檔案(ONLINE LOG FILE)     
       
    6.所有資料全部丟失的處理
        步驟
            a.啟動到nomount狀態(startup nomount)
            b.還原控制檔案(restore controlfile from autobackup)
            c.還原資料(restore database)
            d.將資料切換到mount狀態(alter database mount)
            e.恢復資料庫(recover database using backup controlfile until cancel)
            f.使用open resetlogs開啟資料庫(alter database open resetlogs)
           
        --下面演示資料檔案、日誌檔案、控制檔案全部丟失的處理
            > ho rm $ORACLE_BASE/oradata/orcl/*           --刪除所有的資料檔案、日誌檔案、控制檔案*/
 
            > startup nomount ;
           
            [oracle@oradb ~]$ uniread rman target / catalog
            RMAN> startup nomount;     
            RMAN> restore controlfile from autobackup; 
            RMAN> run {
            2> allocate channel ch1 device type disk;
            3> restore database ;
            4> sql " alter database mount ";
            5> recover database ;}
 
            RMAN-06004: ORACLE error from recovery catalog database: RMAN-20003: target database incarnation not found in
                recovery catalog
 
            idle> recover database using backup controlfile until cancel;    --使用SQLPlus來完成恢復操作
 
            idle> alter database open resetlogs;

 

 

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

相關文章