fbpx

RMAN Level 0 and Level 1 Backup Script

In this article I show you how to backup your Oracle database using a bash script.

You will use 3 files for the RMAN backup.

You execute it like that to generate a level 0 backup for your SID:

./rman_backup.sh -d {SID} -l 0

For level 1 backup you should execute it like that

./rman_backup.sh -d {SID} -l 1

If you want to backup all databases on the server you can execute the script like that

./rman_backup.sh -d A -l 0

The backup will go to the flash / fast recovery area.

Here is the full backup script.

You should name this file as backup_level0.rman

RUN {
    ALLOCATE CHANNEL CH1 DEVICE TYPE DISK;
    ALLOCATE CHANNEL CH2 DEVICE TYPE DISK;
    BACKUP tag 'INCR0_DB' AS COMPRESSED BACKUPSET INCREMENTAL LEVEL 0 DATABASE PLUS ARCHIVELOG DELETE INPUT;
    BACKUP CURRENT CONTROLFILE TAG 'INCR0_CTL';
    BACKUP SPFILE TAG 'INCR0_SPFILE';
    #DELETE NOPROMPT EXPIRED BACKUP;
    #DELETE NOPROMPT OBSOLETE;
    RELEASE CHANNEL CH1;
    RELEASE CHANNEL CH2;
}

Here is the level 1 backup, you should name this file as backup_level1.rman

RUN {
    ALLOCATE CHANNEL CH1 DEVICE TYPE DISK;
    ALLOCATE CHANNEL CH2 DEVICE TYPE DISK;
    BACKUP tag 'INCR_L1_DB' AS COMPRESSED BACKUPSET INCREMENTAL LEVEL 1 DATABASE PLUS ARCHIVELOG DELETE INPUT;
    BACKUP CURRENT CONTROLFILE TAG 'INCR_L1_CTL';
    BACKUP SPFILE TAG 'INCR_L1_SPFILE';
    #DELETE NOPROMPT EXPIRED BACKUP;
    #DELETE NOPROMPT OBSOLETE;
    RELEASE CHANNEL CH1;
    RELEASE CHANNEL CH2;
}

And here is the bash script to backup the database.

You should name it as backup_rman.sh

#!/bin/bash

# This script backs up Oracle Databases using RMAN
# The backup level (either 0 or 1) and ORACLE_SID are passed as command-line arguments
# If -d A is specified, it backs up all databases on the server
# If no level is provided, the script will show a warning and exit

# Get script directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# ORATAB location - typically /etc/oratab on Unix/Linux systems
ORATAB="/etc/oratab"

# Check if any options are provided
# If not, display usage information and exit
if [ $# -eq 0 ]; then
    echo "Usage: $0 -d ORACLE_SID -l BACKUP_LEVEL"
    echo "ORACLE_SID: The Oracle System ID (SID) or A for all databases."
    echo "BACKUP_LEVEL: The backup level, either 0 or 1."
    exit 1
fi

# Initialize variables
DB_SID=""
LEVEL=""

# Parse command-line options for Oracle SID and backup level
while getopts ":d:l:" opt; do
  case $opt in
    d) DB_SID="$OPTARG" ;;
    l) LEVEL="$OPTARG" ;;
    \?) echo "Invalid option -$OPTARG" >&2; exit 1 ;;
  esac
done

# Check if a backup level has been provided
if [ -z "$LEVEL" ]; then
    echo "Warning: Backup level not specified. Please provide a backup level (0 or 1)."
    exit 1
fi

# Function to perform backup
perform_backup() {
    local sid=$1
    local level=$2

    # Adjust log file name to include L0 or L1 based on backup level
    local level_tag="L${level}"
    LOG_FILE="${SCRIPT_DIR}/rman_backup_${sid}_${level_tag}_$(date '+%Y%m%d_%H%M%S').log"

    # Set the necessary Oracle environment variables
    ORACLE_HOME=$(grep ^$sid: $ORATAB | cut -d: -f2)
    export ORACLE_HOME
    export ORACLE_SID=$sid
    export PATH=$PATH:$ORACLE_HOME/bin

    echo "Backing up database ORACLE_SID=$sid at level $level"

    # Determine which backup command file to use
    local cmdfile="${SCRIPT_DIR}/backup_level${level}.rman"

    # Perform the backup
    rman target / log=$LOG_FILE cmdfile=$cmdfile
}

# Backup all databases if -d A is specified
if [ "$DB_SID" = "A" ]; then
    # Read each entry in /etc/oratab
    grep -v '^#' $ORATAB | grep -v '^\*' | cut -d: -f1 | while read sid; do
        if [ -n "$sid" ]; then
            perform_backup "$sid" "$LEVEL"
        fi
    done
else
    # Backup a single database
    if grep -q "^$DB_SID:" $ORATAB; then
        perform_backup "$DB_SID" "$LEVEL"
    else
        echo "The Oracle SID provided does not exist in /etc/oratab."
        exit 1
    fi
fi

I hope this helps.

If you have questions, just post them in the comments.

One Reply to “RMAN Level 0 and Level 1 Backup Script”

Leave a Reply

Your email address will not be published. Required fields are marked *