RMAM 备份与恢复

以下方法 将数据库复制到另外一台机上重建数据库。
生产环境 服务器server1 : 假设SQLDB数据库文件在 G:\SQLDB下,RMAN备份放在D:\SQLCP下
备用环境 服务器server2 : 数据库恢复到 D:\SQLDB下。(那么Server2下需建立好D:\SQLDB,D:\SQLCP两个目录)。

一 server1 数据库备份(server1上执行)

cmd
set oracle_sid=sqldb
rman cmdfile=backup.rcv.

文件 backup.rcv
-- backup.rcv ----
connect target / ;
run
{
  allocate channel c1 type disk format 'D:\SQLCP\%d_%T_%s.DBF' ; 
  backup tag 'full' database;
  release channel c1; 
}
run
{
  configure controlfile autobackup on;
  configure controlfile autobackup format for device type disk to 'D:\SQLCP\%F.CTL';
  allocate channel c1 type disk format 'D:\SQLCP\%d_%T_%s.ARC' ;
  backup archivelog all delete input;
  release channel c1;
}

run
{
  delete noprompt obsolete ;
}
-- backup.rcv end ----

结果:数据库备份,控制文件备份都放在 D:\SQLCP下。控制文件.CTL,数据文件.DBF,归档文件.ARC

二 创建恢复用的脚本(server1上执行)

define base_date=sysdate
define dest_path=D:\SQLDB
set pagesize 10000;
set linesize 200;
set heading off;
set verify off;
set feedback off;
spool restore.rcv;

prompt connect target / ;

select 'restore controlfile from ''' || max(handle)  || ''';'
  from v$backup_piece
 where handle like '%.CTL' ;
  
prompt alter database mount;;
prompt run { ; 

select 'set until sequence ' || to_char(sequence# + 1)  || ' thread ' || thread# || ';'
  from v$backup_redolog
  where next_change# = (select max(next_change#) from v$backup_redolog) ;

select 'set newname for datafile ' || file# || ' to ''&dest_path' ||
       substrb(name, instr(name,'\',-1)) || ''';'
  from v$datafile;

prompt restore database from tag 'FULL';;
prompt switch datafile all;;
prompt recover database;;
prompt } ;

spool off;

spool recover.rcv;

select 'alter database rename file ''' || member || '''' ||
       ' to ''&dest_path' || substrb(member, instr(member,'\',-1)) || ''';'
  from v$logfile;

prompt alter database open resetlogs;;

prompt alter tablespace temp  add tempfile '&dest_path\TEMP01.DBF' size 50M;;
prompt shutdown abort ;;
prompt startup ;;

prompt exit ;

spool off ;

exit

结果:生成两个文件 restore.rcv,留着在备用机上RMAN执行。recover.rcv,留着在备用机上SQLPLUS执行。

文件restore.rcv内容
--restore.rcv ----
connect target /
restore controlfile from 'D:\SQLCP\C-1832621359-20100408-0F.CTL';                                                                                                                                      
alter database mount;
run {
set until sequence 47 thread 1;                                                                                                                                                                        

set newname for datafile 1 to 'D:\SQLDB\SYSTEM01.DBF';                                                                                                                                                 
set newname for datafile 2 to 'D:\SQLDB\UNDOTBS01.DBF';                                                                                                                                                
set newname for datafile 3 to 'D:\SQLDB\INDX01.DBF';                                                                                                                                                   
set newname for datafile 4 to 'D:\SQLDB\TOOLS01.DBF';                                                                                                                                                  
set newname for datafile 5 to 'D:\SQLDB\USERS01.DBF';                                                                                                                                                  
set newname for datafile 6 to 'D:\SQLDB\SQLDATA.DBF';                                                                                                                                                  
restore database from tag 'FULL';
switch datafile all;
recover database;
}
--restore.rcv end ----


文件recover.rcv内容
--recover.rcv -----
alter database rename file 'G:\SQLDB\REDO01.LOG' to 'D:\SQLDB\REDO01.LOG';                                                                                                                             
alter database rename file 'G:\SQLDB\REDO02.LOG' to 'D:\SQLDB\REDO02.LOG';                                                                                                                             
alter database open resetlogs;
alter tablespace temp  add tempfile 'D:\SQLDB\TEMP01.DBF' size 50M;
shutdown abort ;
startup ;
exit
--recover.rcv end-----

三 server2恢复(server2上执行)
将server1的数据库备份文件,和两个rcv文件copy 到 D:\SQLCP

新建实例
cmd
oradim -new -intpwd manager -sid SQLDB -startmode auto
将数据库启动到nomount状态。
cmd
set oracle_sid=sqldb
sqlplus /nolog
CONNECT / AS sysdba ;
startup nomount ;
EXIT

开始恢复
cmd
D:
cd D:\SQLCP
set oracle_sid=sqldb
rman cmdfile = restore.rcv
sqlplus "/ as sysdba" @recover.rcv

恢复完成。


恢复之后,备份出来的controlfile已经被改变。不能用它进行再次恢复。

 

posted @ 2010-04-08 14:10  abenz  阅读(756)  评论(0编辑  收藏  举报