Oracle rman automatic recovery script (production data migration to test)

If the production environment migrates data to the test environment, it is often necessary to use rman backup for recovery.
There are commercial backup and recovery tools for advanced ones, and the production data can be restored to the test environment with a few clicks on the UI interface. The script below is the poor man’s version. After configuring the environment variables and rman backup storage location, run the script That's it.

#!/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`

To be improved

The script needs to be improved, but the time-consuming recovery part has been completed, and it can be improved later. For example, if the path of the online log is inconsistent with the production path, the path needs to be changed. After the change, alter database open resetlogs needs to be performed; some databases also need to be renamed
. , all of which can be implemented with automated scripts.

Guess you like

Origin blog.csdn.net/x6_9x/article/details/124175789