经常做恢复验证,每次都有些小不同,想省点事,搞个一键还原可好?适用于不同实例,源端和目标端最好版本一致,平台一致,开始吧。
步骤介绍:
#适用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次输入密码
然后就是喝茶时间啦。
阅读(10272) | 评论(0) | 转发(1) |