http://www.cnoug.org/viewthread.php?tid=22641

解决方案 »

  1.   

    这篇文章 也是去年写的,最初发表在原来的个人网站上,收录在旧版文集中,可能很多没有看到,总有人写信要,今天乘改版之际就把它单独拿出来了。(gototop 2003.6.24注)无论是看书还是在网上看别人写的关于数据库备份与恢复的文章,大多数都是讲如何进行备份,而真正考验DBA的恢复却很少有人写,不知道是为什么。我们都常说备份是管理,而恢复才是技术,今天就让我们来共同探讨一下Oracle 数据库的恢复技术。如果各位网友有兴趣可以和我联系。关键词:Oracle、恢复、RMAN 谈恢复肯定离不开备份,不过今天我们的重点不在于此,我在在这里将备份分为两类:操作系统备份和数据库备份,而数据库备份工作我们大部分都是用RMAN 来做。对于使用RMAN 我强烈建议使用catalog mode。测试环境:WIN2K ADV+ORACLE817RMAN: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 NOMOUNTCREATE CONTROLFILE REUSE DATABASE "ORA" NORESETLOGSNOARCHIVELOGMAXLOGFILES 32MAXLOGMEMBERS 2MAXDATAFILES 32MAXINSTANCES 16MAXLOGHISTORY 680LOGFILEGROUP 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 1MDATAFILE'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 utility2、rman target sys/sys@host_b catalog rman/rman@rcat3、startup nomount4、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_loggroup by thread#);6、run {set until scn=500; #use appropriate SCN for incomplete recoveryallocate 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 datafilesrecover 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三个不同的选项,当数据库运行在noarchivelogmode 下时可以使用until cancel选项进行数据库的不完全恢复。所有这些的具体过程和以上的数据库恢复都差不多,不再赘述。