此恢复测试脚本在redhat6.5环境,oracle11g可以直接运行:
次恢复测试脚本省略了恢复pfile文件的步骤,pfile由于恢复至做一次,以后的恢复测试不需要再恢复pfile,因此不需要脚本化。
SID=prdtestdb
export ORACLE_SID=$SIDlogfile=shrman_$SID.log
#设置rman备份文件目录,和要恢复的数据文件的目录
SrcDbBackup=/dbfile2/backupprdtestdbDstDbRecover=/dbfile1/database/prdtestdb
#获取rman本分目录内的control文件名,有可能有多个,此处取第一个
ControlFileName=`ls *.ctb|head -n 1`
#恢复rman文件
cmdRestoreControl="restore controlfile to '$DstDbRecover/control1.ctl' from '$SrcDbBackup/$ControlFileName';";echo "restore control cmd is:">$logfile
echo $cmdRestoreControl >>$logfile
echo "startup nomount
exit" > temp.sql
echo "
startup nomount:">>$logfile
sqlplus -S / as sysdba @temp.sql >>$logfile 2>&1
echo "connect target /;
RUN {
allocate channel d1 type disk ;
$cmdRestoreControl
release channel d1;
}
exit" > controlrestore.rman
nohup rman cmdfile controlrestore.rman msglog temp.log >>$logfile 2>&1
cat temp.log >>$logfile
echo "restore control file success" >>$logfile
echo "alter database mount;
exit" > temp.sql
echo "
alter database mount:">>$logfile
sqlplus -S / as sysdba @temp.sql >>$logfile 2>&1
echo "connect target /;
RUN {
allocate channel d1 type disk ;
catalog start with '$SrcDbBackup';
release channel d1;
}
exit" > catalog.rman
nohup rman cmdfile catalog.rman msglog temp.log >>$logfile 2>&1
cat temp.log >>$logfile
#此处为获取所有数据文件名称,并获取将数据文件名称重命名rman脚本。(恢复库和原库的数据文件位置或文件名不一定相同,所以需要重命名)
echo 'set wrap off SQLBLANKLINES ON;set verify off pagesize 0 echo off
set feedback off linesize 10000 long 5000 trimout on;
set trimspool on arraysize 20
set trims on space 0 newpage none numwidth 12 serveroutput on
set wrap off SQLBLANKLINES ON
set term off
spool restore'$SID'.rman
select '"'connect target /;'"'||chr(10)||
'"'RUN {'"'||chr(10)||
'"'set command id to '"'||chr(39)||'"'123'"'||chr(39)||'"';'"'||chr(10)
||'"'allocate channel d1 type disk ;'"'||chr(10)
||'"'allocate channel d2 type disk ;'"'||chr(10)
||'"'allocate channel d3 type disk ;'"'||chr(10)
||'"'allocate channel d4 type disk ;'"'||chr(10) from dual;
select '"'SET NEWNAME FOR DATAFILE '"'||chr(39)||name||chr(39)||'"' to '"'||chr(39)||'"'$DstDbRecover/'"'||substr(name,instr(name, '"'/'"', -1)+1, length(name)-instr(name, '"'/'"', -1))||chr(39)||'"';'"' from V$datafile;
select '"'restore database;'"'||chr(10)
||'"'SWITCH DATAFILE ALL;'"'||chr(10)
||'"'release channel d1;'"'||chr(10)
||'"'release channel d2;'"'||chr(10)
||'"'release channel d3;'"'||chr(10)
||'"'release channel d4;'"'||chr(10)
||'"'}'"'||chr(10)
||'"'exit'"'||chr(10) from dual;
spool off
exit' > temp.sql
sqlplus -S / as sysdba @temp.sql >>$logfile 2>&1
echo 'restore database begin...'>>$logfile
nohup rman cmdfile restore$SID.rman msglog restore$SID.log >>$logfile 2>&1
echo "connect target /;
RUN {
allocate channel d1 type disk ;
recover database;
release channel d1;
}
exit" > recover$SID.rman
echo 'recover database begin...'>>$logfile
nohup rman cmdfile recover$SID.rman msglog recover$SID.log >>$logfile 2>&1
#此处为重命名logfile
echo 'set wrap off SQLBLANKLINES ON;
set verify off pagesize 0 echo off
set feedback off linesize 10000 long 5000 trimout on;
set trimspool on arraysize 20
set trims on space 0 newpage none numwidth 12 serveroutput on
set wrap off SQLBLANKLINES ON
set term off
spool rename.sql
select '"'ALTER DATABASE RENAME FILE '"'||chr(39)||member||chr(39)||'"' to '"'||chr(39)||'"'$DstDbRecover/'"'||substr(member,instr(member, '"'/'"', -1)+1, length(member)-instr(member, '"'/'"', -1))||chr(39)||'"';'"' aaa from V$logfile where type='"'ONLINE'"';
select '"'alter database drop logfile group '"'||group#||'"';'"' aaa from V$logfile where type='"'STANDBY'"';
select '"'exit'"' from dual;
spool off
exit' > temp.sql
echo 'rename logfile begin ...'>>$logfile
sqlplus -S / as sysdba @temp.sql >>$logfile 2>&1
echo 'drop standby logfile begin ...'>>$logfile
sqlplus -S / as sysdba @rename.sql >>$logfile 2>&1
echo "connect target /;
RUN {
allocate channel d1 type disk ;
ALTER DATABASE OPEN RESETLOGS;
release channel d1;
}
exit" > Open$SID.rman
echo 'open db resetlogs begin ...'>>$logfile
nohup rman cmdfile Open$SID.rman msglog Open$SID.log >>$logfile 2>&1