1. 程式人生 > >ORACLE歸檔模式下的熱備以及資料庫開啟狀態狀態下普通資料檔案損壞後的還原

ORACLE歸檔模式下的熱備以及資料庫開啟狀態狀態下普通資料檔案損壞後的還原

前提:歸檔模式下開啟熱備份模式 1.備份資料庫
在PL/SQL下執行以下SQL產生啟動表空間的熱備模式:
select 'alter tablespace ' || tablespace_name || ' begin backup;' from dba_tablespaces where logging = 'LOGGING';
[oracle@oracledb ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Fri Jan 20 07:35:51 2017Copyright (c) 1982, 2013, Oracle.  All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> alter tablespace SYSTEM begin backup; alter tablespace SYSAUX begin backup; alter tablespace UNDOTBS1 begin backup; alter tablespace USERS begin backup; alter tablespace BARD begin backup; alter tablespace BARX begin backup; Tablespace altered.SQL> Tablespace altered.SQL> Tablespace altered.SQL> Tablespace altered.SQL> Tablespace altered. 啟動熱備模式後複製相關檔案進行儲存
su - oracle
cp -R /u01/oracle/oradata/orcl/* /u01/oracle/orabak/hotbak/2017-01-19/ 完成檔案複製後進行end backup操作
su - oracle
sqlplus / as sysdba
alter tablespace SYSTEM end backup;
alter tablespace SYSAUX end backup;
alter tablespace UNDOTBS1 end backup;
alter tablespace USERS end backup;
alter tablespace BARD end backup;
alter tablespace BARX end backup;
2.刪除部分資料檔案
su - oracle
sqlplus / as sysdba 刪除一個數據檔案
!rm /u01/oracle/oradata/orcl/bard01.dbf
嘗試查詢這個表空間上的表,結果能訪問數
select count(*) from nvl_user.tmp1;
SQL> !rm /u01/oracle/oradata/orcl/bard01.dbf;

SQL> select count(*) from nvl_user.tmp1;

  COUNT(*)
----------
    300100


做一個全域性檢查點
alter system checkpoint;
SQL> alter system checkpoint;

System altered.


再次嘗試訪問表,發現已經無法訪問
select count(*) from nvl_user.tmp1;
SQL> select count(*) from nvl_user.tmp1;
select count(*) from nvl_user.tmp1
                              *
ERROR at line 1:
ORA-00376: file 6 cannot be read at this time
ORA-01110: data file 6: '/u01/oracle/oradata/orcl/bard01.dbf'



3.使用備份還原資料庫
嘗試offline表空間,但因檔案丟失,會失敗
alter tablespace bard offline; 需進入offline immediate狀態:
alter tablespace bard offline immediate;
SQL> alter tablespace bard offline immediate;

Tablespace altered.


從熱備中找到對應檔案還原
!cp /u01/oracle/orabak/hotbak/2017-01-19/bard01.dbf  /u01/oracle/oradata/orcl/ 進行表空間的恢復
SQL> recover tablespace bard;
Media recovery complete.


嘗試訪問表,發現還是不能訪問
SQL> select count(*) from nvl_user.tmp1;
select count(*) from nvl_user.tmp1
                              *
ERROR at line 1:
ORA-00376: file 6 cannot be read at this time
ORA-01110: data file 6: '/u01/oracle/oradata/orcl/bard01.dbf'


將表空間進行online操作
alter tablespace bard online; 再次訪問表,可以訪問了
SQL> alter tablespace bard online;

Tablespace altered.

SQL> select count(*) from nvl_user.tmp1;

  COUNT(*)
----------
    300100