Oracle備份與恢復案例二
2024-08-29 13:38:21
供稿:網友
7、 檢查數據庫的數據(完全恢復)
SQL> select * from test;
A
--------------------------------
1
2
說明:
1、采用熱備份,需要運行在歸檔模式下,可以實現數據庫的完全恢復,也就是說,從備份后到數據庫崩潰時的數據都不會丟失;
2、可以采用全備份數據庫的方式備份,對于非凡情況,也可以只備份特定的數據文件,如只備份用戶表空間(一般情況下對于某些寫非凡頻繁的數據文件,可以單獨加大備份頻率);
3、假如在恢復過程中,發現損壞的是多個數據文件,即可以采用一個一個數據文件的恢復方法(第5步中需要對數據文件一一脫機,第6步中需要對數據文件分別恢復),也可以采用整個數據庫的恢復方法;
4、假如是系統表空間的損壞,不能采用此方法。
4.2.2 RMAN備份方案
RMAN也可以進行聯機備份,而且備份與恢復方法將比OS備份更簡單可靠。
1、連接數據庫,創建測試表并插入記錄
SQL> connect internal/passWord 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 8.1.6.0.0 - PRodUCtion
RMAN> connect rcvcat rman/rman@back
RMAN-06008: connected to recovery catalog database
RMAN> connect target internal/virpure
RMAN-06005: connected to target database: TEST (DBID=1788174720)
RMAN> run{
2> allocate channel c1 type disk;
3> backup tag 'tsuser' format 'd:/backup/tsuser_%u_%s_%p'
4> tablespace users;
5> release channel c1;
6> }
RMAN-03022: compiling command: allocate
RMAN-03023: executing command: allocate
RMAN-08030: allocated channel: c1
RMAN-08500: channel c1: sid=16 devtype=DISK
RMAN-03022: compiling command: backup
RMAN-03025: performing implicit partial resync of recovery catalog
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03023: executing command: backup
RMAN-08008: channel c1: starting full datafile backupset
RMAN-08502: set_count=5 set_stamp=494177612 creation_time=16-MAY-03
RMAN-08010: channel c1: specifying datafile(s) in backupset
RMAN-08522: input datafile fno=00003 name=D:/Oracle/ORADATA/TEST/USER01.DBF
RMAN-08013: channel c1: piece 1 created
RMAN-08503: piece handle=D:/BACKUP/TSUSER_05EN93AC_5_1 comment=NONE
RMAN-08525: backup set complete, elapsed time: 00:00:01
RMAN-03023: executing command: partial resync
RMAN-08003: starting partial resync of recovery catalog
RMAN-08005: partial resync complete
RMAN-03022: compiling command: release
RMAN-03023: executing command: release
RMAN-08031: released channel: c1
RMAN>
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/TEST/USER01.DBF
5、 啟動數據庫,檢查錯誤
SQL> startup
Oracle instance started.
Total System Global Area 102020364 bytes
Fixed Size 70924 bytes
Variable Size 85487616 bytes
Database Buffers 16384000 bytes
Redo Buffers 77824 bytes
Database mounted.
ORA-01157: cannot identify/lock data file 3 - see DBWR trace file
ORA-01110: data file 3: 'D:/Oracle/ORADATA/TEST/USER01.DBF'
6、 先打開數據庫
SQL> alter database datafile 3 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 8.1.6.0.0 - Production
RMAN> connect rcvcat rman/rman@back
RMAN-06008: connected to recovery catalog database
RMAN> connect target internal/virpure
RMAN-06005: connected to target database: TEST (DBID=1788174720)
RMAN> run{
2> allocate channel c1 type disk;
3> restore datafile 3;
4> recover datafile 3;
5> sql 'alter database datafile 3 online';
6> release channel c1;
7> }
//輸出內容冗長,省略--編者
RMAN>
8、 檢查數據是否完整
SQL> alter database open;
Database altered.
SQL> select * from test;
A
---------------------------------------
1
2
說明:
1、RMAN也可以實現單個表空間或數據文件的恢復,恢復過程可以在mount下或open方式下,假如在open方式下恢復,可以減少down機時間;
2、假如損壞的是一個數據文件,建議offline并在open方式下恢復;
3、這里可以看到,RMAN進行數據文件與表空間恢復的時候,代碼都比較簡單,而且能保證備份與恢復的可靠性,所以建議采用RMAN的備份與恢復.
4.3丟失多個數據文件,實現整個數據庫的恢復.
4.3.1 OS備份方案
OS備份歸檔模式下損壞(丟失)多個數據文件,進行整個數據庫的恢復
1、 連接數據庫,創建測試表并插入記錄
SQL> connect internal/password as sysdba;
Connected.
SQL> create table test(a int);
Table created
SQL> insert into test values(1);
1 row inserted
SQL> commit;
Commit complete
2、 備份數據庫,備份除臨時數據文件后的所數據文件
SQL> @hotbak.sql 或在DOS下 svrmgrl @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、 關閉數據庫,模擬丟失數據文件
<