Files
llb-oracle/rsc_oracle_jobs.sh
T
2026-04-20 15:04:21 +01:00

164 lines
3.7 KiB
Bash
Executable File

#!/bin/bash
#
# Checks for running Oracle backup-related jobs for an Oracle database
#
usage() { echo "Usage: $0 [filter]" >&2; exit 1; }
if [[ -z "$1" ]]; then
usage
fi
MYDIR="$(dirname "$(realpath "$0")")"
source $MYDIR/rsc_ops.sh
trap cleanup EXIT
pretty_job_type() {
case "$1" in
Backup)
echo "DB Snapshot"
;;
LOG_BACKUP)
echo "Log Backup"
;;
*)
echo "$1"
;;
esac
}
format_local_time() {
local timestamp="$1"
local normalized_timestamp
local epoch
if [[ -z "$timestamp" || "$timestamp" == "null" ]]; then
echo "start time unavailable"
return
fi
normalized_timestamp="${timestamp%%.*}Z"
if [[ "$DATE" == "gdate" ]]; then
epoch=$(gdate -u -d "$normalized_timestamp" +%s 2>/dev/null)
else
epoch=$(TZ=UTC date -j -f "%Y-%m-%dT%H:%M:%SZ" "$normalized_timestamp" "+%s" 2>/dev/null)
fi
if [[ -z "$epoch" ]]; then
echo "$timestamp"
return
fi
if [[ "$DATE" == "gdate" ]]; then
gdate -d "@$epoch" "+%Y-%m-%d %H:%M:%S %Z"
else
date -r "$epoch" "+%Y-%m-%d %H:%M:%S %Z"
fi
}
gql_DBListQuery='query OracleDatabases($filter: [Filter!]) {
oracleDatabases(filter: $filter) {
nodes {
dbUniqueName
id
}
}
}'
variables="{
\"filter\": [
{
\"texts\": [\"$1\"],
\"field\": \"NAME_EXACT_MATCH\"
},
{
\"texts\": [\"false\"],
\"field\": \"IS_RELIC\"
},
{
\"texts\": [\"false\"],
\"field\": \"IS_REPLICATED\"
}
]
}"
gqlQuery="$(echo $gql_DBListQuery)"
gqlVars="$(echo $variables)"
rsc_gql_query
dbid=$(cat /tmp/rbkresponse.$$ | jq -r '.data.oracleDatabases.nodes[] | .id')
db_name=$(cat /tmp/rbkresponse.$$ | jq -r '.data.oracleDatabases.nodes[] | .dbUniqueName')
dbid_count=$(echo "$dbid" | grep -c .)
if [[ "$dbid_count" -ne 1 || -z "$dbid" ]]; then
log_error "Expected exactly one database match! found:"
cat /tmp/rbkresponse.$$ | jq -r '.data.oracleDatabases.nodes[] | .dbUniqueName'
exit 4
fi
log_info "DB ID is $dbid"
gqlListJobs='query ListJobs($filters: ActivitySeriesFilter) {
activitySeriesConnection(filters: $filters) {
nodes {
fid
objectName
objectType
startTime
activitySeriesId
isOnDemand
progress
lastActivityType
}
}
}'
variables="{
\"filters\": {
\"objectFid\": [
\"$dbid\"
],
\"lastActivityStatus\": \"RUNNING\",
\"lastActivityType\": [\"LOG_BACKUP\",\"BACKUP\"]
}
}"
gqlQuery="$(echo $gqlListJobs)"
gqlVars="$(echo $variables)"
rsc_gql_query
job_count=$(cat /tmp/rbkresponse.$$ | jq -r '.data.activitySeriesConnection.nodes | length')
if [[ "$job_count" -eq 0 ]]; then
echo "No running backup jobs found for $db_name"
exit 0
fi
echo "Running backup jobs for $db_name:"
while IFS= read -r job_json; do
if [[ -z "$job_json" ]]; then
continue
fi
last_activity_type=$(echo "$job_json" | jq -r '.lastActivityType // empty')
progress=$(echo "$job_json" | jq -r '.progress // empty')
start_time=$(echo "$job_json" | jq -r '.startTime // empty')
if [[ -z "$last_activity_type" ]]; then
continue
fi
job_type=$(pretty_job_type "$last_activity_type")
local_start_time=$(format_local_time "$start_time")
if [[ -z "$progress" || "$progress" == "null" ]]; then
progress_display="progress unavailable"
else
progress_display="$progress%"
fi
printf ' %-12s %-20s started %s\n' "$job_type" "$progress_display" "$local_start_time"
done < <(cat /tmp/rbkresponse.$$ | jq -c '.data.activitySeriesConnection.nodes[]')
exit 1