熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Oracle >> 正文

無備份丟失部分數據文件和控制文件恢復

2022-06-13   來源: Oracle 

  前提:

          需要有除丟失文件以外其他文件的備份

          丟失的文件需要是在其他文件備份後創建的

          所有其他文件備份後的歸檔都在存在

  恢復步驟:

  拷貝任一未丟失文件的備份回來

  創建新控制文件但控制文件不包括丟失的數據文件

  進行恢復

  碰到

  ORA: recovery session canceled due to errors
    ORA: unnamed datafile(s) added to controlfile by media recovery
    ORA: data file : D:\ORACLE\ORADATA\xxxx\xxxxDBF

  select name from v$datafile找出uname file

   alter database create datafile D:\ORACLE\ORA\DATABASE\UNNAMEDxxxxx as D:\ORACLE\ORADATA\ xxxx\xxxxDBF ;

  繼續恢復

  恢復完成打開

  可能會遇到的問題:

  如果沒有拷貝備份回來直接重建不包含丟失文件的控制文件時可以打開數據庫但丟失的文件會顯示成missingxxxx的file name這時候就會不能恢復

  試驗過程:

  Microsoft Windows XP [版本 ]

  (C) 版權所有 Microsoft Corp

  C:\Documents and Settings\qigong>oradim startup sid test

  C:\Documents and Settings\qigong>sqlplus / as sysdba

  SQL*Plus: Release Production on 星期三 ::

  Copyright (c) Oracle  All rights reserved

  Connected to:
    Oracle Database g Enterprise Edition Release Production
    With the Partitioning OLAP and Data Mining options

  SQL> shutdown immediate;
    Database closed
    Database dismounted
    ORACLE instance shut down
    SQL> exit
    Disconnected from Oracle Database g Enterprise Edition Release Producti
    With the Partitioning OLAP and Data Mining options

  C:\Documents and Settings\qigong>sqlplus / as sysdba

  SQL*Plus: Release Production on 星期三 ::

  Copyright (c) Oracle  All rights reserved

  Connected to an idle instance

  SQL> startup nomount;
    ORACLE instance started

  Total System Global Area  bytes
    Fixed Size                   bytes
    Variable Size              bytes
    Database Buffers           bytes
    Redo Buffers                 bytes
    SQL> alter database mount;
    alter database mount
    *
    ERROR at line :
    ORA: error in identifying controlfile check alert log for more info

  SQL> shutdown
    ORA: database not mounted

  ORACLE instance shut down
    SQL> startup
    ORACLE instance started

  Total System Global Area  bytes
    Fixed Size                   bytes
    Variable Size              bytes
    Database Buffers           bytes
    Redo Buffers                 bytes
    Database mounted
    Database opened
    SQL> alter system switch logfile;

  System altered

  SQL> /

  System altered

  SQL> /

  System altered

  SQL> create tablespace test
      datafile  c:\testdbf size m
               extent management local
               segment space management auto
               uniform size k;

  Tablespace created

  SQL> insert into testlost values();

   row created

  SQL> commit;

  Commit complete

  SQL> create table testlost(a number) tablespace test;

  Table created

  SQL> shutdown immediate;
    Database closed
    Database dismounted
    ORACLE instance shut down
    SQL> startup nomount
    ORACLE instance started

  Total System Global Area  bytes
    Fixed Size                   bytes
    Variable Size              bytes
    Database Buffers           bytes
    Redo Buffers                 bytes

  C:\Documents and Settings\qigong>rman

  Recovery Manager: Release Production

  Copyright (c) Oracle  All rights reserved

  RMAN> connect target

  connected to target database: test (not mounted)

  RMAN> restore controlfile from autobackup;

  Starting restore at
    using target database controlfile instead of recovery catalog
    allocated channel: ORA_DISK_
    channel ORA_DISK_: sid= devtype=DISK

  recovery area destination: C:\oracle\product\\flash_recovery_area
    database name (or lock name space) used for search: TEST
    channel ORA_DISK_: autobackup found in the recovery area
    channel ORA_DISK_: autobackup found: C:\ORACLE\PRODUCT\    \FLASH_RECOVERY_AREA\TEST\AUTOBACKUP\__\O_MF_S__LOSFCX_BKP
    channel ORA_DISK_: controlfile restore from autobackup complete
    output filename=C:\ORACLE\PRODUCT\\ORADATA\TEST\CONTROLCTL
    output filename=C:\ORACLE\PRODUCT\\ORADATA\TEST\CONTROLCTL
    output filename=C:\ORACLE\PRODUCT\\ORADATA\TEST\CONTROLCTL
    Finished restore at

  RMAN> exit

  RMAN: WARNING: unable to generate V$RMAN_STATUS or V$RMAN_OUTPUT row
    RMAN: WARNING: disabling update of the V$RMAN_STATUS and V$RMAN_OUTPUT rows

  SQL> alter database mount;
    alter database mount
    *
    ERROR at line :
    ORA: invalid password file
    C:\oracle\product\\Db_\DATABASE\PWDtestORA

  SQL> DECLARE
      devtype varchar();
      done boolean;
      BEGIN
      devtype:=sysdbms_backup_restoredeviceAllocate (type=>ident=>);
      sysdbms_backup_restorerestoreSetDatafile;
      sysdbms_backup_restorerestoreDatafileTo(dfnumber=>toname=>c:\testDBF);
      sysdbms_backup_restorerestoreBackupPiece(done=>donehandle=>C:\FUATK___
      sysdbms_backup_restoredeviceDeallocate;
      END;
      /

  PL/SQL procedure successfully completed

  SQL> shutdown immediate;
    ORA: database not open

    Database dismounted
    ORACLE instance shut down
    SQL> startup nomount
    ORACLE instance started

  Total System Global Area  bytes
    Fixed Size                   bytes
    Variable Size              bytes
    Database Buffers           bytes
    Redo Buffers                 bytes
    SQL> CREATE CONTROLFILE REUSE DATABASE TEST NORESETLOGS  ARCHIVELOG
          MAXLOGFILES
          MAXLOGMEMBERS
          MAXDATAFILES
          MAXINSTANCES
          MAXLOGHISTORY
      LOGFILE
        GROUP C:\ORACLE\PRODUCT\\ORADATA\TEST\REDOLOG  SIZE M
        GROUP C:\ORACLE\PRODUCT\\ORADATA\TEST\REDOLOG  SIZE M
        GROUP C:\ORACLE\PRODUCT\\ORADATA\TEST\REDOLOG  SIZE M
      STANDBY LOGFILE
      DATAFILE
        C:\ORACLE\PRODUCT\\ORADATA\TEST\SYSTEMDBF
        C:\ORACLE\PRODUCT\\ORADATA\TEST\UNDOTBSDBF
        C:\ORACLE\PRODUCT\\ORADATA\TEST\SYSAUXDBF
        C:\ORACLE\PRODUCT\\ORADATA\TEST\USERSDBF
        C:\ORACLE\PRODUCT\\ORADATA\TEST\EXAMPLEDBF
        C:\TBSDBF
        C:\TBSDBF
        C:\TBSDBF
        C:\TBSDBF
        C:\TBSDBF
        C:\TBSDBF
        C:\TBSDBF
        C:\TBSDBF
        C:\TESTDBF
      CHARACTER SET ZHSGBK
      ;

  Control file created

  SQL> recover database;
    ORA: change generated at // :: needed for thread
    ORA: suggestion : C:\ORACLE\ARCHIVE\ARC_
    ORA: change for thread is in sequence #

  Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
    auto
    ORA: recovery session canceled due to errors
    ORA: unnamed datafile(s) added to controlfile by media recovery
    ORA: data file : C:\TESTDBF

  ORA: media recovery not started

  SQL> select name from v$datafile;

  NAME
   
    C:\ORACLE\PRODUCT\\ORADATA\TEST\SYSTEMDBF
    C:\ORACLE\PRODUCT\\ORADATA\TEST\UNDOTBSDBF
    C:\ORACLE\PRODUCT\\ORADATA\TEST\SYSAUXDBF
    C:\ORACLE\PRODUCT\\ORADATA\TEST\USERSDBF
    C:\ORACLE\PRODUCT\\ORADATA\TEST\EXAMPLEDBF
    C:\TBSDBF
    C:\TBSDBF
    C:\TBSDBF
    C:\TBSDBF
    C:\TBSDBF
    C:\TBSDBF

  NAME
   
    C:\TBSDBF
    C:\TBSDBF
    C:\TESTDBF
    C:\WINDOWS\SYSTEM\UNNAMED

   rows selected

  SQL> alter database create datafile C:\WINDOWS\SYSTEM\UNNAMED as c:\testdb

  Database altered

  SQL> recover database;
    Media recovery complete
    SQL> alter database open;

  Database altered

  SQL> select * from testlost;

  no rows selected

  SQL> select * from testlost;

  A
   
   
   

    在上面這個試驗裡面我們可能還會有一個疑問既然控制文件裡面記載的是unamedxxxxxxx的文件名數據字典的裡面的filename也是 unamedxxxxxxx(實際上數據字典裡面不保存文件名所有的文件名都只包含在控制文件當中)日志裡也是沒有記載文件名那麼當我們 recover database的時候為什麼會出現

  ORA: recovery session canceled due to errors

  ORA: unnamed datafile(s) added to controlfile by media recovery

  ORA: data file : C:\TESTDBF

  ORA: media recovery not started

  Oracle他是從哪裡得到這個unamedxxxxxxx文件的真實文件是C:\TESTDBF呢?

  先來看看日志裡記載了什麼東西

  用logminer查一下

  EXECUTE DBMS_LOGMNR_DBUILD(dictionary_filename => l_dictionaryoradictionary_location => /disk/oradata/);

  EXECUTE DBMS_LOGMNRADD_LOGFILE( LogFileName => /disk/oradata/redologOptions => dbms_logmnrNEW);

  EXECUTE DBMS_LOGMNRSTART_LOGMNR(DictFileName =>/disk/oradata/l_dictionaryora);

  SELECT sql_redo FROM V$LOGMNR_CONTENTS where upper(sql_redo) like %TEST%;

  SQL_REDO
   
    create tablespace test
    datafile  c:\testdbf size m reuse
    extent management local
    segment space management auto
    uniform size k;
    只發現了這句ddl並沒有往數據字典裡插入文件名

  再來看create tablespace的trace文件
    ……
    insert into ts$ (ts#nameonline$contents$undofile#undoblock#blocksize
    dflmaxextdflinitdflincrdflextpctdflminextdflminleninc#owner#scnwrp
    scnbaspitrscnwrppitrscnbasdflogging affstrengthbitmappedplugged
    directallowedflagssparespare)
    values
    (::::::::::::::::::::::::)

  insert into file$ (file#blocksts#status$relfile#maxextendinccrscnwrpcrscnbasspare)
    values
    (::DECODE(:NULL:): DECODE(:NULL:)::::DECODE(:NULL:))
    ……

  這兩條語句說明了oracle在ts$file$分別插入了新創建表空間和文件的信息但並不包括文件名

  看起來好像這個文件名是無出處的即不直接取至日志也不存在於數據字典控制文件中則存在著錯誤的文件名那他究竟從哪裡來呢?

  看下控制文件中包含這個文件的dump 信息

  DATA FILE #:
    (name #) C:\WINDOWS\SYSTEM\UNNAMED
    creation size= block size= status=x head= tail= dup=
    tablespace index= krfil= prev_file=
    unrecoverable scn: x // ::
    Checkpoint cnt: scn: xde // ::
    Stop scn: xde // ::
    Creation Checkpointed at scn:  xc // ::

  從這裡我們知道testdbf是號文件這樣的話oracle可以從file$表中找到這個文件對應的ts#再從ts$中找到表空間的名字然後回到日志對應日志中的ddl即可以得到錯誤的文件是c:\testdbf答案出來了!oracle根據這麼一套流程找到了正確的文件名並提示給用戶這樣的話我們只需要很方便的create datafile reuse一下就可以繼續恢復進程了


From:http://tw.wingwit.com/Article/program/Oracle/201311/16821.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.