oracledba.help
RMAN

RMAN Scripts

Overview

The following are the essential RMAN standard backup scripts. Change as required for your environment.

Prerequisites

  1. Set how long RMAN backup records are stored in the control file via the CFRKT.
    Example: SQLPlUS> ALTER SYSTEM SET CONTROL_FILE_RECORD_KEEP_TIME=45 SCOPE=BOTH;
  2. Set OS privileges on RMAN directory.
mkdir -p /u02/rman/MyDB
chown -R oracle:oinstall /u02/rman
chmod -R 775 /u02/rman
  1. Set then run the configuration script for your environment.

Usage

After you use the Configuration script, the remainder are commonly run as so:

  • Maintenance script before the full\incr backup script.
  • Full backup weekly.
  • Incremental on all other days.
  • Validate at least monthly.

Scripts

Usage Examples:

 OS> $ORACLE_HOME/bin/rman target sys/MyPassword nocatalog @ScriptName.rmn
 RMAN> @ScriptName.rmn

SNAPSHOT CONTROLFILE cannot be on an NFS mounted disk.

Configuration

  1. Set you ORACLE_SID first before configuring if you have multiple DBs on system.
  2. Reset the configuration: SQL> execute dbms_backup_restore.resetConfig;
  3. Change the below configuration script for your environment and run.
# rman.cfg.rmn
CONFIGURE COMPRESSION ALGORITHM 'MEDIUM';
CONFIGURE CONTROLFILE AUTOBACKUP on;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE disk TO '/u02/rman/MyDB/%F';
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 14 DAYS;
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/u02/rman/MyDB/snapshot.ctl';

CONFIGURE DEVICE TYPE disk BACKUP TYPE TO compressed backupset PARALLELISM 2;
CONFIGURE CHANNEL 1 DEVICE TYPE disk FORMAT '/u02/rman/MyDB/%U.bkp' MAXPIECESIZE 32g;
CONFIGURE CHANNEL 2 DEVICE TYPE disk FORMAT '/u02/rman/MyDB/%U.bkp' MAXPIECESIZE 32g;

show all;
  • If you have the Enterprise version you can use multiple channels and PARALLELISM. These need to be set together, i.e. PARALLELISM <number> matches the number of CHANNEL entries.
  • Though the other scripts are not usually changed its useful to have a separate configuration script for each database\instance. Examples:
    • rman.cfg.DB1.rmn
    • rman.cfg.DB2.rmn

Maintenance

# rman.maint.rmn
allocate channel for maintenance type disk;
crosscheck backup;
crosscheck archivelog all;
delete force noprompt obsolete;

Full Backup

# rman.backup_full.rmn
run { 
   backup 
   incremental level 0
   database plus archivelog delete all input;
}

Incremental Backup

# rman.backup_incr.rmn
run { 
   backup 
   incremental level 1
   database plus archivelog delete all input;
}

Validate

# rman.validate.rmn
run {
  restore database validate;
}

BASH RMAN Parent Script

The following script can be used from cron to run these RMAN backup scripts routinely. Example cron entries below:

00 17 * * * /u01/app/scripts/rmanctl.sh DB1 > /u01/app/scripts/out/rmanctl.DB1.out
00 18 * * * /u01/app/scripts/rmanctl.sh DB2 > /u01/app/scripts/out/rmanctl.DB2.out

The instance name can be case sensitive in some environments.

#!/bin/bash
# Name:       rmanctl.sh
# Comments:   Runs RMAN backup scripts.
#             Usage: rmnctl.sh {ORACLE_SID} Ex: rmnctl.sh HR
#
# 2018.05.05: Updated var usage. Fixed DOW bug.
# 2018.03.29: ME updated variable naming, added email notification.
# 2017.07.11: ME Fixed bug not deleting incr log on full backup day.
# 2017.06.28: ME Initial Version

# Standard Preamble
set -a; # Forces all vars to be exported (required to run from cron).
sScriptStarted=`date "+%Y-%m-%d %H:%M:%S"`
sScriptsDir="/u01/app/scripts";cd $sScriptsDir
sFullName=`basename "$0"`;sBaseName=${sFullName%.*}

# User Vars
usrEmailList="scott@mycompany.com"; # Email addresses for status. Space Delimited.
usrRmanDir="/u01/app/scripts/rman"; # Dir to RMAN backup scripts.
usrRmanLogs="$sScriptsDir/logs";    # Where to output log files.
usrFull=5;                          # Full backup day (Mon=1, Fri=5).
usrValidate=0;                      # If 1 then RMAN validate run after full backup.
ORACLE_HOME="/u01/app/oracle/product/12.2.0.1/dbhome_1";

# Get CmdLine Parameter
if [ -z "$1" ]; then
   printf "Error: ORACLE_SID not specified.\n" | tee $usrRmanLogs/$sBaseName.err; exit
else
   ORACLE_SID="$1"
fi

# System Vars
sDOW=$(date +%u)
sLine=`printf '=%.0s' {1..80}`
sSessionLog=$usrRmanLogs/$sBaseName.session.$ORACLE_SID.log
sHistLog=$usrRmanLogs/$sBaseName.hist.$ORACLE_SID.log

