Skip to main content

 

Druva Documentation

Template shell scripts for Linux RMAN hosts

Phoenix Editions: File:/tick.png Business File:/cross.png Enterprise File:/tick.png Elite

Druva provides template shell scripts for on Linux RMAN hosts that you can use to backup databases. The scripts are

  • phoenix_get_mount_details.sh: This script provides the details of the backup mount and the Phoenix Backup Store. Use this script to get the location of the backup mount so that you can map it to the RMAN host. 
  • oracle_rman_backup.sh: This script runs the RMAN commands that backup a database, create an Oracle RMAN backup and store it on the backup mount. You can modify this script to provide RMAN parameters that specify the type of backup and information related to backup like filesperset and channels for a backup.  

The following sections provide the contents of the shell scripts. You can edit these scripts to suit your requirements. 

 phoenix_get_mount_details.sh

#!/bin/bash

if [ "$#" -ne 2 ]; then
    echo ""
    echo "Usage: $0 <backup_store_IP> <backup_mount_name>"
    echo ""
    echo "      <backup_store_IP> : IP address of the Phoenix Backup Store"
    echo "      <backup_mount_name> : Name of the backup mount configured in Phoenix, e.g. mount1"

    exit 1
fi

BACKUP_STORE_IP=$1
BACKUP_MOUNT=$2

