#!/bin/bash
#oracle 数据库恢复源客户端主机名
export client=dmzdb71
cd /usr/openv/netbackup/bin/
CONTROL=`./bplist -C ${client} -t 4 -R / |awk -F: 'NR==1 { print $(NF) }' `;
export CONTROL;
#恢复库Oracle环境变量
export ORACLE_SID=moni
export ORACLE_HOME=/u01/app/oracle/product/11gR2/db
source ~/.bash_profile
DBID=2579332618
#数据库文件恢复目标路径
datafile="/oradata/test/moni/"
#查询到的redo重定向语句文件
rename=/tmp/renameredo.sql
#脚本时间
date=`date +%Y%m%d_%T`
RECOVERLOG=/home/oracle/${date}_restore.log
touch ${RECOVERLOG}
#执行脚本开始时间
starttime=`date +%Y%m%d_%T`
echo "恢复脚本开始运行:`date +%Y%m%d_%T`";
echo "Oracle自动恢复开始:$starttime" >> $RECOVERLOG
#将数据库启动到nomount状态
sqlplus / as sysdba >> $RECOVERLOG <<EOF
shutdown immediate;
startup nomount force;
exit;
EOF
#进入到Oracle RMAN中,开始恢复指定Oracle数据库
rman target / >> $RECOVERLOG << EOF
set dbid ${DBID};
run {
allocate channel c1 type 'sbt_tape' parms='ENV=(NB_ORA_CLIENT=${client})';
restore controlfile from '${CONTROL}';
release channel c1;
}
##恢复数据文件
alter database mount;
crosscheck backupset;
run {
allocate channel c1 type 'sbt_tape' parms='ENV=(NB_ORA_CLIENT=${client})';
allocate channel c2 type 'sbt_tape' parms='ENV=(NB_ORA_CLIENT=${client})';
set newname for database to '${datafile}%b';
restore database;
switch datafile all;
recover database;
release channel c1;
release channel c2;
}
exit;
EOF
#判定源库恢复路径与目标端恢复路径是否一致
sqlplus / as sysdba >>$RECOVERLOG <<EOF
spool /tmp/redolist.log
set linesize 200 echo off heading off feedback off verify off
define y='${datafile}';
select 'alter database rename file '''||member||''' to '''||'&y'||substr(member, INSTR (member, '/', -1) +1 , length(member))||''';' from v\$logfile
where substr(member, 1,instr(member,'/',-1))!='&y';
spool off;
EOF
cat /tmp/redolist.log | awk '/^alter database rename file/' > $rename
#使用上面查到并过滤的redo rename语句重定向redo 日志文件
echo "exit;" >> $rename
sqlplus / as sysdba @${rename} >> $RECOVERLOG
sqlplus / as sysdba >> $RECOVERLOG <<EOF
recover database using backup controlfile until cancel;
AUTO
recover database using backup controlfile until cancel;
CANCEL
alter database open resetlogs;
select status from v\$instance;
exit;
EOF
#数据库自动恢复结束时间
endtime=`date +%Y%m%d_%T`
echo "恢复脚本运行结束时间为:`date +%Y%m%d_%T`";
#echo "恢复脚本运行结束时间为:`date +%Y%m%d_%T`"; >> /tmp/checkdatabase.log
echo "Oracle自动恢复结束:$endtime" >> $RECOVERLOG
##检查ORACLE数据库恢复完成后的状态
echo "检查ORACLE数据库恢复完成后的状态";
(sqlplus / as sysdba << EOF
select status from v\$instance;
EOF
) | grep OPEN > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "当前时间为:`date +%Y%m%d_%T`" >> /tmp/checkdatabase.log
echo "当前数据库实例为${ORACLE_SID},DBID=${DBID},的数据库恢复成功,且数据库当前状态为OPEN"
echo "当前数据库实例为${ORACLE_SID},DBID=${DBID},的数据库恢复成功,且数据库当前状态为OPEN" >> /tmp/checkdatabase.log
else
echo "当前时间为:`date +%Y%m%d_%T`" >> /tmp/checkdatabase.log
echo "当前数据库实例为${ORACLE_SID},DBID=${DBID},的数据库恢复失败,详情请查看${RECOVERLOG}日志文件"
echo "当前数据库实例为${ORACLE_SID},DBID=${DBID},的数据库恢复失败,详情请查看${RECOVERLOG}日志文件" >> /tmp/checkdatabase.log
fi
echo "Oracle数据库恢复记录详情请查看 /tmp/checkdatabase.log 日志"
#cat /tmp/checkdatabase.log
标签:tmp,log,DBID,备份,NBU,echo,RECOVERLOG,oracle,数据库
From: https://www.cnblogs.com/Gatsbysun/p/17330248.html