oracle RMAN備份指令碼

zhengbao_jun發表於2009-11-06

 

 

資料庫備份方案

 

 

 

 

 

 

 

 

 

作者:                               Oracle Consulting Services

文件建立日期:                        Aug 1, 2000

最近變更日期:                        Mar 5 , 2001

文件引用:                           

版本號:                             v1.0

 

Note:Title, Subject, Last Updated Date, Reference Number, and Version are marked by a Word Bookmark so that they can be easily reproduced in the header and footer of documents.  When you change any of these values, be careful not to accidentally delete the bookmark.  You can make bookmarks visible by selecting Tools->OptionsView and checking the Bookmarks option in the Show region.

審批:

 

 

Note:To add additional approval lines, Press [Tab] from the last cell in the table above.

                                                Copy Number  _____

Note:You can delete any elements of this cover page that you do not need for your document.  For example, Copy Number is only required if this is a controlled document and you need to track each copy that you distribute.

 

 

變更記錄

1

日期

作者

版本號

變更參考檔案

 

 

 

 

Aug 20, 2001

李楚良

Draft 1a

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

審閱

 

姓名

職位

 

 

 

 

 

 

 

 

 

 

 

 

發行

 

拷貝號

姓名

地址

 

 

 

1          

 

 

2          

 

 

3          

 

 

4          

 

 

Note:The copy numbers referenced above should be written into the Copy Number space on the cover of each distributed copy.  If the document is not controlled, you can delete this table and the Copy Number label from the cover page.

 

目錄

文件控制.................................................................................................................................. 2

RMAN備份概述.................................................................................................................... 4

線上熱備份...................................................................................................................... 4

多級增量備份.................................................................................................................. 4

並行備份,恢復............................................................................................................. 5

減少所需要的備份資料量............................................................................................ 5

使用簡單.......................................................................................................................... 5

配置RMAN............................................................................................................................. 6

CATALOG資料庫......................................................................................................... 6

磁帶介面.......................................................................................................................... 7

多級備份策略.................................................................................................................. 7

維護RMAN........................................................................................................................... 12

檢查RMAN備份資訊................................................................................................. 12

RMAN資訊同步.......................................................................................................... 12

使用RMAN進行恢復......................................................................................................... 13

恢復整個資料庫........................................................................................................... 13

恢復單個表空間........................................................................................................... 13

其他......................................................................................................................................... 15

archive 方式................................................................................................................ 15

 

Note:To update the table of contents, put the cursor anywhere in the table and press [F9].  To change the number of levels displayed, select the menu option Insert‑>Index and Tables, make sure the Table of Contents tab is active, and change the Number of Levels to a new value.

 

RMAN備份概述

採用資料庫提供的RMAN備份工具有以下的優點:

     支援線上熱備份

     支援多級增量備份

     支援並行備份,恢復

     減小所需要備份量

     備份,恢復使用簡單

    

 

線上熱備份

是指備份不需要關閉資料庫進行,在備份的同時可以進行正常的資料庫的各種操作,滿足了7*24的系統的需要,對於本系統而言,資料庫的備份將不會影響INTERNETINTRANET使用者對資料庫的訪問。

 

多級增量備份

多級增量備份是指第N級的備份只需要備份最後一次同級或N-1級備份以後發生的改變的資料。可以通過下圖來說明:

 

 

上圖是一個增量備份的例子,即在第一個星期天做一個增量的0級備份,然後在星期一,星期二做一個增量的2級備份,在星期三做一個增量的1級備份,然後類推。假設現在在星期五資料庫需要做恢復,則可以先恢復第一個星期天的0級備份,,然後恢復星期三的1級備份,再恢復星期四和星期五的2級備份就可以完成資料庫的恢復。

 

因為在本系統錄入後的資料資料進入資料庫後將大部分變為只讀狀態,所以採用對這些表空間只備份一次或比經常改變的表空間備份次數更少的方法,將能夠很大地減少備份的資料量。具體的方案如下:

對於表PROD_CONTENT_CN_TBLPROD_VALUE_TBL由於做了關於序列號的分割槽,採取的當一個分割槽滿了以後,在資料庫裡把該分割槽改成只讀狀態,然後做一個該表空間的備份,然後在做資料庫的日常增量備份時忽略該表空間的資料則能夠大大減少要備份的資料量。

 

 

並行備份,恢復

RMAN是通過啟動資料庫的SERVER程式來進行備份和恢復,而且支援啟動多個SERVER程式來進行備份和恢復,在同一個SERVER程式內還支援多個BACKUPSET(備份集)的同時產生。主要是通過設定多個通道及filesperset引數來達到並行的目的。

 

分配多個通道的語句(以下語句分配兩個通道)

Allocate channel dev_1 type disk;

Allocate channel dev_2 type disk;

 

