練習:資料庫恢復案例

therorawt發表於2008-03-04

在網上找了些備份與恢復練習,自己動手操作了遍,現把具體步驟與操作過程中遇到的問題記錄如下:

目錄
No.1 非歸檔模式下的備份與恢復

No.2 歸檔模式下丟失或損壞一個資料檔案
--OS備份方案
--RMAN備份方案

No.3 丟失多個資料檔案,實現整個資料庫的恢復
--OS備份方案
--RMAN備份方案

[@more@]

No.4 不完全恢復案例
--OS備份下的基於時間的恢復
--RMAN備份下的基於改變的恢復

--------------------------------------

No.1非歸檔模式下的備份與恢復

1. 改為非歸檔模式
SQL> conn sys/sys as sysdba

SQL> shutdown immediate

SQL> startup mount

SQL> archive log list

SQL> alter database noarchivelog;

SQL> alter database open;

SQL> conn sys/sys as sysdba;

2. 新建測試使用者
--要刪除資料檔案,必須刪除資料檔案所在的表空間。
SQL> drop user therorawt cascade;
--cascade: Specify CASCADE to drop all objects in the user's schema before dropping the user.

SQL> drop tablespace test01 including contents;
--including contents: Specify INCLUDING CONTENTS to drop all the contents of the tablespace.

--新建表空間
SQL> CREATE TABLESPACE test01 DATAFILE 'F:oracleoradatamydbTEST01.DBF' SIZE 10M MINIMUM EXTENT 500K DEFAULT STORAGE (INITIAL 128K NEXT 128K) LOGGING;

--新建使用者
SQL> CREATE USER therorawt IDENTIFIED BY passwd DEFAULT TABLESPACE test01;

SQL> GRANT CONNECT,RESOURCE,DBA TO therorawt;

SQL> conn therorawt/passwd

--建測試資料
SQL> create table test(a int);

SQL> insert into test values(1);

SQL> commit;

3. 正常關閉資料庫,備份測試表空間
SQL> conn sys/sys as sysdba;

SQL> shutdown immediate

複製所有資料檔案、控制檔案,重做日誌檔案

4. 開啟資料庫,給TEST表再插入條資料
SQL> startup

SQL> conn therorawt/passwd

SQL> insert into test values(2);

5. 關閉資料庫,刪除表空間TEST01對應的資料檔案
SQL> conn sys/sys as sysdba

SQL> shutdown immediate

刪除表空間TEST01對應的資料檔案

6. 重啟資料庫,發現錯誤
資料庫裝載完畢。
ORA-01157: 無法標識/鎖定資料檔案 12 - 請參閱 DBWR 跟蹤檔案
ORA-01110: 資料檔案 12: 'F:ORACLEORADATAMYDBTEST01.DBF'

7. 複製備份復原到原來的位置(restore過程)
C:>xcopy E:backupTEST01.DBF F:oracleoradatamydbTEST01.DBF
目標 F:oracleoradatamydbTEST01.DBF 是檔名
還是目錄名
(F = 檔案,D = 目錄)? f
E:backupTEST01.DBF
複製了 1 個檔案

8. 開啟資料庫,檢查資料
SQL> startup
//SQL> recover --進行介質恢復(如果就備份當資料檔案,那麼需要這一步來進行介質恢復,如果備份所有資料檔案、控制檔案、重做日誌檔案,那麼就不需要這一步)
SQL> alter database open;
SQL> conn therorawt/passwd
SQL> select * from test;

--備份成功恢復--

執行過程遇到的問題:
1. 在第3步中,需要備份所有資料檔案、控制檔案、重做日誌檔案。
如果備份單個檔案,需要執行recover,從redofile進行自動恢復,不過,多執行幾次alter system switch logfile就無法恢復了。

/***********************************************************/

No.2歸檔模式下丟失或損壞一個資料檔案

OS備份方案

1. 改為歸檔模式
sqlplus "/ as sysdba"
SQL> shutdown immediate

SQL> startup mount

SQL> archive log list

SQL> alter database archivelog; --改為歸檔模式

SQL> alter database open;

SQL> conn therorawt/passwd

SQL> select * from test;

COL
----------
1
2
3
4
5
6

2. 備份資料庫
SQL> @"C:Documents and Settingsuser桌面hotbak.sql"
--指令碼見附1