# Heading
clear
printf "$sScriptStarted\n"
printf "$sLine\n"
printf "ORACLE_SID:  $ORACLE_SID\n"
printf "usrRmanDir:  $usrRmanDir\n"
printf "usrRmanLogs: $usrRmanLogs\n"
printf "usrFull:     $usrFull\n"
printf "usrValidate: $usrValidate\n"
printf "$sLine\n"
printf "\n\n"
sleep 5


# Process: RMAN Maintenance
printf "$sScriptStarted\n" > $sSessionLog
sRScript=$usrRmanDir/rman.maint.rmn
sRLog=$usrRmanLogs/rman.maint.$ORACLE_SID.log
printf "  Running: $sRScript \n" | tee -a $sSessionLog
$ORACLE_HOME/bin/rman target / nocatalog log=$sRLog @$sRScript


# Process: RMAN Backup
if [[ "$sDOW" == "$usrFull" ]]; then
   rm $usrRmanLogs/rman.maint./rman.backup_incr.$ORACLE_SID.log >/dev/null 2>&1
   sRScript="$usrRmanDir/rman.backup_full.rmn"
   sRLog="$usrRmanLogs/rman.maint./rman.backup_full.$ORACLE_SID.log"
else
   sRScript="$usrRmanDir/rman.backup_incr.rmn"
   sRLog="$usrRmanLogs/rman.backup_incr.$ORACLE_SID.log APPEND"
fi
printf "  Running: $sRScript \n" | tee -a $sSessionLog
$ORACLE_HOME/bin/rman target / nocatalog log=$sRLog @$sRScript

if [[ $? -eq 0 ]]; then
   sMsg="RMAN Backup: OK"
   printf "  $sMsg \n" | tee -a $sSessionLog
   mail -s "$HOSTNAME.$sBaseName $ORACLE_SID (OK)" "$usrEmailList" <<< "$sMsg"
else
   sMsg="RMAN Backup: Failed"
   printf "  $sMsg \n" | tee -a $sSessionLog
   mail -s "$HOSTNAME.$sBaseName $ORACLE_SID (FAILED)" "$usrEmailList" <<< "$sMsg"
fi


# Process: Validate
if [[ "$sDOW" == "$usrFull" ]]; then
   if [[ $usrValidate -eq 1 ]]; then
      sRScript="$usrRmanDir/rman.validate.rmn"
      sRLog="$usrRmanLogs/rman.validate.$ORACLE_SID.log"
      printf "  Running: $RSCRIPT \n" | tee -a $sSessionLog
      $ORACLE_HOME/bin/rman target / nocatalog log=$sRLog @$sRScript
      nStatus=$?
      printf "  RMAN Validate Exit Status: $nStatus \n" | tee -a $sSessionLog
   fi
fi


# End
sScriptEnded=`date "+%Y-%m-%d %H:%M:%S"`
printf "$sScriptEnded \n" >> $sSessionLog
cat $sSessionLog >> $sHistLog;printf "$sLine \n" >> $sHistLog
printf "\nScript Ended: $sScriptEnded \n"

Reset RMAN Configuration

The following command resets the RMAN configuration values back to their default settings. SQL> execute dbms_backup_restore.resetConfig;


Huge Database Version (via SECTION SIZE)

  • With SECTION SIZE RMAN can backup huge tablespaces using PARALLEL.
  • SECTION SIZE and MAXPIECESIZE cannot be used together.
  • Adjust SECTION SIZE for your environment.

rman.cfg.rmn

# rman.cfg.rmn
CONFIGURE COMPRESSION ALGORITHM 'MEDIUM';
CONFIGURE CONTROLFILE AUTOBACKUP on;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE disk TO '/u02/rman/oradb/%F';
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 14 DAYS;
CONFIGURE SNAPSHOT CONTROLFILE NAME TO '/u02/rman/oradb/snapshot.ctl';

CONFIGURE DEVICE TYPE disk BACKUP TYPE TO compressed backupset PARALLELISM 2;
CONFIGURE CHANNEL 1 DEVICE TYPE disk FORMAT '/u02/rman/oradb/%U.bkp';
CONFIGURE CHANNEL 2 DEVICE TYPE disk FORMAT '/u02/rman/oradb/%U.bkp';

show all;
  • Make sure to adjust PARALLELISM n value with number of CHANNELs.
  • For instance, if PARALLELISM value is 8, you will need 8 CHANNEL entries.

rman.maint.rmn

# rman.maint.rmn
allocate channel for maintenance type disk;
crosscheck backup;
crosscheck archivelog all;
delete force noprompt obsolete;

rman.backup_full.rmn

# rman.backup_full.rmn
run { 
   backup 
   incremental level 0
   section size 32g 
   database plus archivelog delete all input;
}

rman.backup_incr.rmn

# rman.backup_incr.rmn
run { 
   backup 
   incremental level 1
   section size 32g
   database plus archivelog delete all input;
}

rman.validate.rmn

# rman.validate.rmn
run {
  restore database validate;
}

Purge RMAN Cfg and Data

Complete reset of related database and RMAN params back to default.

RMAN

  $ORACLE_HOME/bin/rman target sys/MyPassword nocatalog

  RMAN> CROSSCHECK BACKUP;
  RMAN> DELETE BACKUPSET;  Answer Y to prompt to delete all the records.
  RMAN> configure SNAPSHOT CONTROLFILE NAME clear;

SQLPlus

  SQL> execute dbms_backup_restore.resetConfig;