設定在一個通道同時開啟的備份檔案語句(以下語句設定在一個通道下同時開啟3個檔案)

backup filesperset 3。。。

 

 

 

減少所需要的備份資料量

因為RMAN是工作在資料快一級,所以能夠只備份分配的資料快,這樣就大大地減少了所需要的備份的資料量,特別是對於預先分配空間的資料庫而言。

 

使用簡單

RMAN的使用特別簡單,在進行備份和恢復時都不需要指定需要備份或需要恢復的資料檔案,RMAN會自動地把備份或恢復所需要的資料檔案進行備份或進行恢復。減少了人為操作可能產生的錯誤。

 

如以下是備份整個資料庫的語句

backup full tag 'basicdb' format '/bak/bak/basic/basic%u_%p_%c' database ;

 

以下是備份一個表空間的語句

backup tag 'tsusers' format '/bak/bak/basic/basic%u_%p_%c' tablespace users ;

 

以下是備份歸檔檔案的語句

backup filesperset 3   archivelog all delete input;

歸檔檔案會在備份後自動刪除。

 

配置RMAN包括配置CATALOG資料庫,配置資料庫與媒體管理軟體的介面,制定RMAN的多級備份方案,寫RMAN備份指令碼

 

CATALOG資料庫

因為RMAN自動維護備份和恢復所需要的各種資訊,所以RMAN必須把這些以某種形式儲存。RMAN支援兩種形式儲存這些資訊,資料庫的控制檔案或建立一個單獨的資料庫來儲存RMAN的資訊。

 

當選擇把RMAN的資訊儲存在控制檔案時,控制檔案的丟失時將導致備份將不能進行恢復。所以若採用RMAN做備份,推薦一定採用RMAN CATALOG資料庫來單獨存放備份資訊。這個單獨的資料庫(稱為CATALOG資料庫)只需要很小的空間,既可以和被備份的資料庫(E10K)放在同一主機上,也可以單獨放在另一臺主機上(如果條件允許,推薦放在一臺單獨的主機上來確保最大的可恢復性)。

 

備份CATALOG資料庫:因為CATALOG資料庫包含了所有的備份資訊,所以該資料庫本身也是需要通過某種方法進行備份,但因為該資料庫很小(一年內可能才增加十幾二十兆),所以既可以對它進行冷備份,也可以進行邏輯的輸出(EXPORT)。

 

配置CATALOG資料庫:

1.dbassist建立資料庫。

2.在該資料庫建立RMAN資料庫使用者:

create user rman identified by rman default tablespace ts_rman

temporary tablespace temp;

grant connect ,resource , RECOVERY_CATALOG_OWNER to rman.

3.連線到目標資料庫和CATALOG資料庫

rman target system/manager@target_tnsname rcvcat rman/rman@catalog_tnsname

4.建立CATALOG使用者的表:

rman>create catalog

5.登記目標資料庫:

rman>register database

 

這樣就可以利用該RMAN資料庫來備份目標資料庫了。

 

 

磁帶介面

當使用專用的磁帶管理工具時,必須配置資料庫與磁帶的介面,一般是管理工具提供一個動態連線庫與資料庫進行連線。

以下是一個測試的RMAN指令碼:

run {

allocate channel 'dev1' type disk

resync catalog;

backup format /archive/ctl%u_%p_%c current controlfile;

release channel dev1;

}

 

 

多級備份策略

採用多級備份是為了減少了恢復所需要的時間和減少每天備份所需要的時間,而又保證系統有很好的恢復性。但是在恢復時間和備份時間要有一個權衡。比如只要開始的一個全備份和備份所有產生的歸檔檔案就可以保證把資料庫恢復到最新的狀態,但是一般來說實際上並不會這麼進行(因為在恢復時將需要很長很長的時間),多級備份就是為了解決這樣的問題。以下是一種建議的方案。

 

每半年做一個資料庫的全備份(包含只讀表空間)

每個月做一次零級備份(不包含只讀表空間)

每個星期做一次一級備份

每天做一個二級備份

任何表空間改成只讀狀態後做一個該表空間的備份。

當需要時(如四個小時歸檔檔案系統就要接近滿了)備份歸檔檔案

 

資料庫全備份指令碼:

run {

allocate channel 'dev1' type disk   ;

allocate channel 'dev2' type disk ;

allocate channel 'dev3' type disk ;

backup full  tag 'dbfull' format '/o9i/testrman/full%u_%p_%c' database ;

sql 'alter system archive log current' ;

backup filesperset 3   archivelog all delete input;

release channel dev1;

release channel dev2;

release channel dev3;

}

 

零級備份指令碼(只讀表空間除外)

