聽說過還原(restore)資料庫,資料表空間及資料庫檔案,使用歸檔日誌恢複(recover)資料庫,資料表空間,資料庫檔案。咦,還有還原歸檔日誌這一說法呢?沒錯,可能我們忽略了還原歸檔日誌這一個過程,原因是還原歸檔日誌通常情況下是Oracle在recover時自動完成的。大多數情況下我們是先還原資料庫,恢複資料庫,開啟資料庫。實際上在恢複資料庫之前有一個動作,那就是還原歸檔日誌,也就是將記錄檔還原到預設的歸檔位置,如果我們在備份歸檔日誌時使用了delete [all] input子句的話。本文對此給出了單獨還原歸檔日誌以及恢複歸檔日誌的樣本以及restore archivelog的一些用法,僅僅是為了更好來的理解還原與恢複的過程,因為大多數情形下,資料檔案被還原到預設路徑。如果是還原到非預設路徑,那就需要手動restore archivelog。
1、理解還原與恢複
還原(restore): 還原指將資料檔案(可能受損)用之前的備份來替代或者複製到新的路徑,這個是大多數情形和通常的說法。
恢複(recover): 將備份之後的歸檔日誌apply到資料庫,也就根據歸檔日誌的事務將資料庫重新整理到特定或最新狀態(通常在還原之後操作)。對于歸檔日誌中那些已提交的事務進行前滾,未提交的事務進行復原。
還原歸檔日誌: 還原歸檔日誌是位於還原資料庫與恢複資料庫之間的這麼一個過程。它會將那些在備份歸檔日誌時使用delete [all] input方式刪除的歸檔日誌還原到預設的歸檔位置。在還原資料庫之後,如果要做recover,也就是作介質恢複那就需要用到歸檔日誌。那還原之後進行
recover需要的歸檔日誌在哪裡呢?歸檔日誌在指定的歸檔路徑那裡,那到底有沒有呢?如果有,還原時出現提示,歸檔日誌已經在指定位置。
如果沒有,但是備份的歸檔備份組那裡有,也行啊。備份組裡包含備份片,也就是打包了歸檔日誌。那既然打包就要解包,解包到預設路徑或指定路徑。這就是還原歸檔日誌。
2、樣本示範還原歸檔日誌
--示範環境
--為了較好的類比還原歸檔日誌,我們僅僅使用了一個特定的資料檔案進行copy方式備份,然後備份歸檔日誌(備份時刪除歸檔日誌)
--接下來破壞資料檔案,還原資料檔案,還原歸檔記錄檔,恢複記錄檔。
[oracle@linux3 ~]$ cat /etc/issue
Enterprise Linux Enterprise Linux Server release 5.5 (Carthage)
Kernel \r on an \m
[oracle@linux3 ~]$ sqlplus -V
SQL*Plus: Release 11.2.0.1.0 Production
a、備份資料檔案及歸檔日誌
RMAN> list backup of archivelog all; --->列出當前資料庫已經備份的歸檔日誌
specification does not match any backup in the repository
RMAN> list backupset; --->列出當前資料庫已存在的備份組
specification does not match any backup in the repository
SQL> select username,default_tablespace from dba_users where username='SCOTT'; -->查看使用者scott所在的資料表空間及資料檔案
USERNAME DEFAULT_TABLESPACE
------------------------------ ------------------------------
SCOTT USERS
SQL> select name,file# from v$datafile where tablespace_name='USERS';
NAME FILE#
------------------------------------------------------------ ----------
/u01/database/sybo3/oradata/users01.dbf 4
SQL> conn scott/tiger;
Connected.
SQL> select name,sequence#,status,COMPLETION_TIME from v$archived_log where status='A'; -->當前系統無任何歸檔日誌
no rows selected
SQL> host;
RMAN> copy datafile 4 to '/u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/users01.dbf'; -->使用rman copy方式備份資料檔案
RMAN> list copy;
using target database control file instead of recovery catalog
specification does not match any control file copy in the repository
specification does not match any archived log in the repository
List of Datafile Copies
=======================
Key File S Completion Time Ckp SCN Ckp Time
------- ---- - ------------------- ---------- -------------------
3 4 A 2013/07/26 20:10:31 961662 2013/07/26 20:10:31
Name: /u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/users01.dbf
Tag: TAG20130726T201031
-->準備測試表用於驗證還原恢複是否成功
SQL> create table t1 (seq varchar2(10),who varchar2(20));
SQL> insert into t1 select 'First','Robin' from dual;
SQL> commit;
SQL> alter system archive log current; -->產生歸檔日誌
SQL> select name,sequence#,status,COMPLETION_TIME from v$archived_log where status='A';
NAME SEQUENCE# S COMPLETION_TIME
-------------------------------------------------------------------------------- ---------- - -----------------
/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_27_8z4sy5ft_.arc 27 A 20130726 20:12:53
SQL> insert into t1 select 'Second','Robinson' from dual;
SQL> commit;
SQL> alter system archive log current; -->再次產生歸檔日誌
SQL> select name,sequence#,status,COMPLETION_TIME from v$archived_log where status='A';
NAME SEQUENCE# S COMPLETION_TIME
-------------------------------------------------------------------------------- ---------- - -----------------
/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_27_8z4sy5ft_.arc 27 A 20130726 20:12:53
/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_28_8z4t1q0s_.arc 28 A 20130726 20:14:47
-->下面備份歸檔日誌並刪除已備份的歸當日誌
RMAN> backup archivelog all delete input;
Starting backup at 2013/07/26 20:16:39
current log archived
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=21 device type=DISK
channel ORA_DISK_1: starting archived log backup set
channel ORA_DISK_1: specifying archived log(s) in backup set --->備份組裡包含的歸檔日誌
input archived log thread=1 sequence=27 RECID=23 STAMP=821823173
input archived log thread=1 sequence=28 RECID=24 STAMP=821823287
input archived log thread=1 sequence=29 RECID=25 STAMP=821823400
channel ORA_DISK_1: starting piece 1 at 2013/07/26 20:16:40
channel ORA_DISK_1: finished piece 1 at 2013/07/26 20:16:41
piece handle=/u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/o1_mf_annnn_TAG20130726T201640_8z4t58tn_.bkp tag=TAG20130726T201640 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01
channel ORA_DISK_1: deleting archived log(s) --->這裡提示刪除
archived log file name=/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_27_8z4sy5ft_.arc RECID=23 STAMP=821823173
archived log file name=/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_28_8z4t1q0s_.arc RECID=24 STAMP=821823287
archived log file name=/u01/database/sybo3/fra/SYBO3/archivelog/2013_07_26/o1_mf_1_29_8z4t585k_.arc RECID=25 STAMP=821823400
Finished backup at 2013/07/26 20:16:41
b、類比破壞資料檔案
SQL> insert into t1 select 'Last','End of test' from dual;
SQL> commit;
SQL> ho cat /dev/null>/u01/database/sybo3/oradata/users01.dbf --->破壞資料檔案
SQL> select * from t1; --->此時buffer cache依舊可以查詢到資料
SEQ WHO
---------- --------------------
First Robin
Second Robinson
Last End of test
SQL> alter system checkpoint; --->實施檢查點進程
System altered.
SQL> select * from t1; --->此時資料檔案不可訪問
select * from t1
*
ERROR at line 1:
ORA-00376: file 4 cannot be read at this time
ORA-01110: data file 4: '/u01/database/sybo3/oradata/users01.dbf'
SQL> select * from v$recover_file;
select * from v$recover_file
*
ERROR at line 1:
ORA-01135: file 4 accessed for DML/query is offline
ORA-01110: data file 4: '/u01/database/sybo3/oradata/users01.dbf'
c、還原與恢複受損的資料檔案
SQL> select TABLESPACE_NAME,STATUS from dba_tablespaces where tablespace_name='USERS'; --->tablespace 依舊是Online
TABLESPACE_NAME STATUS
------------------------------ ---------
USERS ONLINE
SQL> alter tablespace users offline immediate; --->offline受損的tablespace
Tablespace altered.
SQL> select tablespace_name,status from dba_tablespaces where tablespace_name='USERS'; --->此時狀態為offline
TABLESPACE_NAME STATUS
------------------------------ ---------
USERS OFFLINE
RMAN> restore datafile 4; --->此時使用restore datafile 方式提示失敗
Starting restore at 2013/07/26 20:30:20
using channel ORA_DISK_1
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 07/26/2013 20:30:20
ORA-01135: file 4 accessed for DML/query is offline
ORA-01110: data file 4: '/u01/database/sybo3/oradata/users01.dbf'
RMAN-06010: error while looking up datafile: 4
RMAN> restore tablespace users; --->此時使用restore tablespace 方式提示失敗,看來,對於copy方式的備份,必須要copy回去
Starting restore at 2013/07/26 20:31:12
using channel ORA_DISK_1
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 07/26/2013 20:31:12
ORA-01135: file 4 accessed for DML/query is offline
ORA-01110: data file 4: '/u01/database/sybo3/oradata/users01.dbf'
RMAN-06019: could not translate tablespace name "USERS"
-->下面直接使用copy方式進行還原
SQL> ho cp /u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/users01.dbf /u01/database/sybo3/oradata/users01.dbf
-->接下來我們還原歸檔日誌,制定了from sequence子句,實際上,如果我們沒有指定restore archivelog,在recover時也會自動完成還原歸檔日誌
RMAN> restore archivelog from sequence 27;
Starting restore at 2013/07/26 20:36:55
using channel ORA_DISK_1
channel ORA_DISK_1: starting archived log restore to default destination --->這個地方是關鍵提示,還原到預設位置
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=27
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=28
channel ORA_DISK_1: restoring archived log
archived log thread=1 sequence=29
channel ORA_DISK_1: reading from backup piece /u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/o1_mf_annnn_TAG20130726T201640_8z4t58tn_.bkp
channel ORA_DISK_1: piece handle=/u01/database/sybo3/fra/SYBO3/backupset/2013_07_26/o1_mf_annnn_TAG20130726T201640_8z4t58tn_.bkp tag=TAG20130726T201640
channel ORA_DISK_1: restored backup piece 1
channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
Finished restore at 2013/07/26 20:36:57
--Author : Robinson
--Blog :
推薦閱讀:
Oracle EBS 複製之資料庫複製
基於使用者管理的同機資料庫複製
基於 RMAN 的同機資料庫複製
RMAN 異機複製資料庫