Oracle rman自动恢复脚本(生产数据向测试迁移)

生产环境向测试环境迁移数据的话,往往需要用到rman备份进行恢复。
高级点的有商业化的备份恢复工具,在UI界面上点几下就把生产的数据恢复到测试环境了,下面的脚本是穷人版的 ,配置好环境变量及rman备份存放位置,运行下脚本就可以了。

#!/bin/bash
##############################################
#NAME: rman_auto_restore.sh
#DESC:restore oracle db from prod  environment to test environment very quickly.
#Note: Linux USER - execute as oracle instance user
#Use: nohup ./rman_auto_restore.sh > ./rman_auto_restore.sh.log &
#History:
#       v1.0 2022-03-08 yangshixian
##############################################

. /home/${
    
    USER}/.bash_profile
#
#define Oracle env 配置EBS环境变量
#

export ORACLE_SID=XXXPROD #针对linux的配置文件中实例名大小写敏感
export ORACLE_HOME=/u01/app/xxxx #目标环境ORACLE_HOME路径
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export PERL5LIB=$ORACLE_HOME/perl/lib/5.10.0:$ORACLE_HOME/perl/lib/uate_perl/5.10.0:$ORACLE_HOME/appsutil/perl
export PATH=$ORACLE_HOME/perl/bin:$ORACLE_HOME/bin:$PATH


#
#rman env conf 配置rman环境变量#
#

RMAN_BAK_PATH=/data/ebs_rman_data/ #rman备份文件存放绝对路径
RMAN_CONTROLFILE=`ls $RMAN_BAK_PATH | grep C0_ |head -1` #控制文件绝对路径
TARGET_DATA_FILE_PATH=/data/ebs/uat/db/apps_st/data #目标环境数据文件位置

#
#unmount:execute rman restore scripts
#
echo "restore controlfile begin ..."
rman target / <<EOF
run
{
startup nomount;
RESTORE CONTROLFILE FROM '$RMAN_BAK_PATH$RMAN_CONTROLFILE';
alter database mount;
catalog start with '$RMAN_BAK_PATH' noprompt;
}
exit
EOF

echo "restore controlfile end Status $?"

#
#mount:execute rman restore scripts
#
echo "gen restore rman_scripts begin ...."

sqlplus -s / as sysdba <<EOF
set heading off feed off verify off echo off pages 0 trimspool on
set lines 132 pagesize 0
spo restore.rman
select 'run{' from dual;
select
'set newname for datafile ' || file# || ' to '|| '''' || '$TARGET_DATA_FILE_PATH' || substr(name,INSTR(name,'/',-1,1)) || '''' || ';'
from v\$datafile;
select
'restore database;' || chr(10) ||
'switch datafile all;' || chr(10) ||
'recover database until scn '|| controlfile_change# || ';' || chr(10) ||
'}'
from  v\$database;
spo off
exit;
EOF

echo "gen restore rman_scripts end status $?"

echo "begin rman restore ....."
echo `date`
rman target / log rman_auto_restore_`date -I`.log @restore.rman
echo "begin rman restore over! status: $?"
echo `date`

待改进

脚本待改进地方,只是完成了耗时的恢复部分,后面还可以改进,例如在线日志的路径如果和生产不一致的话需要改路径,改完后还需要进行 alter database open resetlogs;
有的还需要数据库改名,这些都可以用自动脚本来实现。

猜你喜欢

转载自blog.csdn.net/x6_9x/article/details/124175789