国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > Oracle > 正文

ORACLE備份&恢復案例--ORACLE BACKUP&RESTORE SCHEME

2024-08-29 13:41:51
字體:
來源:轉載
供稿:網友
轉貼 Oracle備份&恢復案例--ORACLE BACKUP&RESTORE SCHEME 第一章. 理解什么是數據庫恢復
當我們使用一個數據庫時,總希望數據庫的內容是可靠的、正確的,但由于計算機系統的故障(硬件故障、軟件故障、網絡故障、進程故障和系統故障)影響數據庫系統的操作,影響數據庫中數據的正確性,甚至破壞數據庫,使數據庫中全部或部分數據丟失。因此當發生上述故障后,希望能重構這個完整的數據庫,該處理稱為數據庫恢復。恢復過程大致可以分為復原(Restore)與恢復(Restore)過程。
數據庫恢復可以分為以下兩類:
1.1實例故障的一致性恢復
當實例意外地(如掉電、后臺進程故障等)或預料地(發出SHUTDOUM ABORT語句)中止時出現實例故障,此時需要實例恢復。實例恢復將數據庫恢復到故障之前的事務一致狀態。假如在在線后備發現實例故障,則需介質恢復。在其它情況ORACLE在下次數據庫起動時(對新實例裝配和打開),自動地執行實例恢復。假如需要,從裝配狀態變為打開狀態,自動地激發實例恢復,由下列處理:
(1) 為了解恢復數據文件中沒有記錄的數據,進行向前滾。該數據記錄在在線日志,包括對回滾段的內容恢復。
(2) 回滾未提交的事務,按步1重新生成回滾段所指定的操作。
(3) 釋放在故障時正在處理事務所持有的資源。
(4) 解決在故障時正經歷一階段提交的任何懸而未決的分布事務。
1.2介質故障或文件錯誤的不一致恢復
介質故障是當一個文件、一個文件的部分或磁盤不能讀或不能寫時出現的故障。
文件錯誤一般指意外的錯誤導致文件被刪除或意外事故導致文件的不一致。
這種狀態下的數據庫都是不一致的,需要DBA手工來進行數據庫的恢復,這種恢復有兩種形式,決定于數據庫運行的歸檔方式和備份方式。
(1) 完全介質恢復可恢復全部丟失的修改。一般情況下需要有數據庫的備份且數據庫運行在歸檔狀態下并且有可用歸檔日志時才可能。對于不同類型的錯誤,有不同類型的完全恢復可使用,其決定于毀壞文件和數據庫的可用性。
(2) 不完全介質恢復是在完全介質恢復不可能或不要求時進行的介質恢復。重構受損的數據庫,使其恢復介質故障前或用戶出錯之前的一個事務一致性狀態。不完全介質恢復有不同類型的使用,決定于需要不完全介質恢復的情況,有下列類型:基于撤消、基于時間和基于修改的不完全恢復。
基于撤消(CANCEL)恢復:在某種情況,不完全介質恢復必須被控制,DBA可撤消在指定點的操作。基于撤消的恢復地在一個或多個日志組(在線的或歸檔的)已被介質故障所破壞,不能用于恢復過程時使用,所以介質恢復必須控制,以致在使用最近的、未損的日志組于數據文件后中止恢復操作。
基于時間(TIME)和基于修改(SCN)的恢復:假如DBA希望恢復到過去的某個指定點,是一種理想的不完全介質恢復,一般發生在恢復到某個特定操作之前,恢復到如意外刪除某個數據表之前。
  
第二章. 數據庫恢復案例測試環境
2.1 數據庫環境
以下的所有案例都是通過測試經過,環境為:
   OS:windows 2000 Server
   DB:Oracle 816
   DBNAME:TEST
數據文件:
SQL> select file#,status,enabled,name from v$datafile;
  
      FILE# STATUS  ENABLED    NAME
---------- ------- ---------- --------------------------------------------------------------------------------
          1 SYSTEM  READ WRITE D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF
          2 ONLINE  READ WRITE D:/ORACLE/ORADATA/TEST/RBS01.DBF
          3 ONLINE  READ WRITE D:/ORACLE/ORADATA/TEST/USERS01.DBF
          4 ONLINE  READ WRITE D:/ORACLE/ORADATA/TEST/TEMP01.DBF
          5 ONLINE  READ WRITE D:/ORACLE/ORADATA/TEST/TOOLS01.DBF
          6 ONLINE  READ WRITE D:/ORACLE/ORADATA/TEST/INDX01.DBF
控制文件:
SQL> select * from v$controlfile;
  
STATUS  NAME
------- --------------------------------------------------------------------------------

      D:/ORACLE/ORADATA/TEST/CONTROL01.CTL
      D:/ORACLE/ORADATA/TEST/CONTROL02.CTL
      D:/ORACLE/ORADATA/TEST/CONTROL03.CTL
聯機日志:
SQL> select * from v$logfile;
  
     GROUP# STATUS  MEMBER
---------- ------- --------------------------------------------------------------------------------
       1 STALE   D:/ORACLE/ORADATA/TEST/REDO01.LOG
       2          D:/ORACLE/ORADATA/TEST/REDO02.LOG
       3 STALE   D:/ORACLE/ORADATA/TEST/REDO03.LOG
2.2 數據庫備份腳本
冷備份腳本
rem     script:coldbak.sql
rem     creater:chenjiping
rem     date:5.8.2003
rem     descffline full backup database  
  
