Oracle 物理備份的種類以及完全恢復(fù)
完全脫機備份(冷備份)(noarchivelog | archivelog)
- 優(yōu)點:安全
- 缺點: OS Copy(生產(chǎn)環(huán)境,數(shù)據(jù)文件較大)
- 缺點:需要shutdown(生產(chǎn)環(huán)境,風險較大)
部分脫機備份(表空間tablespace offline,相當于局部的shutdown)
- 缺點:OS Copy - 整個數(shù)據(jù)文件的拷貝,沒有解析出有效數(shù)據(jù)
- 有些表空間無法offline - system, undo
SQL> alter tablespace system offline;
alter tablespace system offline
*
ERROR at line 1:
ORA-01541: system tablespace cannot be brought offline; shut down if necessary
SQL> alter tablespace UNDOTBS1 offline;
alter tablespace UNDOTBS1 offline
*
ERROR at line 1:
ORA-30042: Cannot offline the undo tablespace - offilne + copy + online
SQL> alter tablespace SYSAUX offline;
Tablespace altered.
SQL> alter tablespace SYSAUX online;
Tablespace altered. - 案例:損壞sample數(shù)據(jù)文件
-- 插入系列數(shù)據(jù)到指定tablespace,之后offile指定tablespace
SQL> alter tablespace "sample" offline;
Tablespace altered.
-- ========== 刪除 指定tablespace==========
-- online指定tablespace,會報錯:無法鎖定
SQL> alter tablespace "sample" online;
alter tablespace "sample" online
*
ERROR at line 1:
ORA-01157: cannot identify/lock data file 5 - see DBWR trace file
ORA-01110: data file 5: 'E:\ORACLE\WPENG\WPENG\SAMPLE.DBF'
-- 此時數(shù)據(jù)庫顯示open狀態(tài),沒有shutdown
SQL> select status from v$instance;
STATUS
------------
OPEN
-- ======== copy恢復(fù)之前的備份tablespace =======
-- 再次online 指定tablespace,報錯:需要介質(zhì)恢復(fù)(數(shù)據(jù)文件和控制文件的scn不相符)
SQL> alter tablespace "sample" online;
alter tablespace "sample" online
*
ERROR at line 1:
ORA-01113: file 5 needs media recovery
ORA-01110: data file 5: 'E:\ORACLE\WPENG\WPENG\SAMPLE.DBF'
-- 控制文件 和 數(shù)據(jù)文件頭的 SCN 不符
SQL> select file#, checkpoint_change# from v$datafile where file# = 5;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
5 2798236
SQL> select file#, checkpoint_change# from v$datafile_header where file# = 5;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
5 0
-- 進行recover 恢復(fù) datafile 5
SQL> recover datafile 5;
ORA-00279: change 2798091 generated at 09/29/2012 09:13:56 needed for thread 1
ORA-00289: suggestion : E:\APP\WPENG\PRODUCT\11.1.0\FLASH_RECOVER_AREA\WPENG\ARCHIVELOG\2012_09_29\O1_MF_1_127_86DLZ4TD_
.ARC
ORA-00280: change 2798091 for thread 1 is in sequence #127
Specify log: {<RET>=suggested | filename | AUTO | CANCEL}
auto
Log applied.
Media recovery complete.
-- 第三次online datafile 5,成功
SQL> alter tablespace "sample" online;
Tablespace altered.
-- 查看控制文件和數(shù)據(jù)文件頭的SCN
SQL> select file#, checkpoint_change# from v$datafile where file# = 5;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
5 2798388
SQL> select file#, checkpoint_change# from v$datafile_header where file# = 5;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
5 2798388 - 哪個數(shù)據(jù)文件壞了,可以offline哪個
SQL> alter database datafile 5 offline;
Database altered.
SQL> alter database datafile 5 online;
alter database datafile 5 online
*
ERROR at line 1:
ORA-01113: file 5 needs media recovery
ORA-01110: data file 5: 'E:\ORACLE\WPENG\WPENG\SAMPLE.DBF'
SQL> recover datafile 5;
Media recovery complete.
SQL> alter database datafile 5 online;
Database altered. - 非歸檔模式,表空間是不可以offline的
部分聯(lián)機備份(熱備份)
- 不需要shutdown + 不需要offline
- 記錄數(shù)據(jù)恢復(fù)的起點SCN,就是數(shù)據(jù)文件頭的checkpoint_change#
SQL> alter tablespace USERS begin backup;
表空間已更改。
-- 記錄數(shù)據(jù)庫恢復(fù)的起點
SQL> select file#, checkpoint_change# from v$datafile_header;
FILE# CHECKPOINT_CHANGE#
---------- ------------------
1 1177691
2 1164232
3 1164232
4 1177920
SQL> select * from v$backup;
FILE# STATUS CHANGE# TIME
---------- ------------------ ---------- --------------
1 NOT ACTIVE 1177691 12-10月-12
2 NOT ACTIVE 0
3 NOT ACTIVE 0
4 ACTIVE 1177920 12-10月-12
--========COPY需要備份的數(shù)據(jù)文件==========
SQL> alter tablespace USERS end backup;
表空間已更改。
--==========RECOVER的時候,會從記錄的BEGIN處開始恢復(fù)
SQL> recover datafile 4
ORA-00279: 更改 1177920 (在 10/12/2012 14:00:48 生成) 對于線程 1 是必需的
ORA-00289: 建議:
C:\APP\WPENG\FLASH_RECOVERY_AREA\ORCL\ARCHIVELOG\2012_10_12\O1_MF_1_29_87HDP56C_
.ARC
ORA-00280: 更改 1177920 (用于線程 1) 在序列 #29 中
指定日志: {<RET>=suggested | filename | AUTO | CANCEL}
- 缺點:僅僅只是記錄數(shù)據(jù)頭的checkpoint_change#,但是數(shù)據(jù)文件其他部分會發(fā)生變化
- 操作系統(tǒng)的塊大小:512 bytes;數(shù)據(jù)庫系統(tǒng)的塊大小:8K(16個操作系統(tǒng)塊)。我們備份copy的時候(512 bytes),數(shù)據(jù)庫系統(tǒng)可能會修改數(shù)據(jù)塊(8K)。 備份出去的文件是正確的;但是對于數(shù)據(jù)庫邏輯上,可能是不一致的,導(dǎo)致不可用的情況發(fā)生。
- Oracle規(guī)定:凡事數(shù)據(jù)文件處于熱備份模式下,有進程操作這個數(shù)據(jù)文件上的數(shù)據(jù)塊 - Oracle會首先對這個數(shù)據(jù)塊進行備份,以日志的形式進行備份。
- 處于熱備份的情況,參照上述,那么數(shù)據(jù)塊將會產(chǎn)生多余平常情況下的日志文件 - 而且是多了多!!!
-
由此可以看到,同樣的SQL語句,執(zhí)行相同的操作,在熱備份模式下數(shù)據(jù)庫的日志文件會增加8840 bytes;而在正常模式下,數(shù)據(jù)的日志文件僅僅增加496 bytes
SQL> select distinct sid from v$mystat;
SID
----------
47
SQL> select STATISTIC#, NAME from v$statname where name = 'redo size';
STATISTIC# NAME
---------- ------------------------------------------------------------
169 redo size
SQL> select * from v$mystat where sid=47 and statistic#=169;
SID STATISTIC# VALUE
---------- ---------- ----------
47 169 0
SQL> select *from t;
ID SCN
---------- ----------
11 1178189
12 1178215
13 1178223
14 1178238
15 1178252
SQL> select * from v$mystat where sid=47 and statistic#=169;
SID STATISTIC# VALUE
---------- ---------- ----------
47 169 116
SQL> insert into t values(1,1);
已創(chuàng)建 1 行。
SQL> select * from v$mystat where sid=47 and statistic#=169;
SID STATISTIC# VALUE
---------- ---------- ----------
47 169 116
SQL> commit;
提交完成。
SQL> select * from v$mystat where sid=47 and statistic#=169;
SID STATISTIC# VALUE
---------- ---------- ----------
47 169 612
SQL> alter tablespace USERS begin backup;
表空間已更改。
SQL> select * from v$mystat where sid=47 and statistic#=169;
SID STATISTIC# VALUE
---------- ---------- ----------
47 169 1328
SQL> insert into t values(1,1);
已創(chuàng)建 1 行。
SQL> commit;
提交完成。
SQL> select * from v$mystat where sid=47 and statistic#=169;
SID STATISTIC# VALUE
---------- ---------- ----------
47 169 10168
SQL> select 10168-1328 from dual;
10168-1328
----------
8840
SQL> select 612-116 from dual;
612-116
----------
496
SQL> alter tablespace USERS end backup;
表空間已更改。