Last active
October 4, 2022 16:17
-
-
Save iversond/e6f8fd7ffd7169d05908327f1028ea47 to your computer and use it in GitHub Desktop.
Merging scripts from https://reguchi.wordpress.com/2020/02/11/oci-file-storage-snapshot-management/ into one. Using with Rundeck to create snapshots.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# shellcheck disable=2059,2154,2034,2155,2046,2086 | |
#=============================================================================== | |
# vim: softtabstop=2 shiftwidth=2 expandtab fenc=utf-8 spelllang=en ft=sh | |
#=============================================================================== | |
# | |
# FILE: oci_fss_snapshots.sh | |
# | |
# USAGE: ./oci_fss_snapshots.sh | |
# | |
# DESCRIPTION: Create OCI FSS snapshot and cleanup older snapshots. | |
# | |
#=============================================================================== | |
set -e # Exit immediately on error | |
set -u # Treat unset variables as an error | |
set -o pipefail # Prevent errors in a pipeline from being masked | |
IFS=$'\n\t' # Set the internal field separator to a tab and newline | |
############### | |
# Variables # | |
############### | |
export DEBUG=true | |
declare -A timings | |
export FSSOCID=<OCID> # Comment | |
export TSTAMP=`date +%Y%m%d` | |
export ENV=ps | |
export KEEP=42 | |
############### | |
# Functions # | |
############### | |
function echoinfo() { | |
local GC="\033[1;32m" | |
local EC="\033[0m" | |
printf "${GC} ☆ INFO${EC}: %s${GC}\n" "$@"; | |
} | |
function echodebug() { | |
local BC="\033[1;34m" | |
local EC="\033[0m" | |
local GC="\033[1;32m" | |
if [[ -n ${DEBUG+x} ]]; then | |
printf "${BC} ★ DEBUG${EC}: %s${GC}\n" "$@"; | |
fi | |
} | |
function echoerror() { | |
local RC="\033[1;31m" | |
local EC="\033[0m" | |
printf "${RC} ✖ ERROR${EC}: %s\n" "$@" 1>&2; | |
} | |
function display_timings_summary() { | |
local divider='==============================' | |
divider=$divider$divider | |
local header="\n %-28s %s\n" | |
local format=" %-28s %s\n" | |
local width=40 | |
local total_duration=0 | |
for duration in "${timings[@]}"; do | |
total_duration=$((duration + total_duration)) | |
done | |
printf "$header" "TASK" "DURATION" | |
printf "%$width.${width}s\n" "$divider" | |
for key in "${!timings[@]}"; do | |
local converted_timing=$(date -u -d @${timings[$key]} +"%T") | |
printf "$format" "$key" "${converted_timing}" | |
done | |
printf "%$width.${width}s\n" "$divider" | |
printf "$format" "TOTAL TIME:" $(date -u -d @${total_duration} +"%T") | |
printf "\n" | |
} | |
function create_snapshot(){ | |
local begin=$(date +%s) | |
echoinfo "Creating Snapshot" | |
echodebug "" | |
if [[ -n ${DEBUG+x} ]]; then | |
oci fs snapshot create --file-system-id=$FSSOCID --name=$ENV.$TSTAMP | |
else | |
oci fs snapshot create --file-system-id=$FSSOCID --name=$ENV.$TSTAMP > /dev/null 2>&1 | |
fi | |
local end=$(date +%s) | |
local tottime="$((end - begin))" | |
timings[create_snapshot]=$tottime | |
} | |
function cleanup_snapshots(){ | |
local begin=$(date +%s) | |
echoinfo "Cleaning up Snapshots" | |
oci fs snapshot list --file-system-id=$FSSOCID | grep '"id"' | awk '{print $2}' | sed 's/"//g' | sed 's/,//g' > /tmp/fss.bkp | |
CT=`cat /tmp/fss.bkp | wc -l` | |
if [ "$CT" -gt $KEEP ]; then | |
DIFF=$(expr $CT - $KEEP) | |
for id in `tail -$DIFF /tmp/fss.bkp` | |
do | |
oci fs snapshot delete --force --snapshot-id $id | |
done | |
else | |
echoinfo "Current snapshot count ${CT} is then ${KEEP}" | |
fi | |
local end=$(date +%s) | |
local tottime="$((end - begin))" | |
timings[cleanup_snapshots]=$tottime | |
} | |
######## | |
# Main # | |
######## | |
create_snapshot | |
cleanup_snapshots | |
display_timings_summary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment