You can use this RMAN script to create a backup of an Oracle database and save it to a specified destination.
It is important that the BACKUP_BASE_PATH exists and the ORACLE_SID is replaced in the script.
The script will create a folder inside of BACKUP_BASE_PATH with the ORACLE_SID.
#!/bin/bash
# Define backup base path
BACKUP_BASE_PATH="/home/oracle/backup" # Replace this with your path
# Provided Oracle SID
export ORACLE_SID=oradb # Replace this with your database SID
# Determine the backup path for this specific Oracle SID
BACKUP_PATH="${BACKUP_BASE_PATH}/${ORACLE_SID}"
# Create the backup directory if it doesn't exist
if [ ! -d "$BACKUP_PATH" ]; then
mkdir -p "$BACKUP_PATH"
if [ $? -ne 0 ]; then
echo "Failed to create backup directory: $BACKUP_PATH"
exit 1
fi
fi
# Automatically determine ORACLE_HOME based on ORACLE_SID from /etc/oratab
# Assumes the oratab entry is in the format: <sid>:<oracle_home>:<start_options>
export ORACLE_HOME=$(grep "^$ORACLE_SID:" /etc/oratab | cut -d: -f2)
if [ -z "$ORACLE_HOME" ]; then
echo "ORACLE_HOME could not be determined from /etc/oratab"
exit 1
fi
export PATH=$ORACLE_HOME/bin:$PATH
# Define timestamp
TIMESTAMP=$(date +%Y%m%d_%H%M%S) # Generates a timestamp in 'YYYYMMDD_HHMMSS' format
# Define tags based on ORACLE_SID
DATABASE_TAG="${ORACLE_SID}_DB"
CONTROLFILE_TAG="${ORACLE_SID}_CTL"
SPFILE_TAG="${ORACLE_SID}_SPFILE"
ARCHIVELOG_TAG="${ORACLE_SID}_ARC"
# Define the RMAN script file
RMAN_SCRIPT="${BACKUP_BASE_PATH}/rman_backup_${TIMESTAMP}.rcv"
# Define the log file
LOG_FILE="${BACKUP_BASE_PATH}/rman_backup_${TIMESTAMP}.log"
# Create the RMAN script file
cat <<EOF > ${RMAN_SCRIPT}
RUN {
BACKUP AS COMPRESSED BACKUPSET
DATABASE TAG '${DATABASE_TAG}' FORMAT '${BACKUP_PATH}/${ORACLE_SID}_DB_${TIMESTAMP}_%U.bkp'
CURRENT CONTROLFILE TAG '${CONTROLFILE_TAG}' FORMAT '${BACKUP_PATH}/${ORACLE_SID}_CTL_${TIMESTAMP}_%U.bkp'
SPFILE TAG '${SPFILE_TAG}' FORMAT '${BACKUP_PATH}/${ORACLE_SID}_SPFILE_${TIMESTAMP}_%U.bkp'
PLUS ARCHIVELOG TAG '${ARCHIVELOG_TAG}' FORMAT '${BACKUP_PATH}/${ORACLE_SID}_ARC_${TIMESTAMP}_%U.bkp';
}
EOF
# Execute the RMAN script with nohup and tee to capture output in a log file
nohup rman target / @${RMAN_SCRIPT} | tee ${LOG_FILE} &
You can also add these 2 lines to the run block if you want
SQL "ALTER DATABASE BACKUP CONTROLFILE TO TRACE AS ''${BACKUP_PATH}/controlfile_${TIMESTAMP}.trc'' ";
SQL "CREATE PFILE=''${BACKUP_PATH}/init${ORACLE_SID}_${TIMESTAMP}.ora'' FROM SPFILE";