Skip to main content
Druva Documentation

Template shell scripts for Linux and Solaris RMAN hosts

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

Druva provides template shell scripts for on Linux and Solaris 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 back up 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.  
  • oracle_rman_data_backup: This script runs the RMAN commands that back up data files on Linux RMAN hosts, create an Oracle RMAN backup, and store it on the backup mount. You can modify the script to modify RMAN parameters.
  • oracle_rman_archivelog_backup: This script runs the RMAN commands that back up archived log files on Linux RMAN hosts, create an Oracle RMAN backup for archived logs, and store it on the backup mount. You can modify the script to modify RMAN parameters.
  • oracle_rman_data_backup_solaris: This script runs the RMAN commands that back up data files on Solaris RMAN hosts, create an Oracle RMAN backup, and store it on the backup mount. You can modify the script to modify RMAN parameters.
  • oracle_rman_archivelog_backup_solaris: This script runs the RMAN commands that back up archived log files on Solaris RMAN hosts, create an Oracle RMAN backup for archived logs, and store it on the backup mount. You can modify the script to modify RMAN parameters.

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

oracle_rman_data_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 $RMANBACKUP_MOUNTPOINT'/archivelogs' ]
then
    mkdir -p $RMANBACKUP_MOUNTPOINT'/archivelogs'
    if [ $? -eq 0 ];
    then
        echo "Created directory for archivelogs backup $RMANBACKUP_MOUNTPOINT/archivelogs"
    else
        echo "Oracle user does not have permission for $RMANBACKUP_MOUNTPOINT directory... exiting"
        exit 1
    fi
elif [ ! -d $RMANBACKUP_MOUNTPOINT'/archivelogs' ];
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

#Checking if any .lock file is present and deleting the same if the PID is NOT running.

is_lock_file_present=`find ${RMANBACKUP_MOUNTPOINT} -type f -name ".RMAN_FULL_lock_*"|wc -l `
if [[ ${is_lock_file_present} -ne 0 ]]
then
        for _lock_file in `find ${RMANBACKUP_MOUNTPOINT} -type f -name ".RMAN_FULL_lock_*" | xargs `
        do
                _pid=`echo ${_lock_file}|cut -d"_" -f4`
                is_pid_proc_running=`ps -ef|grep ${_pid}|grep -v grep | wc -l`
                if [[ ${is_pid_proc_running} -ne 0 ]]
                then
                        echo "[INFO] Full backup Lock File: ${_lock_file} is present and PID is active"
                        echo "[ERROR] One Full backup is already in progress with process ${_pid}. Exiting current backup"
                        exit 1
                else
                        echo "[INFO] Full backup Lock File: ${_lock_file} is present and PID is NOT active"
                        echo "Deleting ${_lock_file}"
                        rm ${_lock_file}
                        if [ -e ${_lock_file} ]
                        then
                                echo "ERROR deleting file ${_lock_file}"
                        fi
                fi
        done
fi

# Creating a .lock file to represnt current active full/diff backup.

export file_stamp=`date | sed -e 's/ /_/g'`
PID=`echo $$`
LOCK_FILE=`echo ${RMANBACKUP_MOUNTPOINT}/.RMAN_FULL_lock_${PID}_${file_stamp}`
echo -n > ${LOCK_FILE}
if [ -e ${LOCK_FILE} ]
then
        echo "[INFO] Lock file created: ${LOCK_FILE}"
else
        echo "Error creating lock file: ${LOCK_FILE}"
        echo "[ERROR] Backup will now stop"
        exit -3
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/datafile_%U.bkp';
configure controlfile autobackup format for device type disk to '$RMANBACKUP_MOUNTPOINT/full/full_controlfile_%d_%F';