3. 刪除測試表
SQL> conn therorawt/pass

SQL> delete from test where col not in(3,5);

SQL> select * from test;

COL
----------
3
5

SQL> commit;

SQL> conn sys/sys as sysdba

SQL> alter system switch logfile;

SQL> alter system switch logfile;

4. 關閉資料庫
SQL> shutdown immediate

刪除資料檔案TEST01.DBF

5. 啟動資料庫錯誤,離線該資料檔案
SQL> startup
--ORA-01157: 無法標識/鎖定資料檔案 12 - 請參閱 DBWR 跟蹤檔案
--ORA-01110: 資料檔案 12: 'F:ORACLEORADATAMYDBTEST01.DBF'

檢視告警檔案F:oracleadminmydbbdump,或動態檢視v$recover_file
SQL> select * from v$recover_file;

--確認檔案12的狀態
SQL> select * from v$datafile;

--離線資料檔案
--不離線,資料庫啟不來
SQL> alter database datafile 12 offline drop;

6. 開啟資料庫,進行restore(複製)、recover(恢復)
--restore、recover操作在資料檔案離線的狀態下進行
SQL> alter database open;

SQL> recover datafile 12
AUTO

--恢復成功,聯機該資料檔案
SQL> alter database datafile 12 online;

7. 檢查資料庫的資料(完全恢復)
SQL> conn therorawt/passwd

SQL> select * from test;

COL
----------
3
5

--備份成功恢復--

執行中遇到的問題:
1.
--sql*plus中可用 $ 或host來切換到dos方式;
--svrmgrl 中可用 ! 或host來切換到dos方式。

----------------------------------------

RMAN備份方案

1. 檢視測試資料
SQL> conn therorawt/passwd

SQL> select * from test;

COL
----------
3
5
7

2. 備份表空間TEST01
C:>rman target

RMAN> run{
2> allocate channel c1 type disk;
3> backup tag 'tstest01' format 'e:backuptstest01_%u_%s_%p'
4> tablespace test01;
5> release channel c1;
6> }

3. 在測試表中插入記錄
SQL> insert into test values(7);

SQL> commit;

SQL> select * from test;

COL
----------
7
5

SQL> alter system switch logfile;

SQL> alter system switch logfile;

4. 關閉資料庫,模擬丟失資料檔案
SQL> shutdown immediate

刪除資料檔案test01.dbf

5. 啟動資料庫,檢查錯誤
SQL> startup
--ORA-01157: 無法標識/鎖定資料檔案 12 - 請參閱 DBWR 跟蹤檔案
--ORA-01110: 資料檔案 12: 'F:ORACLEORADATAMYDBTEST01.DBF'

6. 先開啟資料庫
SQL> alter database datafile 12 offline drop;

SQL> alter database open;

7. 恢復該表空間
C:>rman target therorawt/passwd

RMAN> run{
2> allocate channel c1 type disk;
3> restore datafile 12;
4> recover datafile 12;
5> sql 'alter database datafile 12 online';
6> release channel c1;
7> }

8. 檢查資料是否完整
SQL> conn therorawt/passwd
已連線。
SQL> select * from test;

COL
----------
7
5

--備份成功恢復--

執行過程遇到的問題:
1. 當執行alter system switch logfile時,沒響應。
解決:
資料庫如果執行在歸檔模式下的,初始化引數裡面沒有把log_archive_start設為true的話,歸檔程式就不會自動啟動,這時候如果日誌檔案都寫滿的話,由於沒有歸檔程式將日誌檔案及時歸檔,資料庫會掛起來。改變這種狀態就是執行alter system archive log start命令手工啟動歸檔程式。事實上,正是這個原因造成。非歸檔模式日誌檔案迴圈使用,不進行歸檔,不存在這個問題。

2. 延伸出的問題:查詢v$session_wait,怎麼看這檢視的資料 ?

3. 更改引數
--true不需要加引號
SQL> alter system set log_archive_start = true scope = spfile;

4. 檢視系統使用spfile還是pfile
--如果以下語句返回為空,那麼說明使用pfile
SQL> SHOW PARAMETER spfile;

/***********************************************************/

No.3 丟失多個資料檔案,實現整個資料庫的恢復
OS備份方案
1. 檢視備份前的資料
SQL> conn therorawt/passwd

