#!/bin/sh # # Script: rmanbackup.shl # # Purpose: Perform an RMAN backup of the current database SID, or the given # database SID's, or a pre-defined list of database SID's, or all # of the SID's listed in /etc/oratab. The backup can either be a # full backup (incremental level=0) or an incremental (differential # level=1) backup. If no parameters are specified, a full backup # of the current $ORACLE_SID is done. Otherwise, the parameters # consist of the list of SID's to be backed up and/or any of the # following: # LIST - include the pre-defined list (bkup_list) of SID's. # ORATAB - include all SID's listed in the /etc/oratab file. # FULL - make a full backup of all of the SID's that are listed. # INCR - make an incremental (level=1) backup of all SID's listed. # If the type of backup is not specified, and no specific SID's are # given (just LIST or ORATAB), an incremental backup is done Monday # through Thursday or a full backup is done Friday through Sunday # (assuming nightly backups). The pre-defined list of SID's for the # "LIST" backup is specified in the "bkup_list" variable given below: export bkup_list='PROD PPRD TEST' # Databases that are currently down will not be backed up, since they # must be at least in the mount state for RMAN to connect to them. # Databases in NOARCHIVELOG mode will be shut down and started back # up in the mount state to do their backup. Databases in ARCHIVELOG # mode will be backed up online. RMAN configuration settings (default # device type, compression, parallelism, autobackup, etc.) are given # in the rmanbackup.config file, which is run at the beginning of the # RMAN backup session, for example: # configure default device type to disk; # configure device type disk backup type to compressed backupset # parallelism 2; # configure retention policy to recovery window of 35 days; # # configure controlfile autobackup on; # Note: I'm doing the datafile, archivelog, spfile, and controlfile # backups separately so that I can add tags to them; otherwise, only # the datafile backupset will be tagged. This also lets me archive # the current log file after the datafile backups to get the most # current set of archive logs. # # Examples: # To make a full backup of the current $ORACLE_SID: # rmanbackup.shl # rmanbackup.shl full # To make an incremental backup of the current $ORACLE_SID: # rmanbackup.shl incr # To make a full backup of a given database SID, such as PROD: # rmanbackup.shl prod # rmanbackup.shl prod full # To make an incremental backup of a given database SID, such as PROD: # rmanbackup.shl prod incr # To make a full backup of the pre-defined list (bkup_list) of SID's: # rmanbackup.shl list full # To make an incremental backup of the pre-defined list of SID's: # rmanbackup.shl list incr # To make a full or incremental backup of the pre-defined list of SID's # based on the current day of the week: # rmanbackup.shl list # To make a full backup of PROD and TEST: # rmanbackup.shl prod test full # To make a full backup of the pre-defined list of SID's, plus SEED # rmanbackup.shl list seed full # To make a full backup of the SID's listed in /etc/oratab: # rmanbackup.shl oratab full # To make a full or incremental backup of the /etc/oratab SID's # based on the current day of the week: # rmanbackup.shl oratab # # Author: Stephen Rea # Maristream, Inc. # # Updates: # 10/07/08 - Initial Release # 10/14/08 - Delete archivelogs more than a week old which have been backed up # at least 5 times. Also, delete backups older than the retention # policy (but, see 10/27/08). # 10/24/08 - Remove feedback and timing lines from environment variable SQL # assignments. # 10/27/08 - Removed deletion of backups, since it ignored "keep" settings. # Set ORAENV_ASK to YES so that ". oraenv" will prompt for the SID. # 10/31/08 - Run this script using the Bourne shell (for the export command). # 5/11/09 - Include the Oracle SID and the backup type (dbf, spf, ctl, arc) # in the backup file names. # 6/30/09 - Create and back up a standby control file for Data Guard. # unset bkup_parm bkup_listx bkup_sid bkup_type bkup_tag bkup_date bkup_log_mode export NLS_DATE_FORMAT='DD-MON-YY HH24:MI:SS' export ORAENV_ASK=YES # # If there are no parameters, make a full backup of the current ORACLE_SID. # if [ $# -eq 0 ]; then export bkup_list=$ORACLE_SID export bkup_type=FULL # # Otherwise, loop through the parameters, converting them to upper case ... # else for bkup_parm do export bkup_parm=`echo $bkup_parm | tr '[a-z]' '[A-Z]'` # # Set the backup type to INCR or FULL if given. # if [ "$bkup_parm" = "INCR" -o "$bkup_parm" = "FULL" ]; then export bkup_type=$bkup_parm # # Attach the predefined backup list of SID's, if LIST is given and the SID's # are not already listed. # elif [ "$bkup_parm" = "LIST" ]; then for bkup_sid in `echo "$bkup_list" | tr ' ' '\012'` do if [ -z "`echo $bkup_listx | grep $bkup_sid`" ]; then export bkup_listx="$bkup_listx $bkup_sid" fi done # # Attach the SID's listed in /etc/oratab, if ORATAB is given and the SID's # are not already listed. # elif [ "$bkup_parm" = "ORATAB" ]; then for bkup_sid in `cat /etc/oratab | grep -v '^ *#' | sed 's/:.*//'` do if [ -z "`echo $bkup_listx | grep $bkup_sid`" ]; then export bkup_listx="$bkup_listx $bkup_sid" fi done # # Otherwise, this must be a SID - attach it to the list to back up, # if not already listed, and set to full backup, if not already set. # else if [ -z "`echo $bkup_listx | grep $bkup_parm`" ]; then export bkup_listx="$bkup_listx $bkup_parm" fi if [ -z "$bkup_type" ]; then export bkup_type=FULL fi fi done # # If no SID's were listed to back up, back up the current ORACLE_SID; # else, back up the SID's given in the parameter list. # if [ -z "$bkup_listx" ]; then export bkup_list=$ORACLE_SID else export bkup_list=$bkup_listx fi # # If no backup type was given, do the backup based on the current # day of the week: Incremental backup for Monday through Thursday, # else Full backup for Friday through Sunday. # if [ -z "$bkup_type" ]; then if [ `date +"%u"` -le 4 ]; then export bkup_type=INCR else export bkup_type=FULL fi fi fi # # List the backup(s) to be performed. # export bkup_date=`date +"%y%m%d%H%M%S%a$bkup_type"` echo for bkup_sid in `echo "$bkup_list" | tr ' ' '\012'` do unset bkup_log_mode echo $bkup_sid | . oraenv >/dev/null `echo \`sqlplus -S "/ as sysdba" </dev/null set feedback off timing off select log_mode from v\\\\$database; EOF\` | sed 's/.* \(.*\)/export bkup_log_mode=\1/'` export bkup_tag="${bkup_sid}_${bkup_date}" if [ "${ORACLE_SID}" != "${bkup_sid}" ]; then echo "The \". oraenv\" command is unable to change the ORACLE_SID to ${bkup_sid}" elif [ "$bkup_log_mode" = "NOARCHIVELOG" ]; then echo "Shut down and back up $bkup_sid $bkup_type; tag: $bkup_tag" elif [ "$bkup_log_mode" = "ARCHIVELOG" ]; then echo "Back up $bkup_sid $bkup_type; tag: $bkup_tag" else echo "Database $bkup_sid is down or unavailable and will not be backed up" fi done # # Prompt for confirmation, unless the bkup_prompt environment variable has # been set to NO (outside this script; case sensitive). # if [ "$bkup_prompt" != "NO" ]; then echo echo "Do you want to run the above backup(s) (y/n)? \c"; read ANS if [ ! \( "$ANS" = "y" -o "$ANS" = "Y" \) ]; then exit fi fi # # Back up the database(s). # echo echo "Running backup(s) ..." for bkup_sid in `echo "$bkup_list" | tr ' ' '\012'` do unset bkup_log_mode echo $bkup_sid | . oraenv >/dev/null `echo \`sqlplus -S "/ as sysdba" </dev/null set feedback off timing off select log_mode from v\\\\$database; EOF\` | sed 's/.* \(.*\)/export bkup_log_mode=\1/'` export bkup_tag="${bkup_sid}_${bkup_date}" if [ "${ORACLE_SID}" != "${bkup_sid}" ]; then export bkup_log_mode=CantSwitchTo${bkup_sid} fi # # For databases in NOARCHIVELOG mode, shut them down and back them up. # if [ "$bkup_log_mode" = "NOARCHIVELOG" ]; then echo echo "-------------------------------------------------------------------------" echo "Shutting down and backing up $bkup_sid $bkup_type; tag: $bkup_tag ..." echo "-------------------------------------------------------------------------" sqlplus "/ as sysdba" <