Oracle備份/恢復案例02——文件恢復
?
一、非歸檔模式下的備份與恢復
備份方案:采用OS冷備份
1、連接數據庫并創建測試表
?
SQL*Plus: Release 9.2.0.1.0 - Production on Tue Jan 13 10:03:27 2009
Copyright (c) 1982, 2002, Oracle Corporation.? All rights reserved.
?
SQL> connect sys/sys as sysdba;
Connected.
SQL> create table test(a int) tablespace users;
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
2、備份數據庫
SQL> @D:\test\coldbak.sql
3、再插入記錄
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
A
------------------------
1
2
4、關閉數據庫
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
5、毀壞一個或多個數據文件,如刪除user01.dbf
C:>del D:\ORACLE\ORADATA\DODO\USERS01.DBF
6、重新啟動數據庫,Oracle報錯
SQL> startup
ORACLE instance started.
Total System Global Area? 135338868 bytes
Fixed Size?????????????????? 453492 bytes
Variable Size???????????? 109051904 bytes
Database Buffers?????????? 25165824 bytes
Redo Buffers???????????????? 667648 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:\ORACLE\ORADATA\DODO\USERS01.DBF'
在報警文件中,會有更詳細的信息
Errors in file d:\oracle\admin\dodo\bdump\dodo_dbw0_3648.trc:
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:\ORACLE\ORADATA\DODO\USERS01.DBF'
ORA-27041: unable to open file
OSD-04002: 無法打開文件
O/S-Error: (OS 2) 系統找不到指定的文件。
7、拷貝備份復原到原來位置(restore過程)
C:>xcopy D:\DATABASE\BACK\USERS01.DBF D:\ORACLE\ORADATA\DODO/H/R/S
8、打開數據庫,檢查數據
SQL> alter database open;
Database altered.
SQL> select * from test;
A
------------------------
1
這里可以發現,數據庫恢復成功,但在備份之后與崩潰之前的數據丟失了。
?
說明:
1、非歸檔模式下的恢復方案可選性很小,一般情況下只能有一種恢復方式,就是數據庫的冷備份的完全恢復。僅僅需要拷貝原來的備份就可以(restore),不需要recover。
2、這種情況下的恢復,可以完全恢復到備份的點上,但是可能是丟失數據的,在備份之后與崩潰之前的數據將全部丟失。
3、不管毀壞了多少數據文件或是聯機日志或是控制文件,都可以通過這個辦法恢復,因為這個恢復過程是Restore所有的冷備份文件,而這個備份點上的所有文件是一致的,與最新的數據庫沒有關系,就好比把數據庫又放到了一個以前的“點”上。
4、對于非歸檔模式下,最好的辦法就是采用OS的冷備份,建議不要用RMAN來作冷備份,效果不好,因為RMAN不備份聯機日志,restore不能根本解決問題。
5、如果沒有備份聯機日志,如RMAN的備份,就需要利用不完全恢復(until cancel)的方法來重新創建聯機日志文件。
?
?
?
二、歸檔模式下丟失或損壞一個數據文件
?
OS備份方案
?
在歸檔方式下損壞或丟失一個數據文件,如果存在相應的備份與該備份以來的歸檔日志,恢復還是比較簡單的,可以作到盡量少的Down機時間,并能作到數據庫的完全恢復。
?
1、連接數據庫,創建測試表并插入記錄
SQL*Plus: Release 9.2.0.1.0 - Production on Tue Jan 13 10:03:27 2009
Copyright (c) 1982, 2002, Oracle Corporation.? All rights reserved.
?
SQL> connect sys/sys as sysdba;
Connected.
SQL> create table test(a int) tablespace users;
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
2、備份數據庫
SQL> @D:\test\hotbak.sql
3、繼續在測試表中插入記錄
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
A
---------------------------------------
1
2
SQL> alter system switch logfile;
System altered.
SQL> alter system switch logfile;
System altered.
4、關閉數據庫,模擬丟失數據文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down
C:>del D:\ORACLE\ORADATA\DODO\USERS01.DBF
5、啟動數據庫錯誤,脫機該數據文件
SQL> startup
ORACLE instance started.
Total System Global Area? 135338868 bytes
Fixed Size?????????????????? 453492 bytes
Variable Size???????????? 109051904 bytes
Database Buffers?????????? 25165824 bytes
Redo Buffers???????????????? 667648 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:\ORACLE\ORADATA\DODO\USERS01.DBF'
還可以查看報警文件(見上一個恢復案例)或動態視圖v$recover_file
idle> select * from v$recover_file;
?
???? FILE# ONLINE? ONLINE_ ERROR?????????????????? CHANGE# TIME
---------- ------- ------- -------------------- ---------- ----------
???????? 6 ONLINE? ONLINE? FILE NOT FOUND??????????????? 0
脫機數據文件:
SQL> alter database datafile 6 offline drop;
Database altered.
6、打開數據庫,拷貝備份回來(restore),恢復(recover)該數據文件,并聯機
?
打開數據庫:
SQL> alter database open;
Database altered.
拷貝備份從備份處
C:>xcopy D:\DATABASE\BACK\USERS01.DBF D:\ORACLE\ORADATA\DODO/H/R/S
恢復該數據文件
SQL> recover datafile 6;
Media recovery complete.
恢復成功,聯機該數據文件:
SQL> alter database datafile 6 online;
Database altered.
7、檢查數據庫的數據(完全恢復)
SQL> select * from test;
A
--------------------------
1
2
?
說明:
?
1、采用熱備份,需要運行在歸檔模式下,可以實現數據庫的完全恢復,也就是說,從備份后到數據庫崩潰時的數據都不會丟失。
2、可以采用全備份數據庫的方式備份,對于特殊情況,也可以只備份特定的數據文件,如只備份用戶表空間(一般情況下對于某些寫特別頻繁的數據文件,可以單獨加大備份頻率)
3、如果在恢復過程中,發現損壞的是多個數據文件,即可以采用一個一個數據文件的恢復方法(第5步中需要對數據文件一一脫機,第6步中需要對數據文件分別恢復),也可以采用整個數據庫的恢復方法。
4、如果是系統表空間的損壞,不能采用此方法
?
RMAN備份方案
?
RMAN也可以進行聯機備份,而且備份與恢復方法將比OS備份更簡單可靠。
?
1、連接數據庫,創建測試表并插入記錄
SQL*Plus: Release 9.2.0.1.0 - Production on Tue Jan 13 10:03:27 2009
Copyright (c) 1982, 2002, Oracle Corporation.? All rights reserved.
?
SQL> connect sys/sys as sysdba;
Connected.
SQL> create table test(a int) tablespace users;
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
2、備份數據庫表空間users
C:>rman
Recovery Manager: Release 9.2.0.1.0 - Production
Copyright (c) 1995, 2002, Oracle Corporation.? All rights reserved.
RMAN> connect rcvcat rman/rman
connected to recovery catalog database
RMAN> connect target sys/sys
connected to target database: DODO (DBID=472976704)
RMAN> run{
2> allocate channel c1 type disk;
3> backup tag 'tsuser' format 'D:\DATABASE\RMAN\tsuser_%u_%s_%p';
4> tablespace users;
5> release channel c1;
6> }
allocated channel: c1
channel c1: sid=18 devtype=DISK
?
Starting backup at 2009-01-13
channel c1: starting full datafile backupset
channel c1: specifying datafile(s) in backupset
input datafile fno=00006 name=D:\ORACLE\ORADATA\DODO\USERS01.DBF
channel c1: starting piece 1 at 2009-01-13
channel c1: finished piece 1 at 2009-01-13
piece handle=D:\DATABASE\RMAN\TSUSER_1NK4MUKK_55_1 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:35
Finished backup at 2009-01-13
?
Starting Control File and SPFILE Autobackup at 2009-01-13
piece handle=D:\ORACLE\ORADATA\DODO\RMANBACK\CTL_C-472976704-20090113-01 comment=NONE
Finished Control File and SPFILE Autobackup at 2009-01-13
?
released channel: c1
3、繼續在測試表中插入記錄
SQL> insert into test values(2);
1 row inserted
SQL> commit;
Commit complete
SQL> select * from test;
A
---------------------------------
1
2
SQL> alter system switch logfile;
System altered.
SQL> r
1* alter system switch logfile;
System altered.
4、關閉數據庫,模擬丟失數據文件
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down
C:>del D:\ORACLE\ORADATA\DODO\USERS01.DBF
5、啟動數據庫,檢查錯誤
SQL> startup
ORACLE instance started.
?
Total System Global Area? 135338868 bytes
Fixed Size?????????????????? 453492 bytes
Variable Size???????????? 109051904 bytes
Database Buffers?????????? 25165824 bytes
Redo Buffers???????????????? 667648 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: 'D:\ORACLE\ORADATA\DODO\USERS01.DBF'
6、先打開數據庫
SQL> alter database datafile 6 offline drop;
Database altered.
SQL> alter database open;
Database altered.
7、恢復該表空間
恢復腳本可以是恢復單個數據文件
run{
allocate channel c1 type disk;
restore datafile 3;
recover datafile 3;
sql 'alter database datafile 3 online';
release channel c1;
}
也可以是,恢復表空間
run{
allocate channel c1 type disk;
restore tablespace users;
recover tablespace users;
sql 'alter database datafile 3 online';
release channel c1;
}
過程如下:
C:>rman
Recovery Manager: Release 9.2.0.1.0 - Production
Copyright (c) 1995, 2002, Oracle Corporation.? All rights reserved.
RMAN> connect rcvcat rman/rman
connected to recovery catalog database
RMAN> connect target sys/sys
connected to target database: DODO (DBID=472976704)
RMAN> run{
2> allocate channel c1 type disk;
3> restore datafile 6;
4> recover datafile 6;
5> sql 'alter database datafile 6 online';
6> release channel c1;
7> }
allocated channel: c1
channel c1: sid=18 devtype=DISK
?
Starting restore at 2009-01-13
?
channel c1: starting datafile backupset restore
channel c1: specifying datafile(s) to restore from backup set
restoring datafile 00006 to D:\ORACLE\ORADATA\DODO\USERS01.DBF
channel c1: restored backup piece 1
piece handle=D:\DATABASE\RMAN\TSUSER_1NK4MUKK_55_1 tag=TSUSER params=NULL
channel c1: restore complete
Finished restore at 2009-01-13
?
Starting recover at 2009-01-13
?
starting media recovery
media recovery complete
?
Finished recover at 2009-01-13
?
sql statement: alter database datafile 6 online
?
released channel: c1
8、檢查數據是否完整
SQL> select * from test;
A
--------------------------------
1
2
?
說明:
?
1、RMAN也可以實現單個表空間或數據文件的恢復,恢復過程可以在mount下或open方式下,如果在open方式下恢復,可以減少down機時間
2、如果損壞的是一個數據文件,建議offline并在open方式下恢復
3、這里可以看到,RMAN進行數據文件與表空間恢復的時候,代碼都比較簡單,而且能保證備份與恢復的可靠性,所以建議采用RMAN的備份與恢復
?
?