1. 程式人生 > >Oracle 備份與恢復基礎

Oracle 備份與恢復基礎

 

Oracle 備份與恢復基礎 :三思筆記

 備份與恢復

A whole database backup is either a consistent backup or an inconsistent backup.

Whether a backup is consistent determines whether you need to apply redo logs after

restoring the backup.

Tablespace Backups

A tablespace backup is a backup of the datafiles that constitute the tablespace. For

example, if tablespace users contains datafiles 2, 3, and 4, then a backup of

tablespace users backs up these three datafiles.

Tablespace backups, whether online or offline, are valid only if the database is

operating in ARCHIVELOG mode. The reason is that redo is required to make the

restored tablespace consistent with the other tablespaces in the database.

Datafile Backups

A datafile backup is a backup of a single datafile. Datafile backups, which are not as

common as tablespace backups, are valid in ARCHIVELOG databases. The only time a

datafile backup is valid for a database in NOARCHIVELOG mode is if:

■ Every datafile in a tablespace is backed up. You cannot restore the database unless

all datafiles are backed up.

■ The datafiles are read only or offline-normal.

such as

compression or incremental backup. You must use RMAN to restore a backup set.

RMAN with Online Backups

Because the database continues writing to the file during an online backup, there is the

possibility of backing up inconsistent data within a block. For example, assume that

either RMAN or an operating system utility reads the block while database writer is in

the middle of updating the block. In this case, RMAN or the copy utility could read the

old data in the top half of the block and the new data in the bottom top half of the

block. The block is a fractured block, meaning that the data in this block is not

consistent.

During an RMAN backup, the Oracle database server reads the datafiles, not an

operating system utility. The server reads each block and determines whether the

block is fractured. If the block is fractured, then Oracle re-reads the block until it gets a

consistent picture of the data.

When you back up an online datafile with an operating system utility (rather than

with RMAN), you must use a different method to handle fractured blocks. You must

first place the files in backup mode with the ALTER TABLESPACE BEGIN BACKUP 

statement (to back up an individual tablespace), or the ALTER DATABASE BEGIN

BACKUP statement (to back up the entire database). After an online backup is

completed, you must run the ALTER TABLESPACE ... END BACKUP or ALTER

DATABASE END BACKUP statement to take the tablespace out of backup mode.

 

1備份:

所謂備份就是冗餘,本質是將當前的資料複製一份(或多份)到其他位置

1.1 備份型別

  1 物理備份:核心是複製檔案,資料檔案,日誌檔案,控制檔案,歸檔檔案等oracle資料庫啟動的相關檔案,複製到其他路徑或儲存裝置中

  2 邏輯備份:核心是複製資料,按照oracle提供的命令,通過邏輯的方式直接將資料儲存到其他位置,包括exp,expdp,dblink+ctas等,

1.2 資料庫狀態

     啟動和關閉狀態,

      1 熱備份:db處於啟動狀態建立的備份,聯機備份,這種狀態下建立的備份是不一致備份,利用不一致備份進行恢復時,可能需要利用到歸檔日誌和線上重做日誌,db必須出具歸檔狀態,archived

      2 冷備份:db處於關閉狀態時建立的備份,離線備份,利用冷備份恢復時,如果資料庫正常關閉,則不需要進行恢復,否則也需要歸檔跟redo log才能將db恢復到一個一致性的狀態,

處於open read only狀態,建立的備份也是冷備份

 1.3 備份的狀態

1 一致性備份:備份的data file跟control file有相同的scn,即一致性備份,(必須是冷備份)

2 不一致備份:通常db處於open read write或shutdown abort等時都不會是一致性狀態,因為備份操作不可能同時完成,data file 隨便都是寫,scn隨時都在變,

  熱備份肯定是不一致備份,但不一致備份不一定是熱備份(shutdown abort)

  只有archived模式下的不一致備份才是有效備份

 1.4 備份規模

1 完全備份:完成備份,包含data file,redo log,control log,init ora,archived log等

  歸檔模式下

    資料庫處於正常開啟狀態下的備份是不一致備份(有效)

    Db正常關閉的備份是有效備份

  非歸檔模式

    資料庫處於正常開啟狀態下的備份是不一致備份(無效)

    Db正常關閉的備份是有效備份

 2表空間備份

  備份一個或多個tablespace,實質是備份data file,當滿足一下2個條件之一才是有效備份

   資料庫處於歸檔模式,tablespace處於offline或read only狀態

 3 備份資料檔案

  備份一個或多個data file,大多數情況db處於archived模式,或者db處於 open read only

 4 控制檔案備份

   備份control file,控制檔案是db 到mount狀態的必須條件

 5 歸檔日誌檔案備份

  歸檔日誌儲存了資料庫曾經做過的操作,資料庫處於歸檔模式或者force logging模式