res=$(curl -X POST http://$BACKUP_STORE_IP:9090/druva-phoenix/v1/mounts/$BACKUP_MOUNT/backup 2> /dev/null)
if [ $? -ne 0 ];
then
    echo "Request to start phoenix backup failed... exiting"
    echo "Response from Phoenix Backup Store: "$res
    exit 1
fi

# Sample output format
# {"error_code":4295622665,"error_msg":"Backup mount does not exist on Export Service.","result":{"mount_name":"mount4","mount_path":""}}
ERROR_CODE=$(echo $res|awk -F "error_code" '{print $2}' | cut -d ':' -f2|cut -d ',' -f1)
if [ $ERROR_CODE -ne 0 ];
then
    echo "Response from Phoenix Backup Store: "$res
    echo "Error getting mount details... exiting"
    exit 1
fi

REMOTE_MOUNTPOINT=$BACKUP_STORE_IP:$(echo $res|awk -F "mount_path" '{print $2}'|cut -d '"' -f3)
echo ""
echo "Remote mountpath is $REMOTE_MOUNTPOINT"
MOUNT_OPTS=$(echo $res|awk -F "mount_params" '{print $2}'|cut -d '{' -f2|cut -d '}' -f1)
echo "Mount options : rw,user,$MOUNT_OPTS"

oracle_rman_backup.sh

#!/bin/bash

if [ "$#" -ne 3 ]; then
    echo ""
    echo "Usage: $0 <logfile_directory> <backup_store_IP> <backup_mount_name>"
    echo ""
    echo "      <logfile_directory> : Directory path for RMAN logs"
    echo "      <backup_store_IP> : IP address of the Phoenix Backup Store"
    echo "      <backup_mount_name> : Name of the backup mount configured in Phoenix, e.g. mount1"

    exit 1
fi

LOG_FILE_DIR=$1
BACKUP_STORE_IP=$2
BACKUP_MOUNT=$3

PARALLELISM=1

if [[ -z "${ORACLE_HOME}" ]];
then
    echo "Environment variable ORACLE_HOME not set... exiting"
    exit 1
fi

if [[ -z "${ORACLE_SID}" ]];
then
    echo "Environment variable ORACLE_SID not set... exiting"
    exit 1
fi

echo "Validating backup request"
validate_resp=$(curl -X GET http://$BACKUP_STORE_IP:9090/druva-phoenix/v1/mounts/$BACKUP_MOUNT/backup 2> /dev/null)
if [ $? -ne 0 ];
then
    echo "Request to validate phoenix backup failed... exiting"
    echo "Response from Phoenix Backup Store: "$validate_resp
    exit 1
fi

VALIDATE_ERROR_CODE=$(echo $validate_resp|awk -F "error_code" '{print $2}' | cut -d ':' -f2|cut -d ',' -f1)
if [ $VALIDATE_ERROR_CODE -ne 0 ];
then
    echo "Response from Phoenix Backup Store: "$validate_resp
    echo "Request to validate Phoenix backup failed... exiting"
    exit 1
fi

# Sample output format
# {"error_code":0,"error_msg":"","result":{"is_reached_max_snapshots":false,"mount_name":"m2"}}
IS_REACHED_MAX_SNAPSHOTS=$(echo $validate_resp|awk -F "is_reached_max_snapshots" '{print $2}'|cut -d ',' -f1 |cut -d ':' -f2)
if [ $IS_REACHED_MAX_SNAPSHOTS == "false" ];
then
    echo "Validation for phoenix backup succeeded"
else
    echo "Response from Phoenix Backup Store: "$validate_resp
    echo "Validation for phoenix backup failed... exiting"
    exit 1
fi

echo "Notifying Phoenix to start backup - Phoenix Backup Store=$BACKUP_STORE_IP mount name=$BACKUP_MOUNT"

res=$(curl -X POST http://$BACKUP_STORE_IP:9090/druva-phoenix/v1/mounts/$BACKUP_MOUNT/backup 2> /dev/null)
if [ $? -ne 0 ];
then
    echo "Request to start phoenix backup failed... exiting"
    echo "Response from Phoenix Backup Store: "$res
    exit 1
fi

# Sample output format
# {"error_code":4295622665,"error_msg":"Backup mount does not exist on Export Service.","result":{"mount_name":"mount4","mount_path":""}}
ERROR_CODE=$(echo $res|awk -F "error_code" '{print $2}' | cut -d ':' -f2|cut -d ',' -f1)

if [ $ERROR_CODE -ne 0 ];
then
    echo "Response from Phoenix Backup Store: "$res
    echo "Request to start Phoenix backup failed... exiting"
    exit 1
fi

REMOTE_MOUNTPOINT=$BACKUP_STORE_IP:$(echo $res|awk -F "mount_path" '{print $2}'|cut -d '"' -f3)
RMANBACKUP_MOUNTPOINT=$(grep "^$REMOTE_MOUNTPOINT" /etc/fstab| sed 's/\s\+/ /g' |cut -d ' ' -f2)

if [ -z "$RMANBACKUP_MOUNTPOINT" ];
then
    echo "Entry for Phoenix Backup Store mount not found in /etc/fstab... exiting"
    exit 1
fi

ACTUAL_MOUNTPOINT=$(mount|grep "^$REMOTE_MOUNTPOINT"|sed 's/\s\+/ /g' |cut -d " " -f3 2> /dev/null)
if [ -z "$ACTUAL_MOUNTPOINT" ];
then
    mount $RMANBACKUP_MOUNTPOINT 2> /dev/null
    if [ $? -ne 0 ];
    then
        echo "Unable to mount $RMANBACKUP_MOUNTPOINT... exiting"
        exit 1
    fi
else
    RMANBACKUP_MOUNTPOINT=$ACTUAL_MOUNTPOINT
fi

export PATH=$ORACLE_HOME/bin:$PATH
full_backup () {

if [ ! -e $RMANBACKUP_MOUNTPOINT'/full' ]
then
    mkdir -p $RMANBACKUP_MOUNTPOINT'/full'
    if [ $? -eq 0 ];
    then
        echo "Created directory for full backup $RMANBACKUP_MOUNTPOINT/full"
    else
        echo "Oracle user does not have permission for $RMANBACKUP_MOUNTPOINT directory... exiting"
        exit 1
    fi
elif [ ! -d $RMANBACKUP_MOUNTPOINT'/full' ];
then
    echo "$RMANBACKUP_MOUNTPOINT is not a directory... exiting"
fi

if [ ! -e $LOG_FILE_DIR ]
then
    mkdir -p $LOG_FILE_DIR
    if [ $? -eq 0 ];
    then
        echo "Created log directory for oracle backup $LOG_FILE_DIR"
    else
        echo "Oracle user does not have permission for $LOG_FILE_DIR directory... exiting"
        exit 1
    fi
fi

echo -n >$LOG_FILE_DIR'/rman_full_backup.log'
touch $RMANBACKUP_MOUNTPOINT/_workspace/
if [ $? -ne 0 ];
then
    echo "$RMANBACKUP_MOUNTPOINT directory is not writable.. exiting"
    exit 1
fi

echo "Starting with RMAN backup"
rman log=$LOG_FILE_DIR'/rman_full_backup.log' 2> /dev/null << EOF

connect target /
set echo on;

configure backup optimization on;
configure controlfile autobackup on;
configure device type disk parallelism $PARALLELISM BACKUP TYPE TO COPY;
configure datafile backup copies for device type disk to 1;
configure archivelog backup copies for device type disk to 1;
configure channel device type disk format '$RMANBACKUP_MOUNTPOINT/full/full_bk_datafile_%U.rbk';
configure controlfile autobackup format for device type disk to '$RMANBACKUP_MOUNTPOINT/full/full_bk_controlfile_%F';

run
{

backup incremental level 1 for recover of copy with tag 'phoenix_oracle_backup' database;
backup format '$RMANBACKUP_MOUNTPOINT/full/full_bk_Archivelog_%d_%s_%t' archivelog all not backed up;
recover copy of database with tag 'phoenix_oracle_backup';

#Force is to ignore I/O errors
delete noprompt obsolete device type disk;
delete force NOPROMPT expired copy;
delete force NOPROMPT expired backup;

crosscheck backup;
crosscheck copy;
}

configure backup optimization clear;
configure controlfile autobackup clear;

exit

EOF

RMAN_EXIT_STATUS=$?
if [ $RMAN_EXIT_STATUS -ne 0 ];
then
    echo ""
    echo "rman failed with status $RMAN_EXIT_STATUS.. exiting"
    exit 1
fi

echo ""
file_stamp=`date | sed -e 's/ /_/g'`
mv $LOG_FILE_DIR/rman_full_backup.log $LOG_FILE_DIR/rman_full_backup_$file_stamp.log
echo "RMAN log location: $LOG_FILE_DIR/rman_full_backup_$file_stamp.log"

echo "Notifying Phoenix to upload RMAN Logs"

rman_uploadlog_res=$(curl -X POST -H "Content-Type: multipart/form-data" -F "file=@$LOG_FILE_DIR/rman_full_backup_$file_stamp.log" -F mountpoint=$RMANBACKUP_MOUNTPOINT -F platform=linux http://$BACKUP_STORE_IP:9090/druva-phoenix/v1/mounts/$BACKUP_MOUNT/uploadlog 2> /dev/null)
if [ $? -ne 0 ];
then
    echo "Request to send RMAN Logs failed..."
    echo "Response from Phoenix Backup Store: "$res
fi

echo "Notifying Phoenix to end backup"
final_res=$(curl -X PUT http://$BACKUP_STORE_IP:9090/druva-phoenix/v1/mounts/$BACKUP_MOUNT/backup 2> /dev/null)

if [ $? -ne 0 ];
then
    echo "Request to end phoenix backup failed... exiting"
    echo "Response from Phoenix Backup Store: "$res
    exit 1
fi
ERROR_CODE=$(echo $final_res|awk -F "error_code" '{print $2}' | cut -d ':' -f2|cut -d ',' -f1)
if [ $ERROR_CODE -ne 0 ];
then
    echo "Response from Phoenix Backup Store: "$res
    echo "Request to start Phoenix backup failed... exiting"
fi
echo $final_res

echo "Exiting with successful status"
}

# Main
full_backup