--connect database
connect internal/passWord;
--shutdown database
shutdown immediate;
--Copy Data file
!xcopy d:/oracle/oradata/test/*.dbf d:/database/H/R;  
--Copy Control file
!xcopy d:/oracle/oradata/test/*.ctl d:/database/H/R;
--Copy Log file
!xcopy d:/oracle/oradata/test/*.log d:/database/H/R;
--startup database
startup;
  
說明:
1、以上腳本在數據庫關閉狀態下備份數據庫所有的數據文件,聯機日志,控制文件(在一個目錄下),假如成功備份,所有文件是一致的。
2、沒有備份參數文件,參數文件可以另外備份,沒有必要每次都備份,只需要在改變設置后備份一次。
3、假如以上命令沒有成功依次執行,那么備份將是無效的,如連接數據庫不成功,那么肯定關閉數據庫也不成功,那么備份則無效
4、冷備份建議下人工干預下執行。
  
數據庫OS熱全備份腳本
rem     script:hotbak.sql
rem     creater:chenjiping
rem     date:5.8.2003
rem     desc:backup all database datafile in archive
  
--connect database
connect internal/password;
  
--archive
alter system archive log current;
--start
  
alter tablespace system begin backup;
!xcopy d:/oracle/oradata/test/system01.dbf d:/databak/H/R;
alter tablespace system end backup;
  
alter tablespace rbs begin backup;
!xcopy d:/oracle/oradata/test/rbs01.dbf d:/databak/H/R;
alter tablespace rbs end backup;
  
alter tablespace users begin backup;
!xcopy d:/oracle/oradata/test/users01.dbf d:/databak/H/R;
alter tablespace users end backup;
  
alter tablespace tools begin backup;
!xcopy d:/oracle/oradata/test/tools01.dbf d:/databak/H/R;
alter tablespace tools end backup;
  
alter tablespace indx begin backup;
!xcopy d:/oracle/oradata/test/indx01.dbf d:/databak/H/R;
alter tablespace indx end backup;
--end
  
--bak control file

--binary
alter database backup controlfile to 'd:/databak/controlbinbak.000';
--ascii
alter database backup controlfile to trace;
  
alter system archive log current;
說明:
1、熱備份必須在數據庫歸檔方式下才可以運行
2、以上腳本可以在數據庫運行狀態下備份數據庫所有的數據文件(除了臨時數據文件),沒有必要備份聯機日志。
3、歸檔日志至少需要一次完整備份之后的所有日志。
4、假如以上命令沒有成功依次執行,那么備份也是無效的,如連接數據庫不成功,那么備份則無效
  
RMAN備份只講敘有恢復目錄的情況,假如沒有恢復目錄,情形大致相似。以下是RMAN的熱備份全備份的腳本:
#   script:bakup.rcv
#   creater:chenjiping
#   date:5.8.2003
#   desc:backup all database datafile in archive with rman
  
# connect database
connect rcvcat rman/rman@back;
connect target internal/virpure;
  
# start backup database
run{
allocate channel c1 type disk;
backup full tag 'dbfull' format 'd:/backup/full%u_%s_%p' database
include current controlfile;
sql 'alter system archive log current';
release channel c1;
}
# end
  
說明:
1、  數據庫必須運行在歸檔模式下
2、  RMAN將自動備份數據文件,運行可靠
3、  歸檔日志另外備份處理,但至少需要保存一次備份來的日志
4、  沒有必要用RMAN做冷備份,效果不好
  
以上舉例說明了數據庫的恢復案例的測試環境與部分備份測試腳本,其它的備份腳本可以根據以上腳本演變而來或在案例中加以說明。
數據庫的自動實例將不加以說明,這里只舉例說明媒體錯誤或人為錯誤造成的恢復可能。
以上包括以下案例都是在WINDOWS+ORACLE816上測試驗證的,在不同的操作系統與不同的數據庫版本中略有差別。
  
第三章. 了解與恢復相關的信息
1、理解報警日志文件
報警日志文件一般記載了數據庫的啟動/關閉信息,歸檔信息,備份信息,恢復信息,常見錯誤信息,部分數據庫修改記錄等。一般令名規則為<SID>Alrt.log或Alrt<SID>.log,如我的測試數據庫的報警日志文件的名稱為testalrt.log。
報警日志文件的路徑是根據初始化參數background_dump_dest來決定的,如在我的機器上,該參數值為 D:/Oracle/admin/test/bdump,那么,你就可以在該路徑下找到該文件
2、后臺進程跟蹤文件
后臺進程跟蹤文件的路徑與報警日志文件的路徑一致,在某些情況下,你可以通過后臺跟蹤文件的信息了解更多的需要恢復的信息。如在數據庫需要恢復的時候,報警日志文件中常有這樣的語句:
Errors in file D:/Oracle/admin/test/bdump/testDBW0.TRC:
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
通過提示的DBWR跟蹤文件,可以查詢到更具體的信息。
3、v$recover_file與v$recovery_log
這是兩個動態性能視圖,可以在mount下查看,通過這兩個視圖,你可以了解具體的需要恢復的數據文件與需要使用第四章. 數據庫恢復案例
4.1非歸檔模式下的備份與恢復
備份方案:采用OS冷備份
1.連接數據庫并創建測試表
SQL*Plus: Release 8.1.6.0.0 - PRodUCtion on Tue May 6 13:46:32 2003
(c) Copyright 1999 Oracle Corporation.  All rights reserved.
SQL> connect internal/password as sysdba;
Connected.
SQL> create table test(a int);
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
  
2.備份數據庫
SQL> @coldbak.sql 或在DOS下 svrmgrl @coldbak.sql
  
3.再插入記錄
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
                          A

---------------------------------------
                          1
                          2
4.關閉數據庫
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
  
5.毀壞一個或多個數據文件,如刪除user01.dbf
C:/>del D:/ORACLE/ORADATA/TEST/USERS01.DBF
模擬媒體毀壞
  
6.重新啟動數據庫,會發現如下錯誤
SQL> startup
ORACLE instance started.
  
Total System Global Area  102020364 bytes
Fixed Size                    70924 bytes
Variable Size              85487616 bytes
Database Buffers           16384000 bytes
Redo Buffers                  77824 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 3 - see DBWR trace file
ORA-01110: data file 3: 'D:/ORACLE/ORADATA/TEST/USERS01.DBF'
  
在報警文件中,會有更具體的信息
Errors in file D:/Oracle/admin/test/bdump/testDBW0.TRC:
ORA-01157: cannot identify/lock data file 3 - see DBWR trace file
ORA-01110: data file 3: 'D:/ORACLE/ORADATA/TEST/USERS01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統找不到指定的文件。
  
7.拷貝備份復原到原來位置(restore過程)
C:/>xcopy d:/database/*.* d:/oracle/oradata/test/H/R/S
  
8.打開數據庫,檢查數據
SQL> alter database open;
Database altered.
SQL> select * from test;
                          A
---------------------------------------
                          1
  
這里可以發現,數據庫恢復成功,但在備份之后與崩潰之前的數據丟失了。
說明:
1、非歸檔模式下的恢復方案可選性很小,一般情況下只能有一種恢復方式,就是數據庫的冷備份的完全恢復,僅僅需要拷貝原來的備份就可以(restore),不需要recover。
2、這種情況下的恢復,可以完全恢復到備份的點上,但是可能是丟失數據的,在備份之后與崩潰之前的數據將全部丟失。
3、不管毀壞了多少數據文件或是聯機日志或是控制文件,都可以通過這個辦法恢復,因為這個恢復過程是Restore所有的冷備份文件,而這個備份點上的所有文件是一致的,與最新的數據庫沒有關系,就好比把數據庫又放到了一個以前的“點”上。
4、對于非歸檔模式下,最好的辦法就是采用OS的冷備份,建議不要用RMAN來作冷備份,效果不好,因為RMAN不備份聯機日志,restore不能根本解決問題。
5、假如沒有備份聯機日志,如RMAN的備份,就需要利用不完全恢復(until cancel)的方法來重新創建聯機日志文件
4.2歸檔模式下丟失或損壞一個數據文件
4.2.1 OS備份方案
在歸檔方式下損壞或丟失一個數據文件,假如存在相應的備份與該備份以來的歸檔日志,恢復還是比較簡單的,可以作到盡量少的Down機時間,并能作到數據庫的完全恢復。
1、連接數據庫,創建測試表并插入記錄
SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003
(c) Copyright 1999 Oracle Corporation.  All rights reserved.
SQL> connect internal/password as sysdba;
Connected.
SQL> create table test(a int) tablespace users;
Table created
SQL> insert into test values(1);

1 row inserted
SQL> commit;
Commit complete
  
2、備份數據庫
SQL> @hotbak.sql 或在DOS下 svrmgrl @hotbak.sql
  
3、繼續在測試表中插入記錄
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
SQL> alter system switch logfile;
System altered.
SQL> alter system switch logfile;
System altered.
  
4、關閉數據庫,模擬丟失數據文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down
C:/>del D:/ORACLE/ORADATA/TEST/USERS01.DBF
模擬媒體毀壞
  
5、啟動數據庫錯誤,脫機該數據文件
SQL> startup
ORACLE instance started.
  
Total System Global Area  102020364 bytes
Fixed Size                    70924 bytes
Variable Size              85487616 bytes
Database Buffers           16384000 bytes
Redo Buffers                  77824 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 3 - see DBWR trace file
ORA-01110: data file 3: 'D:/ORACLE/ORADATA/TEST/USERS01.DBF'
還可以查看報警文件(見上一個恢復案例)或動態視圖v$recover_file
如SQL> select * from v$recover_file;
  
      FILE# ONLINE  ERROR                 CHANGE# TIME
---------- ------- ------------------ ---------- -----------
          3 ONLINE                        1013500 2003-05-07
  
脫機數據文件
SQL> alter database datafile 3 offline drop;
Database altered.
  
6、打開數據庫,拷貝備份回來(restore),恢復(recover)該數據文件,并聯機
SQL> alter database open;
Database altered.
拷貝備份從備份處
copy d:/databak/ users01.dbf d:/oracle/oradata/test;
恢復該數據文件
SQL> recover datafile 3;
ORA-00279: change 1053698 generated at 05/07/2003 17:51:26 needed for thread 1
ORA-00289: suggestion : D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00304.ARC
ORA-00280: change 1053698 for thread 1 is in sequence #304
  
Specify log: {<RET>=suggested filename AUTO CANCEL}
AUTO
ORA-00279: change 1053701 generated at 05/07/2003 17:51:39 needed for thread 1
ORA-00289: suggestion : D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00305.ARC

ORA-00280: change 1053701 for thread 1 is in sequence #305
ORA-00278: log file 'D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00304.ARC' no
longer needed for this recovery
  
Log applied.
Media recovery complete.
恢復成功,聯機該數據文件
SQL> alter database datafile 3 online;
Database altered.
  
7、檢查數據庫的數據(完全恢復)
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
說明:
1、采用熱備份,需要運行在歸檔模式下,可以實現數據庫的完全恢復,也就是說,從備份后到數據庫崩潰時的數據都不會丟失。
2、可以采用全備份數據庫的方式備份,對于非凡情況,也可以只備份特定的數據文件,如只備份用戶表空間(一般情況下對于某些寫非凡頻繁的數據文件,可以單獨加大備份頻率)
3、假如在恢復過程中,發現損壞的是多個數據文件,即可以采用一個一個數據文件的恢復方法(第5步中需要對數據文件一一脫機,第6步中需要對數據文件分別恢復),也可以采用整個數據庫的恢復方法。
4、假如是系統表空間的損壞,不能采用此方法
4.2.2 RMAN備份方案
RMAN也可以進行聯機備份,而且備份與恢復方法將比OS備份更簡單可靠。
1、連接數據庫,創建測試表并插入記錄
SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003
(c) Copyright 1999 Oracle Corporation.  All rights reserved.
SQL> connect internal/password as sysdba;
Connected.
  
SQL> create table test(a int) tablespace users;
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
  
2、備份數據庫表空間users
C:/>rman
Recovery Manager: Release 8.1.6.0.0 - Production
RMAN> connect rcvcat rman/rman@back
RMAN-06008: connected to recovery catalog database
RMAN> connect target internal/virpure
RMAN-06005: connected to target database: TEST (DBID=1788174720)
  
RMAN> run{
2> allocate channel c1 type disk;
3> backup tag 'tsuser' format 'd:/backup/tsuser_%u_%s_%p'
4> tablespace users;
5> release channel c1;
6> }
  
RMAN-03022: compiling command: allocate
RMAN-03023: executing command: allocate
RMAN-08030: allocated channel: c1
RMAN-08500: channel c1: sid=16 devtype=DISK
  
RMAN-03022: compiling command: backup
RMAN-03025: performing implicit partial resync of recovery catalog
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03023: executing command: backup
RMAN-08008: channel c1: starting full datafile backupset
RMAN-08502: set_count=5 set_stamp=494177612 creation_time=16-MAY-03
RMAN-08010: channel c1: specifying datafile(s) in backupset
RMAN-08522: input datafile fno=00003 name=D:/ORACLE/ORADATA/TEST/USER01.DBF
RMAN-08013: channel c1: piece 1 created
RMAN-08503: piece handle=D:/BACKUP/TSUSER_05EN93AC_5_1 comment=NONE
RMAN-08525: backup set complete, elapsed time: 00:00:01

RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
RMAN>
  
3、繼續在測試表中插入記錄
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
SQL> alter system switch logfile;
System altered.
SQL>r
1* alter system switch logfile;
System altered.
  
4、關閉數據庫,模擬丟失數據文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down
  
C:/>del D:/ORACLE/ORADATA/TEST/USER01.DBF
  
5、啟動數據庫,檢查錯誤
SQL> startup
ORACLE instance started.
Total System Global Area  102020364 bytes
Fixed Size                    70924 bytes
Variable Size              85487616 bytes
Database Buffers           16384000 bytes
Redo Buffers                  77824 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 3 - see DBWR trace file
ORA-01110: data file 3: 'D:/ORACLE/ORADATA/TEST/USER01.DBF'
  
6、先打開數據庫
SQL> alter database datafile 3 offline drop;
Database altered.
SQL> alter database open;
Database altered.
  
7、恢復該表空間
恢復腳本可以是恢復單個數據文件
run{
allocate channel c1 type disk;
restore datafile 3;
recover datafile 3;
sql 'alter database datafile 3 online';
release channel c1;
}
也可以是,恢復表空間
run{
allocate channel c1 type disk;
restore tablespace users;
recover tablespace users;
sql 'alter database datafile 3 online';
release channel c1;
}
過程如下:
C:/>rman
Recovery Manager: Release 8.1.6.0.0 - Production
RMAN> connect rcvcat rman/rman@back
RMAN-06008: connected to recovery catalog database
RMAN> connect target internal/virpure
RMAN-06005: connected to target database: TEST (DBID=1788174720)
  
RMAN> run{
2> allocate channel c1 type disk;
3> restore datafile 3;
4> recover datafile 3;
5> sql 'alter database datafile 3 online';
6> release channel c1;
7> }
  
RMAN-03022: compiling command: allocate

RMAN-03023: executing command: allocate
RMAN-08030: allocated channel: c1
RMAN-08500: channel c1: sid=13 devtype=DISK
RMAN-03022: compiling command: restore
RMAN-03022: compiling command: IRESTORE
RMAN-03023: executing command: IRESTORE
RMAN-08016: channel c1: starting datafile backupset restore
RMAN-08502: set_count=5 set_stamp=494177612 creation_time=16-MAY-03
RMAN-08089: channel c1: specifying datafile(s) to restore from backup set
RMAN-08523: restoring datafile 00003 to D:/ORACLE/ORADATA/TEST/USER01.DBF
RMAN-08023: channel c1: restored backup piece 1
RMAN-08511: piece handle=D:/BACKUP/TSUSER_05EN93AC_5_1 tag=TSUSER params=NULL
RMAN-08024: channel c1: restore complete
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03022: compiling command: recover
RMAN-03022: compiling command: recover(1)
RMAN-03022: compiling command: recover(2)
RMAN-03022: compiling command: recover(3)
RMAN-03023: executing command: recover(3)
RMAN-08054: starting media recovery
RMAN-03022: compiling command: recover(4)
RMAN-06050: archivelog thread 1 sequence 332 is already on disk as file D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00332.ARC
RMAN-06050: archivelog thread 1 sequence 333 is already on disk as file D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00333.ARC
RMAN-06050: archivelog thread 1 sequence 334 is already on disk as file D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00334.ARC
RMAN-03023: executing command: recover(4)
RMAN-08515: archivelog filename=D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00332.ARC thread=1 sequence=332
RMAN-08055: media recovery complete
RMAN-03022: compiling command: sql
RMAN-06162: sql statement: alter database datafile 3 online
RMAN-03023: executing command: sql
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
RMAN>
  
8、檢查數據是否完整
SQL> alter database open;
Database altered.
  
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
說明:
1、RMAN也可以實現單個表空間或數據文件的恢復,恢復過程可以在mount下或open方式下,假如在open方式下恢復,可以減少down機時間
2、假如損壞的是一個數據文件,建議offline并在open方式下恢復
3、這里可以看到,RMAN進行數據文件與表空間恢復的時候,代碼都比較簡單,而且能保證備份與恢復的可靠性,所以建議采用RMAN的備份與恢復
4.3丟失多個數據文件,實現整個數據庫的恢復
4.3.1 OS備份方案
OS備份歸檔模式下損壞(丟失)多個數據文件,進行整個數據庫的恢復
1、連接數據庫,創建測試表并插入記錄
SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003
(c) Copyright 1999 Oracle Corporation.  All rights reserved.

SQL> connect internal/password as sysdba;
Connected.
  
SQL> create table test(a int);
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
  
2、備份數據庫,備份除臨時數據文件后的所數據文件
SQL> @hotbak.sql 或在DOS下 svrmgrl @hotbak.sql
  
3、繼續在測試表中插入記錄
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
SQL> alter system switch logfile;
System altered.
SQL> alter system switch logfile;
System altered.
  
4、關閉數據庫,模擬丟失數據文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down
  
C:/>del D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF
C:/>del D:/ORACLE/ORADATA/TEST/INDX01.DBF
C:/>del D:/ORACLE/ORADATA/TEST/TOOLS01.DBF
C:/>del D:/ORACLE/ORADATA/TEST/RBS01.DBF
模擬媒體毀壞(這里刪除多個數據文件)
  
5、啟動數據庫,檢查錯誤
SQL> STARTUP
ORACLE instance started.
Total System Global Area  102020364 bytes
Fixed Size                    70924 bytes
Variable Size              85487616 bytes
Database Buffers           16384000 bytes
Redo Buffers                  77824 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: 'D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF'
  
具體信息可以查看報警文件
ORA-1157 signalled during: ALTER DATABASE OPEN...
Thu May 08 09:39:36 2003
Errors in file D:/Oracle/admin/test/bdump/testDBW0.TRC:
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: 'D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統找不到指定的文件。
  
Thu May 08 09:39:36 2003
Errors in file D:/Oracle/admin/test/bdump/testDBW0.TRC:
ORA-01157: cannot identify/lock data file 2 - see DBWR trace file
ORA-01110: data file 2: 'D:/ORACLE/ORADATA/TEST/RBS01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統找不到指定的文件。
  
Thu May 08 09:39:36 2003
Errors in file D:/Oracle/admin/test/bdump/testDBW0.TRC:
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file

ORA-01110: data file 5: 'D:/ORACLE/ORADATA/TEST/TOOLS01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統找不到指定的文件。
  
Thu May 08 09:39:36 2003
Errors in file D:/Oracle/admin/test/bdump/testDBW0.TRC:
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:/ORACLE/ORADATA/TEST/INDX01.DBF'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統找不到指定的文件。
  
通過查詢v$recover_file可以看到
SQL> select * from v$recover_file;
  
      FILE# ONLINE  ERROR                 CHANGE# TIME
---------- ------- ------------------ ---------- -----------
          1 ONLINE  FILE NOT FOUND              0  
          2 ONLINE  FILE NOT FOUND              0  
          5 ONLINE  FILE NOT FOUND              0  
          6 ONLINE  FILE NOT FOUND              0
有四個數據文件需要恢復
  
6、拷貝備份回到原地點(restore),開始恢復數據庫(recover)
restore過程:
C:/>copy D:/DATABAK/SYSTEM01.DBF D:/ORACLE/ORADATA/TEST/
C:/>copy D:/DATABAK/TEST/INDX01.DBF D:/ORACLE/ORADATA/TEST/
C:/>copy D:/DATABAK/TEST/TOOLS01.DBF D:/ORACLE/ORADATA/TEST/
C:/>copy D:/DATABAK/TEST/RBS01.DBF.DBF D:/ORACLE/ORADATA/TEST/
  
Recover過程:
SQL> recover database;
ORA-00279: change 1073849 generated at 05/08/2003 08:58:35 needed for thread 1
ORA-00289: suggestion : D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00311.ARC
ORA-00280: change 1073849 for thread 1 is in sequence #311
  
Specify log: {<RET>=suggested filename AUTO CANCEL}
auto
ORA-00279: change 1073856 generated at 05/08/2003 09:03:27 needed for thread 1
ORA-00289: suggestion : D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00312.ARC
ORA-00280: change 1073856 for thread 1 is in sequence #312
ORA-00278: log file 'D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00311.ARC' no
longer needed for this recovery
  
ORA-00279: change 1073858 generated at 05/08/2003 09:11:43 needed for thread 1
ORA-00289: suggestion : D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00313.ARC
ORA-00280: change 1073858 for thread 1 is in sequence #313
ORA-00278: log file 'D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00312.ARC' no
longer needed for this recovery
  
ORA-00279: change 1073870 generated at 05/08/2003 09:11:46 needed for thread 1
ORA-00289: suggestion : D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00314.ARC

ORA-00280: change 1073870 for thread 1 is in sequence #314
ORA-00278: log file 'D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00313.ARC' no
longer needed for this recovery
  
Log applied.
Media recovery complete.
  
7、打開數據庫,檢查數據庫的數據(完全恢復)
SQL> alter database open;
Database altered.
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
  
說明:
1、只要有備份與歸檔存在,就可以實現數據庫的完全恢復(不丟失數據)
2、適合于丟失大量數據文件,或包含系統數據文件在內的數據庫的恢復
3、恢復過程在mount下進行,假如恢復成功,再打開數據庫,down機時間可能比較長一些。
4.3.2 RMAN備份方案
RMAN備份歸檔模式下損壞(丟失)多個數據文件,進行整個數據庫的恢復
1、連接數據庫,創建測試表并插入記錄
SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003
(c) Copyright 1999 Oracle Corporation.  All rights reserved.
SQL> connect internal/password as sysdba;
Connected.
SQL> create table test(a int);
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
  
2、備份數據庫
DOS下 C:>/ rman cmdfile=bakup.rcv msglog=backup.log;
  
以下是backup.log內容。
Recovery Manager: Release 8.1.6.0.0 - Production
RMAN> #     script:bakup.rcv
2> #     creater:chenjiping
3> #     date:5.8.2003
4> #     desc:backup all database datafile in archive with rman
5>  
6> #connect database
7> connect rcvcat rman/rman@back;
8> connect target internal/virpure;
9>  
10> #start backup database
11> run{
12> allocate channel c1 type disk;
13> backup full tag 'dbfull' format 'd:/backup/full%u_%s_%p' database
14> include current controlfile;
15> sql 'alter system archive log current';
16> release channel c1;
17> }
18> #end
19>  
  
RMAN-06008: connected to recovery catalog database
RMAN-06005: connected to target database: TEST (DBID=1788174720)
RMAN-03022: compiling command: allocate
RMAN-03023: executing command: allocate
RMAN-08030: allocated channel: c1
RMAN-08500: channel c1: sid=15 devtype=DISK
RMAN-03022: compiling command: backup
RMAN-03023: executing command: backup
RMAN-08008: channel c1: starting full datafile backupset
RMAN-08502: set_count=4 set_stamp=494074368 creation_time=15-MAY-03
RMAN-08010: channel c1: specifying datafile(s) in backupset
RMAN-08522: input datafile fno=00002 name=D:/ORACLE/ORADATA/TEST/RBS01.DBF
RMAN-08522: input datafile fno=00001 name=D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF

RMAN-08011: including current controlfile in backupset
RMAN-08522: input datafile fno=00005 name=D:/ORACLE/ORADATA/TEST/TOOLS01.DBF
RMAN-08522: input datafile fno=00004 name=D:/ORACLE/ORADATA/TEST/TEMP01.DBF
RMAN-08522: input datafile fno=00006 name=D:/ORACLE/ORADATA/TEST/INDX01.DBF
RMAN-08522: input datafile fno=00003 name=D:/ORACLE/ORADATA/TEST/USER01.DBF
RMAN-08013: channel c1: piece 1 created
RMAN-08503: piece handle=D:/BACKUP/FULL04EN5UG0_4_1 comment=NONE
RMAN-08525: backup set complete, elapsed time: 00:01:16
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03022: compiling command: sql
RMAN-06162: sql statement: alter system archive log current
RMAN-03023: executing command: sql
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
Recovery Manager complete.
到這里表示備份成功。
  
3、繼續在測試表中插入記錄
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
SQL> alter system switch logfile;
System altered.
SQL> alter system switch logfile;
System altered.
  
4、關閉數據庫,模擬丟失數據文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down
C:/>del D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF
C:/>del D:/ORACLE/ORADATA/TEST/INDX01.DBF
C:/>del D:/ORACLE/ORADATA/TEST/TOOLS01.DBF
C:/>del D:/ORACLE/ORADATA/TEST/RBS01.DBF
  
5、啟動數據庫,檢查錯誤
SQL> STARTUP
ORACLE instance started.
Total System Global Area  102020364 bytes
Fixed Size                    70924 bytes
Variable Size              85487616 bytes
Database Buffers           16384000 bytes
Redo Buffers                  77824 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 1 - see DBWR trace file
ORA-01110: data file 1: 'D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF'
  
查詢v$recover_file
SQL> select * from v$recover_file;
  
      FILE# ONLINE  ERROR                 CHANGE# TIME
---------- ------- ------------------ ---------- -----------

          1 ONLINE  FILE NOT FOUND              0  
          2 ONLINE  FILE NOT FOUND              0  
          5 ONLINE  FILE NOT FOUND              0  
          6 ONLINE  FILE NOT FOUND              0
可以知道有四個數據文件需要恢復
  
6、利用RMAN進行恢復
C:/>rman
Recovery Manager: Release 8.1.6.0.0 - Production
RMAN> connect rcvcat rman/rman@back
RMAN-06008: connected to recovery catalog database
RMAN> connect target internal/virpure
RMAN-06005: connected to target database: TEST (DBID=1788174720)
RMAN> run{
2> allocate channel c1 type disk;
3> restore database;
4> recover database;
5> sql 'alter database open';
6> release channel c1;
7> }
  
RMAN-03022: compiling command: allocate
RMAN-03023: executing command: allocate
RMAN-08030: allocated channel: c1
RMAN-08500: channel c1: sid=17 devtype=DISK
RMAN-03022: compiling command: restore
RMAN-03025: performing implicit partial resync of recovery catalog
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03022: compiling command: IRESTORE
RMAN-03023: executing command: IRESTORE
RMAN-08016: channel c1: starting datafile backupset restore
RMAN-08502: set_count=4 set_stamp=494074368 creation_time=15-MAY-03
RMAN-08089: channel c1: specifying datafile(s) to restore from backup set
RMAN-08523: restoring datafile 00001 to D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF
RMAN-08523: restoring datafile 00002 to D:/ORACLE/ORADATA/TEST/RBS01.DBF
RMAN-08523: restoring datafile 00003 to D:/ORACLE/ORADATA/TEST/USER01.DBF
RMAN-08523: restoring datafile 00004 to D:/ORACLE/ORADATA/TEST/TEMP01.DBF
RMAN-08523: restoring datafile 00005 to D:/ORACLE/ORADATA/TEST/TOOLS01.DBF
RMAN-08523: restoring datafile 00006 to D:/ORACLE/ORADATA/TEST/INDX01.DBF
RMAN-08023: channel c1: restored backup piece 1
RMAN-08511: piece handle=D:/BACKUP/FULL04EN5UG0_4_1 tag=DBFULL params=NULL
RMAN-08024: channel c1: restore complete
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03022: compiling command: recover
RMAN-03022: compiling command: recover(1)
RMAN-03022: compiling command: recover(2)
RMAN-03022: compiling command: recover(3)
RMAN-03023: executing command: recover(3)
RMAN-08054: starting media recovery
RMAN-03022: compiling command: recover(4)
RMAN-06050: archivelog thread 1 sequence 327 is already on disk as file D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00327.ARC

RMAN-06050: archivelog thread 1 sequence 328 is already on disk as file D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00328.ARC
RMAN-06050: archivelog thread 1 sequence 329 is already on disk as file D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00329.ARC
RMAN-06050: archivelog thread 1 sequence 330 is already on disk as file D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00330.ARC
RMAN-03023: executing command: recover(4)
RMAN-08515: archivelog filename=D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00327.ARC thread=1 sequence=327
RMAN-08515: archivelog filename=D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00328.ARC thread=1 sequence=328
RMAN-08055: media recovery complete
RMAN-03022: compiling command: sql
RMAN-06162: sql statement: alter database open
RMAN-03023: executing command: sql
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
RMAN>
  
7、檢查數據庫的數據(完全恢復)
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
  
說明:
1、只要有備份與歸檔存在,RMAN也可以實現數據庫的完全恢復(不丟失數據)
2、同OS備份數據庫恢復,適合于丟失大量數據文件,或包含系統數據文件在內的數據庫的恢復
3、目標數據庫在mount下進行,假如恢復成功,再打開數據庫。
4、RMAN的備份與恢復命令相對比較簡單并可靠,建議有條件的話,都采用RMAN進行數據庫的備份。
4.4 不完全恢復案例
4.4.1 OS備份下的基于時間的恢復
不完全恢復可以分為基于時間的恢復,基于改變的恢復與基于撤消的恢復,這里已基于時間的恢復為例子來說明不完全恢復過程。
基于時間的恢復可以不完全恢復到現在時間之前的某一個時間,對于某些誤操作,如刪除了一個數據表,可以在備用恢復環境上恢復到表的刪除時間之前,然后把該表導出到正式環境,避免一個人為的錯誤。
1、連接數據庫,創建測試表并插入記錄
SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003
(c) Copyright 1999 Oracle Corporation.  All rights reserved.
SQL> connect internal/password as sysdba;
Connected.
SQL> create table test(a int);
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
  
2、備份數據庫,這里最好備份所有的數據文件,包括臨時數據文件
SQL> @hotbak.sql 或在DOS下 svrmgrl @hotbak.sql
或冷備份也可以
  
3、刪除測試表,假定刪除前的時間為T1,在刪除之前,便于測試,繼續插入數據并應用到歸檔。
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
SQL> alter system switch logfile;
Statement processed.
SQL> alter system switch logfile;

Statement processed.
  
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'YY
-------------------
2003-05-21 14:43:01
SQL> drop table test;
Table dropped.
  
4、預備恢復到時間點T1,找回刪除的表,先關閉數據庫
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
  
5、拷貝剛才備份的所有數據文件回來
C:/>copy D:/DATABAK/*.DBF D:/ORACLE/ORADATA/TEST/
  
6、啟動到mount下
SQL> startup mount;
ORACLE instance started.
Total System Global Area  102020364 bytes
Fixed Size                    70924 bytes
Variable Size              85487616 bytes
Database Buffers           16384000 bytes
Redo Buffers                  77824 bytes
Database mounted.
  
7、開始不完全恢復數據庫到T1時間
SQL> recover database until time '2003-05-21:14:43:01';
ORA-00279: change 30944 generated at 05/21/2003 14:40:06 needed for thread 1
ORA-00289: suggestion : D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00191.ARC
ORA-00280: change 30944 for thread 1 is in sequence #191
  
Specify log: {<RET>=suggested filename AUTO CANCEL}
auto
Log applied.
Media recovery complete.
8、打開數據庫,檢查數據
SQL> alter database open resetlogs;
  
Database altered.
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
  
說明:
1、不完全恢復最好備份所有的數據,冷備份亦可,因為恢復過程是從備份點往后恢復的,假如因為其中一個數據文件的時間戳(SCN)大于要恢復的時間點,那么恢復都是不可能成功的。
2、不完全恢復有三種方式,過程都一樣,僅僅是recover命令有所不一樣,這里用基于時間的恢復作為示例。
3、不完全恢復之后,都必須用resetlogs的方式打開數據庫,建議馬上再做一次全備份,因為resetlogs之后再用以前的備份恢復是很難了。
4、以上是在刪除之前獲得時間,但是實際應用中,很難知道刪除之前的實際時間,但可以采用大致時間即可,或可以采用分析日志文件(logmnr),取得精確的需要恢復的時間。
5、一般都是在測試機后備用機器上采用這種不完全恢復,恢復之后導出/導入被誤刪的表回生產系統
4.4.2 RMAN備份下的基于改變的恢復
以上用OS備份說明了一個基于時間的恢復,現在用RMAN說明一個基于改變的恢復
1、連接數據庫,創建測試表并插入記錄
SQL*Plus: Release 8.1.6.0.0 - Production on Tue May 6 13:46:32 2003
(c) Copyright 1999 Oracle Corporation.  All rights reserved.
SQL> connect internal/password as sysdba;
Connected.
SQL> create table test(a int);
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
  
2、備份數據庫
C:/>rman
Recovery Manager: Release 8.1.6.0.0 - Production
RMAN> connect rcvcat rman/rman@back

RMAN-06008: connected to recovery catalog database
RMAN> connect target internal/virpure
RMAN-06005: connected to target database: TEST (DBID=874705288)
  
RMAN> run{
2> allocate channel c1 type disk;
3> backup full tag 'dbfull' format 'd:/backup/full%u_%s_%p' database
4> include current controlfile;
5> sql 'alter system archive log current';
6> release channel c1;
7> }
  
RMAN-03022: compiling command: allocate
RMAN-03023: executing command: allocate
RMAN-08030: allocated channel: c1
RMAN-08500: channel c1: sid=12 devtype=DISK
RMAN-03022: compiling command: backup
RMAN-03023: executing command: backup
RMAN-08008: channel c1: starting full datafile backupset
RMAN-08502: set_count=1 set_stamp=494607834 creation_time=21-MAY-03
RMAN-08010: channel c1: specifying datafile(s) in backupset
RMAN-08522: input datafile fno=00001 name=D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF
RMAN-08011: including current controlfile in backupset
RMAN-08522: input datafile fno=00002 name=D:/ORACLE/ORADATA/TEST/RBS01.DBF
RMAN-08522: input datafile fno=00003 name=D:/ORACLE/ORADATA/TEST/USERS01.DBF
RMAN-08522: input datafile fno=00004 name=D:/ORACLE/ORADATA/TEST/TEMP01.DBF
RMAN-08522: input datafile fno=00005 name=D:/ORACLE/ORADATA/TEST/TOOLS01.DBF
RMAN-08522: input datafile fno=00006 name=D:/ORACLE/ORADATA/TEST/INDX01.DBF
RMAN-08013: channel c1: piece 1 created
RMAN-08503: piece handle=D:/BACKUP/FULL01ENM7EQ_1_1 comment=NONE
RMAN-08525: backup set complete, elapsed time: 00:00:16
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
  
RMAN-03022: compiling command: sql
RMAN-06162: sql statement: alter system archive log current
RMAN-03023: executing command: sql
  
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
  
RMAN>
  
3、刪除測試表,在刪除之前,便于測試,繼續插入數據并應用到歸檔,并獲取刪除前的scn號。
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
SQL> alter system switch logfile;
Statement processed.
SQL> alter system switch logfile;
Statement processed.
  
SQL> select max(ktuxescnw * power(2, 32) + ktuxescnb) scn from x$ktuxe;
    SCN
----------
   31014

SQL> drop table test;
Table dropped.
  
4、預備恢復到SCN 31014,先關閉數據庫,然后啟動到mount下
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.
Total System Global Area                         53126412 bytes
Fixed Size                                          70924 bytes
Variable Size                                    26763264 bytes
Database Buffers                                 26214400 bytes
Redo Buffers                                        77824 bytes
Database mounted.
  
5、開始恢復到改變點SCN 31014
RMAN> run{
2>      allocate channel c1 type disk;
3>      restore database;
4>      recover database until scn 31014;
5>      sql 'ALTER DATABASE OPEN RESETLOGS';
6>      release channel c1;
7> }
  
RMAN-03022: compiling command: allocate
RMAN-03023: executing command: allocate
RMAN-08030: allocated channel: c1
RMAN-08500: channel c1: sid=10 devtype=DISK
RMAN-03022: compiling command: restore
RMAN-03022: compiling command: IRESTORE
RMAN-03023: executing command: IRESTORE
RMAN-08016: channel c1: starting datafile backupset restore
RMAN-08502: set_count=1 set_stamp=494613682 creation_time=21-MAY-03
RMAN-08089: channel c1: specifying datafile(s) to restore from backup set
RMAN-08523: restoring datafile 00001 to D:/ORACLE/ORADATA/TEST/SYSTEM01.DBF
RMAN-08523: restoring datafile 00002 to D:/ORACLE/ORADATA/TEST/RBS01.DBF
RMAN-08523: restoring datafile 00003 to D:/ORACLE/ORADATA/TEST/USERS01.DBF
RMAN-08523: restoring datafile 00004 to D:/ORACLE/ORADATA/TEST/TEMP01.DBF
RMAN-08523: restoring datafile 00005 to D:/ORACLE/ORADATA/TEST/TOOLS01.DBF
RMAN-08523: restoring datafile 00006 to D:/ORACLE/ORADATA/TEST/INDX01.DBF
RMAN-08023: channel c1: restored backup piece 1
RMAN-08511: piece handle=D:/BACKUP/FULL01ENmd5I_1_1 tag=DBFULL params=NULL
RMAN-08024: channel c1: restore complete
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03022: compiling command: recover
RMAN-03022: compiling command: recover(1)

RMAN-03022: compiling command: recover(2)
RMAN-03022: compiling command: recover(3)
RMAN-03023: executing command: recover(3)
RMAN-08054: starting media recovery
RMAN-03022: compiling command: recover(4)
RMAN-06050: archivelog thread 1 sequence 191 is already on disk as file D:/ORACL
E/ORADATA/TEST/ARCHIVE/TESTT001S00191.ARC
RMAN-06050: archivelog thread 1 sequence 192 is already on disk as file D:/ORACL
E/ORADATA/TEST/ARCHIVE/TESTT001S00192.ARC
RMAN-03023: executing command: recover(4)
RMAN-08515: archivelog filename=D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00191.AR
C thread=1 sequence=191
RMAN-08515: archivelog filename=D:/ORACLE/ORADATA/TEST/ARCHIVE/TESTT001S00192.AR
C thread=1 sequence=192
RMAN-08055: media recovery complete
RMAN-03022: compiling command: sql
RMAN-06162: sql statement: ALTER DATABASE OPEN RESETLOGS
RMAN-03023: executing command: sql
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
  
6、檢查數據
Database altered.
SQL> select * from test;
                          A
---------------------------------------
                          1
                          2
可以看到,表依然存在
  
說明:
1、RMAN也可以實現不完全恢復,方法比OS備份恢復的方法更簡單可靠
2、RMAN可以基于時間,基于改變與基于日志序列的不完全恢復,基于日志序列的恢復可以指定恢復到哪個日志序列,如
run {   
      allocate channel ch1 type disk;   
      allocate channel ch2 type 'sBT_tape';  
      set until logseq 1234 thread 1;  
      restore controlfile to '$ORACLE_HOME/dbs/cf1.f' ;   
      replicate controlfile from '$ORACLE_HOME/dbs/cf1.f';  
      alter database mount;   
      restore database;   
      recover database;   
      sql "ALTER DATABASE OPEN RESETLOGS";
}
3、與所有的不完全恢復一樣,必須在mount下,restore所有備份數據文件,需要resetlogs
4、基于改變的恢復比基于時間的恢復更可靠,但是可能也更復雜,需要知道需要恢復到哪一個改變號(SCN),在正常生產中,獲取SCN的辦法其實也有很多,如查詢數據庫字典表(V$archived_log or v$log_history),或分析歸檔與聯機日志(logmnr)等。


第五章 其它恢復案例
5.1 損壞聯機日志的恢復方法
5.1.1 損壞非當前聯機日志
大家都清楚,聯機日志分為當前聯機日志和非當前聯機日志,非當前聯機日志的損壞是比較簡單的,一般通過clear命令就可以解決問題。
1、啟動數據庫,碰到ORA-00312 or ORA-00313錯誤,如
ORA-00313: open failed for members of log group 1 of thread 1
ORA-00312: online log 1 thread 1: 'D:/ORACLE/ORADATA/TEST/REDO01.LOG'
從這里我們知道日志組1的數據文件損壞了

從報警文件可以看到更具體的信息
2、查看V$log視圖
SQL> select group#,sequence#,archived,status from v$log;
  
     GROUP#  SEQUENCE# ARCHIVED STATUS
---------- ---------- -------- ----------------
          1          1 YES      INACTIVE
          2          2 YES      INACTIVE
          3          3 NO       CURRENT
可以知道,該組是非當前狀態,而且已經歸檔。
3、用CLEAR命令重建該日志文件
SQL>alter database clear logfile group 1;
假如是該日志組還沒有歸檔,則需要用
SQL>alter database clear unarchived logfile group 1;
4、打開數據庫,重新備份數據庫
SQL>alter database open;
說明:
1、假如損壞的是非當前的聯機日志文件,一般只需要clear就可以重建該日志文件,但是假如該數據庫處于歸檔狀態但該日志還沒有歸檔,就需要強行clear。
2、建議clear,非凡是強行clear后作一次數據庫的全備份。
3、此方法適用于歸檔與非歸檔數據庫
5.1.2 損壞當前聯機日志
歸檔模式下當前日志的損壞有兩種情況,
一、是數據庫是正常關閉,日志文件中沒有未決的事務需要實例恢復,當前日志組的損壞就可以直接用alter database clear unarchived logfile group n來重建。
二、是日志組中有活動的事務,數據庫需要媒體恢復,日志組需要用來同步,有兩種補救辦法
A.  最好的辦法就是通過不完全恢復,可以保證數據庫的一致性,但是這種辦法要求在歸檔方式下,并且有可用的備份
B.  通過強制性恢復,但是可能導致數據庫不一致。
下面分別用來說明這兩種恢復方法
5.1.2.1 通過備份來恢復
1、打開數據庫,會碰到一個類似的錯誤
ORA-00313: open failed for members of log group 1 of thread 1
ORA-00312: online log 1 thread 1: 'D:/ORACLE/ORADATA/TEST/REDO01.LOG'
ORA-27041: unable to open file
OSD-04002: unable to open file
O/S-Error: (OS 2) 系統找不到指定的文件
  
2、查看V$log,發現是當前日志
SQL> select group#,sequence#,archived,status from v$log;
  
     GROUP#  SEQUENCE# ARCHIVED STATUS
---------- ---------- -------- ----------------
          1          1 NO       CURRENT
          2          2 YES      INACTIVE
          3          3 YES      INACTIVE
  
3、發現clear不成功
SQL> alter database clear unarchived logfile group 1;
alter database clear unarchived logfile group 1
*
ERROR at line 1:
ORA-01624: log 1 needed for crash recovery of thread 1
ORA-00312: online log 1 thread 1: 'D:/ORACLE/ORADATA/TEST/REDO01.LOG'
  
4、拷貝有效的數據庫的全備份,并不完全恢復數據庫
可以采用獲取最近的SCN的辦法用until scn恢復或用until cnacel恢復
recover database until cancel
先選擇auto,盡量恢復可以利用的歸檔日志,然后重新
recover database until cancel
這次輸入cancel,完成不完全恢復,也就是說恢復兩次。
如:
SQL> recover database until cancel;
Auto
……
SQL> recover database until cancel;
Cancel;
5、利用alter database open resetlogs打開數據庫
說明:

   1、這種辦法恢復的數據庫是一致的不完全恢復,會丟失當前聯機日志中的事務數據
   2、這種方法適合于歸檔數據庫并且有可用的數據庫全備份。
   3、恢復成功之后,記得再做一次數據庫的全備份。
   4、建議聯機日志文件一定要實現鏡相在不同的磁盤上,避免這種情況的發生,因為任何數據的丟失對于生產來說都是不容許的。
  
5.1.2.2 假如沒有備份,進行強制性恢復
1、打開數據庫,會碰到一個類似的錯誤
ORA-00313: open failed for members of log group 1 of thread 1
ORA-00312: online log 1 thread 1:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 板桥市| 嘉义市| 江山市| 武平县| 嘉善县| 萨迦县| 勐海县| 泾源县| 锡林浩特市| 丹棱县| 卓尼县| 客服| 西城区| 武夷山市| 上思县| 洛阳市| 永安市| 唐山市| 东兴市| 通州区| 义乌市| 吉安县| 广汉市| 邯郸市| 溧水县| 随州市| 秀山| 叶城县| 山阳县| 香河县| 土默特右旗| 舟曲县| 易门县| 察隅县| 两当县| 木里| 饶河县| 米泉市| 延庆县| 旬邑县| 古浪县|