SQL> select * from test;

COL
----------
57

2. 備份資料庫,備份除臨時資料檔案後的所有資料檔案
SQL> @hotbak.sql
--指令碼見附1

3. 繼續在測試表中插入記錄
SQL> insert into test values (35);

SQL> select * from test;

COL
----------
57
35

SQL> commit;

SQL> alter system switch logfile;

SQL> alter system switch logfile;

4. 關閉資料庫,模擬資料檔案丟失
SQL> conn sys/sys as sysdba

SQL> shutdown immediate

刪除資料檔案JFK_DATA01.DBF、TEST01.DBF、SYSTEM01.DBF

5. 啟動資料庫,檢查錯誤
SQL> startup
資料庫裝載完畢。
ORA-01157: 無法標識/鎖定資料檔案 1 - 請參閱 DBWR 跟蹤檔案
ORA-01110: 資料檔案 1: 'F:ORACLEORADATAMYDBSYSTEM01.DBF'

--查詢需要恢復的資料檔案
SQL> select * from v$recover_file;

6. 複製備份回到原地點(restore),開始恢復資料庫(recover)
複製資料檔案JFK_DATA01.DBF、TEST01.DBF、SYSTEM01.DBF。

--恢復資料庫
SQL> recover database

SQL> alter database open;

7. 檢查資料庫的恢復
SQL> conn sys/sys as sysdba

SQL> select * from dba_data_files;

SQL> conn therorawt/passwd
已連線。
SQL> select * from test;

COL
----------
57
35

--備份成功恢復--

----------------------------------------
RMAN備份方案

RMAN備份歸檔模式下損壞(丟失)多個資料檔案,進行資料庫的恢復
1. 檢查測試資料
SQL> conn therorawt/passwd
已連線。
SQL> select * from test;

COL
----------
57
35

2. 進行RMAN備份
C:>rman target

RMAN> run{
2> allocate channel c1 type disk;
3> backup full tag 'dbfull' format 'e:backupfull%u_%s_%p' database
4> include current controlfile;
5> sql 'alter system archive log current';
6> release channel c1;
7> }

3. 改變測試資料
SQL> delete from test where col = 35;

SQL> select * from test;

COL
----------
57

SQL> commit;

SQL> alter system switch logfile;

SQL> alter system switch logfile;

4. 關閉資料庫,模擬資料庫檔案丟失
SQL> conn sys/sys as sysdba

SQL> shutdown immediate

刪除資料檔案USERS01.DBF、TEST01.DBF、JFK_DATA01.DBF

5. 啟動資料庫,檢視錯誤
SQL> startup
資料庫裝載完畢。
ORA-01157: 無法標識/鎖定資料檔案 1 - 請參閱 DBWR 跟蹤檔案
ORA-01110: 資料檔案 1: 'F:ORACLEORADATAMYDBUSERS01.DBF'

6. 用RMAN進行恢復
C:>rman target

RMAN> run{
2> allocate channel c1 type disk;
3> restore database;
4> recover database;
5> sql 'alter database open';
6> release channel c1;
7> }

7. 檢視恢復後資料
SQL> conn therorawt/passwd
已連線。
SQL> select * from test;

COL
----------
57

--備份成功恢復--

/***********************************************************/
No.4 不完全恢復案例
--OS備份下的基於時間的恢復
1. 檢視測試資料
SQL> show user
USER 為"THERORAWT"
SQL> select * from test;

COL
----------
57

2. 進行備份,指令碼見附1 hotbak.sql
SQL> @hotbak.sql

3. 改變測試資料,然後刪除測試表TEST,記下時間點
SQL> insert into test values(35);

SQL> select * from test;

COL
----------
57
35

SQL> commit;

SQL> alter system switch logfile;

SQL> alter system switch logfile;

--T1時間
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;

