经常做恢复验证,每次都有些小不同,想省点事,搞个一键还原可好?适用于不同实例,源端和目标端最好版本一致,平台一致,开始吧。
步骤介绍:
#适用oracle 11g单机环境
#备份在192.168.1.1的RESTORDIR目录下,采用rman备份的多个备份集文件,含参数文件(.par)、控制文件(c-xxx)、全库备份、归档备份
#先清理本地的验证库环境,传备份片过来
#恢复pfile文件后清理参数
#恢复控制文件,注册备份信息
#恢复数据库,调整重做日志
#去掉归档模式
#/bin/bash
# restore database from rman
# please first modify ORACLE_SID and RESTORDIR:
. ~/.bash_profile
RESTORDIR=/oradata/ORCL/20190131
ORACLE_SID=ORCL
export ORACLE_SID=$ORACLE_SID
DBNAME=$ORACLE_SID
dbca -silent -deleteDatabase -sourceDB $DBNAME --先删库,再跑路:-)
RMANDIR=/oradata/rmanbak/$DBNAME
ssh oracle@192.168.1.1 ln -s $RESTORDIR $RMANDIR
BAKDIR=/oradata/$DBNAME/bak
DATADIR=/oradata/$DBNAME/db
rm -fr $BAKDIR/*
rm -fr $DATADIR/*
rm -fr $ORACLE_HOME/dbs/*$DBNAME*
mkdir -p $BAKDIR
mkdir -p $DATADIR
scp 192.168.1.1:$RMANDIR/* $BAKDIR
CMDFILE=/tmp/rman1.txt
PARFILE=`ls $BAKDIR/*.par`
(cat << EOF
startup nomount
restore spfile to pfile '$DATADIR/init.ora' from '$PARFILE';
exit
EOF
)>$CMDFILE
rman target / cmdfile=$CMDFILE
grep -v ".__" $DATADIR/init.ora|grep -v cluster_database|grep -v "_listener"|grep -v "*.log_archive"|grep -v pga_aggregate_target|grep -v sga_max_size|grep -v sga_target|grep -v thread|grep -v control_files >/tmp/init2.ora
grep -v db_cache_size /tmp/init2.ora|grep -v db_create_file_dest|grep -v "*.fal_"|grep -v instance_number|grep -v shared_pool_size>$DATADIR/init.ora
echo "*.control_files=$DATADIR/control01.dbf" >>$DATADIR/init.ora
echo "*.sga_max_size=4G" >> $DATADIR/init.ora
echo "*.sga_target=4G" >> $DATADIR/init.ora
mkdir -p `grep audit_file_dest $DATADIR/init.ora |awk -F "'" '{print $2}'`
CMDFILE=/tmp/rman2.txt
CTLFILE=`ls $BAKDIR/c-*`
(cat << EOF
startup nomount pfile='$DATADIR/init.ora' force
restore controlfile from '$CTLFILE';
alter database mount;
catalog start with '$BAKDIR/' noprompt;
crosscheck backupset;
delete noprompt expired backupset;
exit
EOF
)>$CMDFILE
rman target / cmdfile=$CMDFILE
RMANLOG=/tmp/rman_log_$DBNAME.log
CMDFILE=/tmp/rman_$DBNAME.txt
(cat << EOF
run
{
allocate channel c1 device type disk;
allocate channel c2 device type disk;
set newname for database to '$DATADIR/%b';
restore database;
switch datafile all;
switch tempfile all;
recover database;
}
exit
EOF
)>$CMDFILE
time nohup rman target / cmdfile=$CMDFILE log=$RMANLOG &
(cat << EOF
set pagesize 9999 lin 250 echo off heading off verify off feedback off trims on
spool /tmp/rename_log.sql
select 'ALTER DATABASE RENAME FILE '''||member||''' to '||''''||'$DATADIR'
||substr(member,instr(member,'/',-1)+1)||''';' cc from v$logfile;
spool off
@/tmp/rename_log.sql
spool /tmp/clear_log.sql
select 'alter database clear logfile group '||group#||';' cc from v$log;
spool off
@/tmp/clear_log.sql
alter database open resetlogs;
--alter database disable thread 2;
--alter database drop logfile group 6,7,8,9,10;
create spfile from pfile='$DATADIR/init.ora';
shutdown immediate
startup mount
alter database noarchivelog;
alter database open;
exit
)>$CMDFILE
sqlplus / as sysdba @$CMDFILE
echo 'game over!'
将上述代码保存为r.sh,放到目标端执行!
基本上修改2处即可:要恢复的实例名和备份文件位置(备份服务器IP地址通常是固定的),
如果没有配置信任关系执行时需要2次输入密码
然后就是喝茶时间啦。
阅读(1782) | 评论(0) | 转发(0) |