分类: Oracle
2008-10-12 13:06:00
谈恢复肯定离不开备份,不过今天我们的 重点不在于此,我在在这里将备份分为两类:备份和数据库备份,而数据库备份工作我们大部分都是用RMAN 来做。对于使用RMAN 我强烈建议使用catalog mode。
测试环境:WIN2K ADV+ORACLE817
RMAN:target database named ORA,catalog database named RCVDB
一、Control file的恢复
说明:RMAN 的备份必须在catalog mode下进行,否则备份了control file也没有用;但即使是这样有时候可能还会出现很多问题。建议:control file 用SQL或操作系统的方式做备份和恢复。
1、RMAN备份的恢复
备份:
run {
allocate channel c1 type disk;
backup current controlfile;
}
恢复:
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备份的恢复
备份:
alter database backup controlfile to trace;
恢复:
先将数据库shutdown,然后从备份所产生的trace文件中拷出创建恢复所用的SQL执行一遍
即可。如果你之前没有做这样的备份那也没关系,形式如下你可以照着写一个:
#--------------------------------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 备份的恢复
备份:
OS copy
恢复:
OS copy回来即可,要注意的是这个备份必须是最新有效的。
二、Database的恢复
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 catalog
3、startup nomount
4、run {
allocate channel c1 type disk;
restore controlfile;
sql ‘alter 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 ‘alter database open resetlogs’;
}
B、To restore the database from host_a to host_b with a different filesystem
步骤和A差不多,不同的是在启动数据库之前要修改init.ora文件中所有和路径相关的参数,
如:*_DEST,*_PATH 等。然后执行如下脚本:
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 ‘alter database mount’;
restore database;
switch datafile all; #points the control file to the renamed datafiles
recover database;
.... ....
sql ‘alter database open resetlogs’;
}
三、Tablespace and datafile的恢复
run {
allocate channel c1 type disk;
sql ‘alter 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 ‘alter tablespace users online’;
}
如果不在archive log mode下,执行以上SQL时会出错:ORA-01145。你将不得不shutdown。
四、关于set until 选项
set until后面可以跟time、scn和logseq三个不同的选项,当数据库运行在noarchivelog
mode 下时可以使用until cancel选项进行数据库的不完全恢复。所有这些的具体过程和以上
的数据库恢复都差不多,不再赘述。