TO_CHAR(SYSDATE,'YY
-------------------
2008-03-04 18:14:14

SQL> truncate table test;

SQL> drop table test;

4. 重啟資料庫,檢視錯誤
SQL> conn sys/sys as sysdba

SQL> shutdown immediate

SQL> startup
ORA-01113: 檔案 1 需要介質恢復
ORA-01110: 資料檔案 1: 'F:ORACLEORADATAMYDBSYSTEM01.DBF'

5. 不完全恢復資料庫到T1時間
SQL> recover database until time '2008-03-04 18:14:14';
auto

--不完全恢復之後,都必須用resetlogs的方式開啟資料庫。
SQL> alter database open resetlogs;

SQL> conn therorawt/passwd

SQL> select * from test;

COL
----------
57
35

----------------------------------------
--RMAN備份下的基於改變的恢復
1. 檢視測試資料
SQL> conn therorawt/passwd

SQL> select * from test;

COL
----------
57
35

2. 進行RMAN備份
C:>rman target

RMAN> run{
2> allocate channel c1 type disk;
3> restore database;
4> recover database until scn 3120753;
5> sql 'alter database open resetlogs';
6> release channel c1;
7> }

3. 刪除測試表
SQL> delete from test where col = 35;

SQL> select * from test;

COL
----------
57

SQL> commit;

SQL> alter system switch logfile;

SQL> alter system switch logfile;

--獲取SCN
SQL> conn sys/sys as sysdba

SQL> select max(ktuxescnw*power(2,32)+ktuxescnb) scn from x$ktuxe;

SCN
----------
3120753

SQL> truncate table therorawt.test;

SQL> drop table therorawt.test;

4. 關閉資料庫,啟動到mount狀態下,恢復到SCU 3120753
SQL> shutdown immediate

SQL> startup mount

C:>rman target

--不完全恢復一樣,必須在mount下,restore所有備份資料檔案,需要resetlogs
RMAN> run{
2> allocate channel c1 type disk;
3> restore database;
4> recover database until scn 3120753;
5> sql 'alter database open resetlogs';
6> release channel c1;
7> }

5. 檢查測試資料
SQL> show user
USER 為"SYS"
SQL> select * from therorawt.test;

COL
----------
57

--備份成功恢復--

/***********************************************************/

----------------------------------------
附1 hotbak.sql
--------------
/*hotbak.sql*/

--archive
alter system archive log current;
--start

alter tablespace XDB begin backup;
$xcopy F:oracleoradatamydbXDB01.DBF e:backup/H/R;
alter tablespace XDB end backup;

alter tablespace USERS begin backup;
$xcopy F:oracleoradatamydbUSERS01.DBF e:backup/H/R;
alter tablespace USERS end backup;

alter tablespace UNDOTBS1 begin backup;
$xcopy F:oracleoradatamydbUNDOTBS01.DBF e:backup/H/R;
alter tablespace UNDOTBS1 end backup;

alter tablespace TOOLS begin backup;
$xcopy F:oracleoradatamydbTOOLS01.DBF e:backup/H/R;
alter tablespace TOOLS end backup;

alter tablespace TEST01 begin backup;
$xcopy F:oracleoradatamydbTEST01.DBF e:backup/H/R;
alter tablespace TEST01 end backup;

alter tablespace SYSTEM begin backup;
$xcopy F:oracleoradatamydbSYSTEM01.DBF e:backup/H/R;
alter tablespace SYSTEM end backup;

alter tablespace ODM begin backup;
$xcopy F:oracleoradatamydbODM01.DBF e:backup/H/R;
alter tablespace ODM end backup;

alter tablespace JFK_DATA01 begin backup;
$xcopy F:oracleoradatamydbJFK_DATA01.DBF e:backup/H/R;
alter tablespace JFK_DATA01 end backup;

alter tablespace INDX begin backup;
$xcopy F:oracleoradatamydbINDX01.DBF e:backup/H/R;
alter tablespace INDX end backup;

alter tablespace EXAMPLE begin backup;
$xcopy F:oracleoradatamydbEXAMPLE01.DBF e:backup/H/R;
alter tablespace EXAMPLE end backup;

alter tablespace DRSYS begin backup;
$xcopy F:oracleoradatamydbDRSYS01.DBF e:backup/H/R;
alter tablespace DRSYS end backup;

alter tablespace CWMLITE begin backup;
$xcopy F:oracleoradatamydbCWMLITE01.DBF e:backup/H/R;
alter tablespace CWMLITE end backup;
--end

--bak control file
--binary
alter database backup controlfile to 'e:backupcontrolbinbak.000';
--ascii
alter database backup controlfile to trace;

alter system archive log current;
--------------

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

相關文章