RMAN恢復(fù)實踐
2024-07-21 02:35:03
供稿:網(wǎng)友
要害詞:Oracle、恢復(fù)、RMAN
談恢復(fù)肯定離不開備份,不過今天我們的重點不在于此,我在在這里將備份分為兩類:操作系統(tǒng)備份和數(shù)據(jù)庫備份,而數(shù)據(jù)庫備份工作我們大部分都是用RMAN 來做。對于使用RMAN 我強烈建議使用catalog mode。
測試環(huán)境:WIN2K ADV+ORACLE817
RMAN:target database named ORA,catalog database named RCVDB
一、Control file的恢復(fù)
說明:RMAN 的備份必須在catalog mode下進行,否則備份了control file也沒有用;但即使是這樣有時候可能還會出現(xiàn)很多問題。建議:control file 用SQL或操作系統(tǒng)的方式做備份和恢復(fù)。
1、RMAN備份的恢復(fù)
備份:
run {
allocate channel c1 type disk;
backup current controlfile;
}
恢復(fù):
run {
allocate channel c1 type disk;
restore controlfile to '/oracle/oradata/ora/control01.ctl';
replicate controlfile from '/oracle/oradata/ora/control01.ctl';
restore database;
sql 'alter database mount';
recover database until cancel;
sql 'alter database open resetlogs';
release channel c1;
}
使用resetlogs之后需在catalog database 上進行reset database,原有備份信息將不可用,所以要及時進行新的完全備份。
2、SQL備份的恢復(fù)
備份:
alter database backup controlfile to trace;
恢復(fù):
先將數(shù)據(jù)庫shutdown,然后從備份所產(chǎn)生的trace文件中拷出創(chuàng)建恢復(fù)所用的SQL執(zhí)行一遍
即可。假如你之前沒有做這樣的備份那也沒關(guān)系,形式如下你可以照著寫一個:
#--------------------------------BEGIN-----------------------------------------
# The following commands will create a new control file and use it
# to open the database.
# Data used by the recovery manager will be lost. Additional logs may
# be required for media recovery of offline data files. Use this
# only if the current version of all online logs are available.
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "ORA" NORESETLOGS
NOARCHIVELOG
MAXLOGFILES 32
MAXLOGMEMBERS 2
MAXDATAFILES 32
MAXINSTANCES 16
MAXLOGHISTORY 680
LOGFILE
GROUP 1 (
'C:/ORACLE/ORADATA/ORA/REDO01.LOG',
'C:/ORACLE/ORADATA/ORA/REDO01_1.LOG'
) SIZE 1M,
GROUP 2 (
'C:/ORACLE/ORADATA/ORA/REDO02.LOG',
'C:/ORACLE/ORADATA/ORA/REDO02_1.LOG'
) SIZE 1M,
GROUP 3 (
'C:/ORACLE/ORADATA/ORA/REDO03_1.LOG',
'C:/ORACLE/ORADATA/ORA/REDO03_2.LOG'
) SIZE 1M
DATAFILE
'C:/ORACLE/ORADATA/ORA/SYSTEM01.DBF',
'C:/ORACLE/ORADATA/ORA/RBS01.DBF',
'C:/ORACLE/ORADATA/ORA/USERS01.DBF',
'C:/ORACLE/ORADATA/ORA/TEMP01.DBF',
'C:/ORACLE/ORADATA/ORA/TOOLS01.DBF',
'C:/ORACLE/ORADATA/ORA/INDX01.DBF'
CHARACTER SET ZHS16GBK
;
# Recovery is required if any of the datafiles are restored backups,
# or if the last shutdown was not normal or immediate.
RECOVER DATABASE
# Database can now be opened normally.
ALTER DATABASE OPEN;
#----------------------------------END-----------------------------------------
3、OS 備份的恢復(fù)
備份:
OS copy
恢復(fù):
OS copy回來即可,要注重的是這個備份必須是最新有效的。
二、Database的恢復(fù)
A、To restore the database from host_a to host_b with the same file system
#此處使用recovery catalog;
#假如不是catalog mode,就用OS COPY直接將control file
#copy過來并mount database。
1、copy the init.ora file from host_a to host_b using o/s utility
2、rman target sys/sys@host_b catalog rman/rman@rcat
3、startup nomount
4、run {
allocate channel c1 type disk;
restore controlfile;
sql ‘a(chǎn)lter database mount’;
}
5、select min(scn) from (
select max(next_change#) scn from v$archived_log
group by thread#);
6、run {
set until scn=500; #use apPRopriate SCN for incomplete recovery
allocate channel c1 type ‘sBT_type’;
restore database;
recover database;
sql ‘a(chǎn)lter database open resetlogs’;
}
B、To restore the database from host_a to host_b with a different filesystem
步驟和A差不多,不同的是在啟動數(shù)據(jù)庫之前要修改init.ora文件中所有和路徑相關(guān)的參數(shù),
如:*_DEST,*_PATH 等。然后執(zhí)行如下腳本:
run {
set until scn=500;
allocate channel c1 type disk;
set newname for datafile 1 to ‘/disk’/%U’;
set newname for datafile 2 to ‘/disk’/%U’;
set newname for datafile 3 to ‘/disk’/%U’;
set newname for datafile 4 to ‘/disk’/%U’;
set newname for datafile 5 to ‘/disk’/%U’;
set newname for datafile 6 to ‘/disk’/%U’;
set newname for datafile 7 to ‘/disk’/%U’;
sql ‘a(chǎn)lter database mount’;
restore database;
switch datafile all; #points the control file to the renamed datafiles
recover database;
.... ....
sql ‘a(chǎn)lter database open resetlogs’;
}
三、Tablespace and datafile的恢復(fù)
run {
allocate channel c1 type disk;
sql ‘a(chǎn)lter tablespace users offline immediate’; #must be in archive log mode
#如下改變datafile位置
set newname for datafile 'c:/oracle/oradata/ora/users01.dbf'
to 'c:/oracle/oradata/orabk/user01.dbf';
restore tablespace users;
switch datafile all;
recover tablespace users;
sql ‘a(chǎn)lter tablespace users online’;
}
假如不在archive log mode下,執(zhí)行以上SQL時會出錯:ORA-01145。你將不得不shutdown。
四、關(guān)于set until 選項
set until后面可以跟time、scn和logseq三個不同的選項,當數(shù)據(jù)庫運行在noarchivelog
mode 下時可以使用until cancel選項進行數(shù)據(jù)庫的不完全恢復(fù)。所有這些的具體過程和以上
的數(shù)據(jù)庫恢復(fù)都差不多,不再贅述。