run {

allocate channel 'dev1' type disk   ;

allocate channel 'dev2' type disk ;

allocate channel 'dev3' type disk ;

backup incremental level 0  tag 'dbL0' format '/o9i/testrman/basic%u_%p_%c' database skip readonly;

sql 'alter system archive log current' ;

backup filesperset 3   archivelog all delete input;

release channel dev1;

release channel dev2;

release channel dev3;

}

一級備份指令碼

run {

allocate channel 'dev1' type disk   ;

allocate channel 'dev2' type disk ;

allocate channel 'dev3' type disk ;

backup incremental level 1  tag 'dbL1' format '/o9i/testrman/basic%u_%p_%c' ddatabase skip readonly;

sql 'alter system archive log current' ;

backup filesperset 3   archivelog all delete input;

release channel dev1;

release channel dev2;

release channel dev3;

}

 

二級備份指令碼

run {

allocate channel 'dev1' type disk   ;

allocate channel 'dev2' type disk ;

allocate channel 'dev3' type disk ;

backup incremental level 2  tag 'dbL2' format '/o9i/testrman/basic%u_%p_%c' ddatabase skip readonly;

sql 'alter system archive log current' ;

backup filesperset 3   archivelog all delete input;

release channel dev1;

release channel dev2;

release channel dev3;

}

表空間備份指令碼(以users表空間為例)

run {

allocate channel 'dev1' type disk   ;

allocate channel 'dev2' type disk ;

allocate channel 'dev3' type disk ;

backup  tag 'tsusers' format '/o9i/testrman/basic%u_%p_%c' tablespace users;

sql 'alter system archive log current' ;

backup filesperset 3   archivelog all delete input;

release channel dev1;

release channel dev2;

release channel dev3;

}

 

歸檔檔案備份指令碼

run {

allocate channel 'dev1' type disk   ;

allocate channel 'dev2' type disk ;

allocate channel 'dev3' type disk ;

backup filesperset 3   archivelog all delete input;

release channel dev1;

release channel dev2;

release channel dev3;

}

 

則每天所需要備份的資料量只有一天的改變數。而做恢復時最多隻要恢復當月的一個零級備份+三個一級備份+6個二級備份+當天的歸檔檔案。如果不能接受這樣的恢復時間,就只能夠減少零級備份之間的時間間隔(如可以每個星期做一個零級備份,這樣恢復時最多隻需要恢復一個星期的資料量)。

 

備份CATALOG資料庫(資料庫邏輯輸出)

exp pafile=exp.dat

其中exp.dat如下

userid=rman/rman

file=rmanexp.dmp

RMAN的維護工作主要是:

1.檢視備份資訊

2.因為手工刪除了備份檔案或歸檔檔案需要把RMAN CATALOG資料庫的資訊與手工刪除的資訊同步。

 

 

檢查RMAN備份資訊

列出已經過時的備份:

RMAN>report obsolete;

 

刪除過時的備份資訊

RMAN> allocate channel for maintenance type disk;

RMAN> change backupset id delete;

RMAN> release channel;

 

 

RMAN資訊同步

當手工刪除了資料庫的歸檔檔案後,要執行以下指令碼進行同步:

RMAN> allocate channel for maintenance type disk;

RMAN> change archivelog all crosscheck;

RMAN> release channel;

 

當手工刪除了資料庫的RMAN備份檔案後,要執行以下指令碼進行同步:

RMAN> allocate channel for maintenance type disk;

RMAN> crosscheck backup;

RMAN> delete expired backup;

RMAN> release channel;

 

使用RMAN進行恢復

資料庫的的恢復比較複雜,有許多不同的情況必須在瞭解ORACLE資料庫結構的條件下用不同的方法處理,RMAN的好處在於大部分情況不需要理解資料庫的結構而使用一些最簡單語句來進行恢復,這裡只討論了最常用最普通的恢復情況

 

 

恢復整個資料庫

在恢復整個資料庫之前必須讓資料庫處於安裝的狀態(mount狀態)

svrmgrl>startup mount

 

rman>

run {

allocate channel 'dev1' type disk   ;

allocate channel 'dev2' type disk ;

allocate channel 'dev3' type disk ;

restore  database ;

recover database ;

release channel dev1 ;

release channel dev2 ;

release channel dev3 ;

}

 

 

恢復單個表空間

在恢復該表空間之前讓該表空間處於離線的狀態(offline狀態)

svrmgrl>alter tablespace users offline;

 

rman>

run {

allocate channel 'dev1' type disk   ;

allocate channel 'dev2' type disk ;

allocate channel 'dev3' type disk ;

restore  tablespace users ;

recover tablespace users ;

release channel dev1 ;

release channel dev2 ;

release channel dev3 ;

}

 

資料庫要使用RMAN必須符合一些條件

 

 

archive 方式

資料庫要執行在歸檔模式下,這樣資料庫將有很好的可恢復性

 

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

相關文章