Files
nokia-hpux-oracle/oracle_funcs.sh
2025-10-24 16:56:57 +01:00

305 lines
9.4 KiB
Bash
Executable File

#!/bin/bash
#
# Oracle shell script support functions
# Written for Avon
# v1.0 - James Pattinson - October 2023
nowait=0
LOGFILE=$API_LOG_DIR/api_calls.log
mkdir -p $API_LOG_DIR
tabwidth=25
HOST=$(hostname -s)
os=$(uname -o)
if [ $os == "Solaris" ]; then
# Use the GNU version of date binary
DATE=/usr/gnu/bin/date
else
DATE=$(which date)
fi
echo "`$DATE` -$$-: CALLED $0 $@" >> $LOGFILE
trap ctrl_c INT
function ctrl_c () {
echo "`$DATE` -$$-: TRAPPED CTRL-C - EXITING" >> $LOGFILE
exit_with_error
}
function ctrl_c_inhibit () {
echo "`$DATE` -$$-: TRAPPED CTRL-C - CONTINUING" >> $LOGFILE
}
exit_with_error () {
# if [ $usingsatoken ]; then
# ENDPOINT="https://$RUBRIK_IP/api/v1/session/me"
# rest_api_delete
# check_http_error
# fi
cat /tmp/rbkresponse.$$ | mailx -s "Backup error on ${HOST} for ${ORACLE_SID}. Please investigate" $ALERT_EMAILS
rm -f /tmp/rbkresponse.$$
rm -f /tmp/mountedDBs.$$
rm -f $PIDFILE
echo Aborting Script!
echo "`$DATE` -$$-: EXITED WITH ERROR $0 $@" >> $LOGFILE
exit 1
}
check_http_error () {
# All good responses start with a 2
if [ ${http_response:0:1} != "2" ]; then
echo FATAL: HTTP error from API call: $http_response. The server responded with:
cat /tmp/rbkresponse.$$ ; echo ; exit_with_error
fi
}
check_pid () {
if [ -f $PIDFILE ]
then
PID=$(cat $PIDFILE)
ps -p $PID > /dev/null 2>&1
if [ $? -eq 0 ]
then
echo "ERROR: MV already being used by process ID $PID"
ps -fp $PID
exit_with_error
else
## Process not found assume not running
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "ERROR: Could not create mvLock file"
exit_with_error
fi
fi
else
echo $$ > $PIDFILE
if [ $? -ne 0 ]
then
echo "ERROR: Could not create mvLock file"
exit_with_error
fi
fi
}
check_get_token () {
if [ -z "${AUTH_TOKEN}" ]; then
# RSC
id_string=$(echo $ID | cut -d\| -f 2)
# CDM
id_string=$(echo $ID | cut -d: -f 4)
if [ -f ~/.rbksession.$id_string ]; then
echo "`$DATE` -$$-: AUTH SESSION FILE EXISTS" >> $LOGFILE
read expiration token < <(echo $(cat ~/.rbksession.$id_string))
if [ $($DATE +%s -u -d $expiration) -lt $(( $($DATE +%s) + 1800 )) ]; then
echo "`$DATE` -$$-: AUTH TOKEN EXPIRED" >> $LOGFILE
get_token
else
AUTH_TOKEN=$token
fi
else
get_token
fi
fi
}
get_token () {
trap '' INT
echo "`$DATE` -$$-: AUTH USER $ID" >> $LOGFILE
MYENDPOINT="https://$RUBRIK_IP/api/v1/service_account/session"
MYPAYLOAD="{\"serviceAccountId\":\"$ID\",\"secret\":\"$SECRET\"}"
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X POST $MYENDPOINT -H "accept: application/json" -H "Content-Type: application/json" -d $MYPAYLOAD)
check_http_error
AUTH_TOKEN=$(grep -Eo '"token"[^,]*' /tmp/rbkresponse.$$ | grep -Eo '[^:]*$' | sed 's/\"//g')
SESSION=$(grep -Eo '"sessionId"[^,]*' /tmp/rbkresponse.$$ | grep -Eo '[^:]*$' | sed 's/\"//g')
EXPIRATION=$(grep -Eo '"expirationTime"[^,]*' /tmp/rbkresponse.$$ | cut -d: -f 2-4 | sed 's/\"//g' | sed 's/.000Z//;s/T/Z/')
echo "`$DATE` -$$-: AUTH SESSION $SESSION" >> $LOGFILE
echo "`$DATE` -$$-: SAVING TOKEN TO FILE" >> $LOGFILE
echo "$EXPIRATION $AUTH_TOKEN" > ~/.rbksession.$id_string
usingsatoken=1
trap ctrl_c INT
}
# HTTP GET: Given $ENDPOINT write output to file
rest_api_get () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X GET $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN")
echo "`$DATE` -$$-: REST API GET: ENDPOINT $ENDPOINT" >> $LOGFILE
echo "`$DATE` -$$-: REST API GET: RESPONSE STARTS" >> $LOGFILE
cat /tmp/rbkresponse.$$ >> $LOGFILE
echo >> $LOGFILE
echo "`$DATE` -$$-: REST API GET: RESPONSE ENDS" >> $LOGFILE
check_http_error
}
rest_api_get_2 () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse2.$$ -w "%{http_code}" -X GET $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN")
echo "`$DATE` -$$-: REST API GET: ENDPOINT $ENDPOINT" >> $LOGFILE
echo "`$DATE` -$$-: REST API GET: RESPONSE STARTS" >> $LOGFILE
cat /tmp/rbkresponse2.$$ >> $LOGFILE
echo >> $LOGFILE
echo "`$DATE` -$$-: REST API GET: RESPONSE ENDS" >> $LOGFILE
check_http_error
}
# HTTP POST: Given $ENDPOINT and $PAYLOAD write output to file
rest_api_post () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X POST $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json" -d $PAYLOAD)
echo "`$DATE` -$$-: REST API POST: ENDPOINT $ENDPOINT" >> $LOGFILE
echo "`$DATE` -$$-: REST API POST: PAYLOAD $PAYLOAD" >> $LOGFILE
echo "`$DATE` -$$-: REST API POST: RESPONSE STARTS" >> $LOGFILE
cat /tmp/rbkresponse.$$ >> $LOGFILE
echo >> $LOGFILE
echo "`$DATE` -$$-: REST API POST: RESPONSE ENDS" >> $LOGFILE
check_http_error
}
rest_api_post_empty () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X POST $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json")
echo "`$DATE` -$$-: REST API POST: ENDPOINT $ENDPOINT" >> $LOGFILE
echo "`$DATE` -$$-: REST API POST: PAYLOAD <NULL>" >> $LOGFILE
echo "`$DATE` -$$-: REST API POST: RESPONSE STARTS" >> $LOGFILE
cat /tmp/rbkresponse.$$ >> $LOGFILE
echo >> $LOGFILE
echo "`$DATE` -$$-: REST API POST: RESPONSE ENDS" >> $LOGFILE
check_http_error
}
rest_api_patch () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X PATCH $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN" -H "Content-Type: application/json" -d "$PAYLOAD")
echo "`$DATE` -$$-: REST API PATCH: ENDPOINT $ENDPOINT" >> $LOGFILE
echo "`$DATE` -$$-: REST API PATCH: PAYLOAD $PAYLOAD" >> $LOGFILE
echo "`$DATE` -$$-: REST API PATCH: RESPONSE STARTS" >> $LOGFILE
cat /tmp/rbkresponse.$$ >> $LOGFILE
echo >> $LOGFILE
echo "`$DATE` -$$-: REST API PATCH: RESPONSE ENDS" >> $LOGFILE
check_http_error
}
rest_api_delete () {
check_get_token
http_response=$(curl -s -k -o /tmp/rbkresponse.$$ -w "%{http_code}" -X DELETE $ENDPOINT -H "accept: application/json" -H "Authorization: Bearer $AUTH_TOKEN")
echo "`$DATE` -$$-: REST API DELETE: $http_response: ENDPOINT $ENDPOINT" >> $LOGFILE
check_http_error
}
get_mv () {
ENDPOINT="https://$RUBRIK_IP/api/internal/managed_volume?name=$mv_name&is_relic=false"
rest_api_get
mvId=$(grep -Eo '"id"[^,]*' /tmp/rbkresponse.$$ | grep -Eo 'ManagedVolume:::[a-z0-9-]*')
numChannels=$(grep -Eo '"numChannels"[^,]*' /tmp/rbkresponse.$$ | cut -d: -f2)
if [[ $mvId == "" ]]; then
echo ERROR: MV with name $mv_name was not found
exit_with_error
fi
}
get_data_mv () {
mv_name=${HOST}_${ORACLE_SID}_data
ENDPOINT="https://$RUBRIK_IP/api/internal/managed_volume?name=$mv_name&is_relic=false"
rest_api_get
mvId=$(grep -Eo '"id"[^,]*' /tmp/rbkresponse.$$ | grep -Eo 'ManagedVolume:::[a-z0-9-]*')
numChannels=$(grep -Eo '"numChannels"[^,]*' /tmp/rbkresponse.$$ | cut -d: -f2)
if [[ $mvId == "" ]]; then
echo ERROR: MV with name $mv_name was not found
exit_with_error
fi
}
get_log_mv () {
# Look for a log volume. If not present, return the data volume
mv_name=${HOST}_${ORACLE_SID}_log
ENDPOINT="https://$RUBRIK_IP/api/internal/managed_volume?name=$mv_name&is_relic=false"
rest_api_get
logMvId=$(grep -Eo '"id"[^,]*' /tmp/rbkresponse.$$ | grep -Eo 'ManagedVolume:::[a-z0-9-]*')
if [[ $logMvId == "" ]]; then
echo "INFO: Log volume ($mv_name) not found. Logs will be written to DB volume"
logMvPresent=0
mv_name=${HOST}_${ORACLE_SID}_data
ENDPOINT="https://$RUBRIK_IP/api/internal/managed_volume?name=$mv_name&is_relic=false"
rest_api_get
mvId=$(grep -Eo '"id"[^,]*' /tmp/rbkresponse.$$ | grep -Eo 'ManagedVolume:::[a-z0-9-]*')
numChannels=$(grep -Eo '"numChannels"[^,]*' /tmp/rbkresponse.$$ | cut -d: -f2)
if [[ $mvId == "" ]]; then
echo ERROR: MV with name $mv_name was not found
exit_with_error
fi
else
mvId=$(grep -Eo '"id"[^,]*' /tmp/rbkresponse.$$ | grep -Eo 'ManagedVolume:::[a-z0-9-]*')
numChannels=$(grep -Eo '"numChannels"[^,]*' /tmp/rbkresponse.$$ | cut -d: -f2)
logMvPresent=1
echo "INFO: Log volume ($mv_name) exists with $numChannels channels"
fi
}
open_mv () {
PIDFILE=/tmp/mvLock_${mv_name}.pid
check_pid
ENDPOINT="https://$RUBRIK_IP/api/internal/managed_volume/$mvId/begin_snapshot"
echo "Opening MV $mv_name with ID $mvId (numChannels is $numChannels)"
rest_api_post_empty
}
close_mv () {
if [ -z "${SLANAME}" ]; then
echo Closing MV $mv_name using default assigned SLA
ENDPOINT="https://$RUBRIK_IP/api/internal/managed_volume/$mvId/end_snapshot"
rest_api_post_empty
else
get_sla
echo Closing MV $mv_name as On Demand with SLA $SLANAME
ENDPOINT="https://$RUBRIK_IP/api/internal/managed_volume/$mvId/end_snapshot"
PAYLOAD="{\"retentionConfig\":{\"slaId\":\"$slaId\"}}"
rest_api_post
fi
}
cleanup () {
# if [ $usingsatoken ]; then
# ENDPOINT="https://$RUBRIK_IP/api/v1/session/me"
# rest_api_delete
# fi
echo "`$DATE` -$$-: EXITED $0 $@" >> $LOGFILE
rm -f /tmp/mountedDBs.$$
rm -f /tmp/rbkresponse.$$
rm -f $PIDFILE
}