2 恢復

  有有效的備份,才能更好的恢復

2.1 錯誤型別

   1 使用者誤操作導致的錯誤

   由於使用者誤操作的執行某些語句,需要撤銷某些操作

   2 系統程序出現的錯誤

   3由於instance導致錯誤

    例項錯誤可能導致:提交的事務要修改的資料還未寫到資料檔案

                      未提交的事務已經修改了資料檔案

  對於例項錯誤,下次在啟動例項時,自動嘗試進行例項恢復instance recovered

       4 儲存介質導致的錯誤

        由於硬體問題出現,可能導致db崩潰,data file 等檔案破壞,需要進行介質恢復,

2.2恢復型別

     1 例項恢復:由oracle自動恢復,通過讀取當前的資料檔案跟日誌檔案(不是歸檔日誌)來恢復資料,使資料檔案和控制檔案恢復到崩潰前的一致性狀態

         1 快取恢復:恢復過程中已提交的事務但沒寫到資料檔案,前滾

         2 事務恢復:前滾後,把沒有提交的事務但是已經修改了資料檔案的事務回滾,回滾

      2 介質恢復:需要利用到備份來進行恢復,或重新利用日誌檔案

          介質恢復是首先把db恢復到備份的狀態,然後利用redo日誌把db恢復到最近時間點(或某個時間點),通常提到的介質恢復都是修復資料檔案。

2.3 恢復方式

     1 完全恢復

db恢復到最近時間點的恢復方式就是完全恢復,這種方式通常是當磁碟故障導致資料檔案或控制檔案無法訪問時選擇的恢復方式(還可以對錶空間或資料檔案進行完全恢復)

        如果對整個資料庫做恢復,

        Mount資料庫

        確認所有資料檔案均為online狀態,

        執行恢復操作,

        應用所有生成的redologs檔案

       如果對錶空間或資料檔案進行完全恢復

        Open 資料庫

        將要恢復的表空間或資料檔案設定為offline狀態

        執行恢復操作

        應用所有生成的redologs檔案

   2 不完全恢復

     不完全恢復就是將db恢復到非當前時刻,只利用部分redo跟歸檔日誌,恢復到知道的scn或者指定的時間點的狀態,(基於時間點的恢復)

     不完全恢復使用於以下情況

       介質損壞導致部分日誌不可用

       使用者誤刪資料,無法邏輯恢復

       由於丟失部分歸檔,無法進行完全恢復

       控制檔案丟失,只能以備份的控制檔案開啟資料庫

要進行不完全恢復,必須要有適當的備份,並且備份是在恢復之前的時間點建立的,

首先利用備份恢復到備份時的狀態,然後利用重做日誌在恢復到指定的時間點,最後以

Open resetlogs開啟db

 由於不完全恢復只利用了部分日誌,需要給oracle指定結束標誌

 基於時間:指定一個時間

 基於scn:指定scn號,在oracle中,scn可以轉換成時間點,scn更加精確

 基於cancel:應用所有能應用的日誌,直到使用者取消

 基於日誌序號:指定歸檔日誌檔案序號

2.4 恢復操作:

1 db的恢復是指將db恢復到一個一致性的狀態,

恢復操作可分為:資料修復和恢復

      修復:將要恢復的檔案從備份集中讀取出來,並保持到指定的路徑,

      恢復:應用所有重做日誌,將db恢復到崩潰前的狀態,或者應用部分redo,恢復到指定的時間的狀態,

   2 Resetlogs操作

  在執行完不完全恢復操作或者使用了備份的控制檔案進行恢復或者執行了flashback database操作之後,在開啟資料庫時必須指定resetlogs

 

     必須在執行完不完全恢復後,以resetlogs操作,日誌檔案序號重置到1

 

 Resetlogs操作:

  歸檔當前的線上日誌,然後清空日誌,並把日誌序號重置為1,(如果線上重做日誌檔案不存在,則重建)

 重置控制檔案中關於線上日誌檔案的元資料

 更新資料檔案和redo中的resetlogs scn 和重置時間資訊,