run
{
sql 'alter system archive log current';
backup incremental level 1 for recover of copy with tag 'phoenix_oracle_backup' database;
recover copy of database with tag 'phoenix_oracle_backup';
sql 'alter system archive log current';
backup as backupset format '${RMANBACKUP_MOUNTPOINT}/archivelogs/%d_%h_%e_%s_%t.arc' archivelog all not backed up;

#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"
    rm ${LOCK_FILE}
    if [ -e ${LOCK_FILE} ]
    then
        echo "[ERROR] Could NOT delete Lock file: ${LOCK_FILE}"
    else
        dt=`date | sed -e 's/ /_/g'`
        echo "[INFO] Lock file deleted: ${LOCK_FILE} at ${dt} "
    fi
    exit 1
fi

echo ""
#export 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

    rm ${LOCK_FILE}
    if [ -e ${LOCK_FILE} ]
    then
        echo "[ERROR] Could NOT delete Lock file: ${LOCK_FILE}"
    else
        dt=`date | sed -e 's/ /_/g'`
        echo "[INFO] Lock file deleted: ${LOCK_FILE} at ${dt} "
    fi
    exit 1
fi

rm ${LOCK_FILE}
if [ -e ${LOCK_FILE} ]
then
        echo "[ERROR] Could NOT delete Lock file: ${LOCK_FILE}"
else
        dt=`date | sed -e 's/ /_/g'`
        echo "[INFO] Lock file deleted: ${LOCK_FILE} at ${dt} "
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

oracle_rman_archivelog_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
arch_backup () {

if [ ! -e $RMANBACKUP_MOUNTPOINT'/archivelogs' ]
then
    mkdir -p $RMANBACKUP_MOUNTPOINT'/archivelogs'
    if [ $? -eq 0 ];
    then
        echo "Created directory for archivelogs backup $RMANBACKUP_MOUNTPOINT/archivelogs"
    else
        echo "Oracle user does not have permission for $RMANBACKUP_MOUNTPOINT directory... exiting"
        exit 1
    fi
elif [ ! -d $RMANBACKUP_MOUNTPOINT'/archivelogs' ];
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'/archivelogs_backup.log'
touch $RMANBACKUP_MOUNTPOINT/_workspace/
if [ $? -ne 0 ];
then
    echo "$RMANBACKUP_MOUNTPOINT directory is not writable.. exiting"
    exit 1
fi

# Checking if any of the Full/Diff backup is currently running and PID is active.
# In this case, we will fail/skip the current Log backup.
# Next trigger (external) of this script will retry log backup.

#LOCK_FILE=`echo ${RMANBACKUP_MOUNTPOINT}/.RMAN_FULL_lock_${PID}_${file_stamp}`

is_lock_file_present=`find ${RMANBACKUP_MOUNTPOINT} -type f -name ".RMAN_FULL_lock_*"|wc -l `
export BKP_CONTINUE=0
if [[ ${is_lock_file_present} -ne 0 ]]
then
        for _lock_file in `find ${RMANBACKUP_MOUNTPOINT} -type f -name ".RMAN_FULL_lock_*" | xargs `
        do
                _pid=`echo ${_lock_file}|cut -d"_" -f4`
                is_pid_proc_running=`ps -ef|grep ${_pid}|grep -v grep | wc -l`
                if [[ ${is_pid_proc_running} -ne 0 ]]
                then
                        echo "[INFO] Full backup Lock File: ${_lock_file} is present and PID is active"
                        export BKP_CONTINUE=1
                else
                        echo "[INFO] Full backup Lock File: ${_lock_file} is present and PID is NOT active"
                        echo "Deleting ${_lock_file}"
                        rm ${_lock_file}
                        if [ -e ${_lock_file} ]
                        then
                                echo "ERROR deleting file ${_lock_file}"
                                echo "Exiting Log backup"
                                export BKP_CONTINUE=1
                        fi
                fi
        done

        if [[ ${BKP_CONTINUE} -eq 1 ]]
        then
                echo "[WARNING] There is full backup running or the lock file for Full/diff backup is present"
                echo "[ERROR] Exiting the current Log backup"
                exit 5
        fi
fi

echo "Starting with RMAN backup"
rman log=$LOG_FILE_DIR'/archivelogs_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 controlfile autobackup format for device type disk to '${RMANBACKUP_MOUNTPOINT}/archivelogs/arch_controlfile_%d_%F.bkp';

run
{
sql 'alter system archive log current';
backup as backupset format '${RMANBACKUP_MOUNTPOINT}/archivelogs/%d_%h_%e_%s_%t.arc' archivelog all not backed up;

#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/archivelogs_backup.log $LOG_FILE_DIR/archivelogs_backup_$file_stamp.log
echo "RMAN log location: $LOG_FILE_DIR/archivelogs_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/archivelogs_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
arch_backup

oracle_rman_data_backup_Solaris.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|/usr/xpg4/bin/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|/usr/xpg4/bin/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|/usr/xpg4/bin/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|/usr/xpg4/bin/awk -F "mount_path" '{print $2}'|cut -d '"' -f3)
RMANBACKUP_MOUNTPOINT=$(grep "$REMOTE_MOUNTPOINT" /etc/vfstab| sed 's/\s\+/ /g' |cut -d ' ' -f3)


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

ACTUAL_MOUNTPOINT=$(mount|grep -w "$REMOTE_MOUNTPOINT"| cut -d" " -f1 |head -1|sed 's/\s\+/ /g'  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 () {

#Creating full backup directory

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

# Creating archivelog directory
if [ ! -e $RMANBACKUP_MOUNTPOINT'/archivelogs' ]
then
    mkdir -p $RMANBACKUP_MOUNTPOINT'/archivelogs'
    if [ $? -eq 0 ];
    then
        echo "Created directory for archivelogs backup $RMANBACKUP_MOUNTPOINT/archivelogs"
    else
        echo "Oracle user does not have permission for $RMANBACKUP_MOUNTPOINT directory... exiting"
        exit 1
    fi
elif [ ! -d $RMANBACKUP_MOUNTPOINT'/archivelogs' ];
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_${ORACLE_SID}.log'
touch $RMANBACKUP_MOUNTPOINT/_workspace/
if [ $? -ne 0 ];
then
    echo "$RMANBACKUP_MOUNTPOINT directory is not writable.. exiting"
    exit 1
fi

#Checking if any .lock file is present and deleting the same if the PID is NOT running.

is_lock_file_present=`find ${RMANBACKUP_MOUNTPOINT} -type f -name ".RMAN_FULL_lock_*"|wc -l `
if [[ ${is_lock_file_present} -ne 0 ]]
then
        for _lock_file in `find ${RMANBACKUP_MOUNTPOINT} -type f -name ".RMAN_FULL_lock_*" | xargs `
        do
                _pid=`echo ${_lock_file}|cut -d"_" -f4`
                is_pid_proc_running=`ps -ef|grep ${_pid}|grep -v grep | wc -l`
                if [[ ${is_pid_proc_running} -ne 0 ]]
                then
                        echo "[INFO] Full backup Lock File: ${_lock_file} is present and PID is active."
                        echo "[ERROR] One Full backup is already in progress with process ${_pid}. Exiting current backup"
                        exit 1

                else
                        echo "[INFO] Full backup Lock File: ${_lock_file} is present and PID is NOT active"
                        echo "Deleting ${_lock_file}"
                        rm ${_lock_file}
                        if [ -e ${_lock_file} ]
                        then
                                echo "ERROR deleting file ${_lock_file}"
                        fi
                fi
        done
fi

# Creating a .lock file to represnt current active full/diff backup.

export file_stamp=`date | sed -e 's/ /_/g'`
PID=`echo $$`
LOCK_FILE=`echo ${RMANBACKUP_MOUNTPOINT}/.RMAN_FULL_lock_${PID}_${file_stamp}`
echo -n > ${LOCK_FILE}
if [ -e ${LOCK_FILE} ]
then
        echo "[INFO] Lock file created: ${LOCK_FILE}"
else
        echo "Error creating lock file: ${LOCK_FILE}"
        echo "[ERROR] Backup will now stop"
        exit -3
fi

echo "Starting with RMAN backup"
rman log=$LOG_FILE_DIR'/rman_full_backup_${ORACLE_SID}.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/datafile_%U.bkp';
configure controlfile autobackup format for device type disk to '$RMANBACKUP_MOUNTPOINT/full/full_controlfile_%d_%F';

run
{
sql 'alter system archive log current';
backup incremental level 1 for recover of copy with tag 'phoenix_oracle_backup' database;
recover copy of database with tag 'phoenix_oracle_backup';
sql 'alter system archive log current';
backup as backupset format '${RMANBACKUP_MOUNTPOINT}/archivelogs/%d_%h_%e_%s_%t.arc' archivelog all not backed up;
#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"
    rm ${LOCK_FILE}
    if [ -e ${LOCK_FILE} ]
    then
        echo "[ERROR] Could NOT delete Lock file: ${LOCK_FILE}"
    else
        dt=`date | sed -e 's/ /_/g'`
        echo "[INFO] Lock file deleted: ${LOCK_FILE} at ${dt} "
    fi
    exit 1
fi

echo ""
#export file_stamp=`date | sed -e 's/ /_/g'`
mv $LOG_FILE_DIR/rman_full_backup_${ORACLE_SID}.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

    rm ${LOCK_FILE}
    if [ -e ${LOCK_FILE} ]
    then
        echo "[ERROR] Could NOT delete Lock file: ${LOCK_FILE}"
    else
        dt=`date | sed -e 's/ /_/g'`
        echo "[INFO] Lock file deleted: ${LOCK_FILE} at ${dt} "
    fi
    exit 1
fi

rm ${LOCK_FILE}
if [ -e ${LOCK_FILE} ]
then
        echo "[ERROR] Could NOT delete Lock file: ${LOCK_FILE}"
else
        dt=`date | sed -e 's/ /_/g'`
        echo "[INFO] Lock file deleted: ${LOCK_FILE} at ${dt} "
fi

ERROR_CODE=$(echo $final_res|/usr/xpg4/bin/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

oracle_rman_archivelog_backup_Solaris.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|/usr/xpg4/bin/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|/usr/xpg4/bin/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|/usr/xpg4/bin/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|/usr/xpg4/bin/awk -F "mount_path" '{print $2}'|cut -d '"' -f3)
RMANBACKUP_MOUNTPOINT=$(grep "$REMOTE_MOUNTPOINT" /etc/vfstab| sed 's/\s\+/ /g' |cut -d ' ' -f3)

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

ACTUAL_MOUNTPOINT=$(mount|grep -w "$REMOTE_MOUNTPOINT"| cut -d" " -f1 |head -1|sed 's/\s\+/ /g'  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
arch_backup () {

if [ ! -e $RMANBACKUP_MOUNTPOINT'/archivelogs' ]
then
    mkdir -p $RMANBACKUP_MOUNTPOINT'/archivelogs'
    if [ $? -eq 0 ];
    then
        echo "Created directory for archivelogs backup $RMANBACKUP_MOUNTPOINT/archivelogs"
    else
        echo "Oracle user does not have permission for $RMANBACKUP_MOUNTPOINT directory... exiting"
        exit 1
    fi
elif [ ! -d $RMANBACKUP_MOUNTPOINT'/archivelogs' ];
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'/archivelogs_backup_${ORACLE_SID}.log'
touch $RMANBACKUP_MOUNTPOINT/_workspace/
if [ $? -ne 0 ];
then
    echo "$RMANBACKUP_MOUNTPOINT directory is not writable.. exiting"
    exit 1
fi

# Checking if any of the Full/Diff backup is currently running and PID is active.
# In this case, we will fail/skip the current Log backup.
# Next trigger (external) of this script will retry log backup.

#LOCK_FILE=`echo ${RMANBACKUP_MOUNTPOINT}/.RMAN_FULL_lock_${PID}_${file_stamp}`

is_lock_file_present=`find ${RMANBACKUP_MOUNTPOINT} -type f -name ".RMAN_FULL_lock_*"|wc -l `
export BKP_CONTINUE=0
if [[ ${is_lock_file_present} -ne 0 ]]
then
        for _lock_file in `find ${RMANBACKUP_MOUNTPOINT} -type f -name ".RMAN_FULL_lock_*" | xargs `
        do
                _pid=`echo ${_lock_file}|cut -d"_" -f4`
                is_pid_proc_running=`ps -ef|grep ${_pid}|grep -v grep | wc -l`
                if [[ ${is_pid_proc_running} -ne 0 ]]
                then
                        echo "[INFO] Full backup Lock File: ${_lock_file} is present and PID is active"
                        export BKP_CONTINUE=1
                else
                        echo "[INFO] Full backup Lock File: ${_lock_file} is present and PID is NOT active"
                        echo "Deleting ${_lock_file}"
                        rm ${_lock_file}
                        if [ -e ${_lock_file} ]
                        then
                                echo "ERROR deleting file ${_lock_file}"
                                echo "Exiting Log backup"
                                export BKP_CONTINUE=1
                        fi
                fi
        done

        if [[ ${BKP_CONTINUE} -eq 1 ]]
        then
                echo "[WARNING] There is full backup running or the lock file for Full/diff backup is present"
                echo "[ERROR] Exiting the current Log backup"
                exit 5
        fi
fi

echo "Starting with RMAN backup"

rman log=$LOG_FILE_DIR'/archivelogs_backup_${ORACLE_SID}.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 controlfile autobackup format for device type disk to '${RMANBACKUP_MOUNTPOINT}/archivelogs/arch_controlfile_%d_%F.bkp';

run
{
sql 'alter system archive log current';
backup as backupset format '${RMANBACKUP_MOUNTPOINT}/archivelogs/%d_%h_%e_%s_%t.arc' archivelog all not backed up;

#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/archivelogs_backup_${ORACLE_SID}.log $LOG_FILE_DIR/archivelogs_backup_${ORACLE_SID}_$file_stamp.log
echo "RMAN log location: $LOG_FILE_DIR/archivelogs_backup_${ORACLE_SID}_$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/archivelogs_backup_${ORACLE_SID}_$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|/usr/xpg4/bin/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
arch_backup