Skip to content

Instantly share code, notes, and snippets.

@aneveux
Last active September 16, 2020 14:33
Show Gist options
  • Save aneveux/984a1db72915249552b5a0d7af5d6e9e to your computer and use it in GitHub Desktop.
Save aneveux/984a1db72915249552b5a0d7af5d6e9e to your computer and use it in GitHub Desktop.
Noob-level shell script allowing dumping a pgsql database running on an openshift pod in a file.
#!/bin/sh
# oc-postgre-backup
# creates a backup file for a postgre database running on an openshift pod
# ------------------
# requires:
# - oc - openshift client, allowing connecting to the postgre pod
# - pg_dump - part of the postgresql package, allowing creating the dump file from the database
# ------------------
# arguments:
# - url - url of the openshift instance
# - token - authentication token to use for connecting to openshift
# - namespace - openshift namespace in which the database pod is running
# - pod - pod name in which the postgre database is running (not actual name, it'll be grepped)
# - username - username to be used for connecting to the database
# - dbname - name of the database to backup
# - oc - (optional) path to the oc binary
# - proxyHost - (optional) host of the proxy to use
# - proxyPort - (optional) port of the proxy to use
# - noProxy - (optional) list of non proxy hosts
# - verbose - (optional) writes more logs
# - help - (optional) displays help information about the program
# - debug - (optional) displays values retrieved from all parameters
# ------------------
# Execution example:
# ./oc-postgre-backup --url openshift_url --token your_token --namespace your_namespace --pod postgre --username your_username --dbname your_db --oc /home/user/tools/oc --proxyHost http://someProxy --proxyPort 1234 --noProxy localhost,127.0.0.1 --verbose --help --debug
# ------------------
# Author: [email protected]
# Version: 0.1
# ------------------
# Helper functions
# ------------------
log() {
if [[ -n $VERBOSE ]]; then
echo "$1"
fi
}
# ------------------
# Welcome!
# ------------------
echo "[oc-postgre-backup] - Hello!"
echo "Parsing all arguments... (use --debug to check the arguments used)"
# ------------------
# Arguments Parsing
# ------------------
REMAINING=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--url)
URL="$2"
shift # past argument
shift # past value
;;
--token)
TOKEN="$2"
shift # past argument
shift # past value
;;
--namespace)
NAMESPACE="$2"
shift # past argument
shift # past value
;;
--pod)
POD="$2"
shift # past argument
shift # past value
;;
--username)
USERNAME="$2"
shift # past argument
shift # past value
;;
--dbname)
DBNAME="$2"
shift # past argument
shift # past value
;;
--oc)
OC="$2"
shift # past argument
shift # past value
;;
--proxyHost)
PROXYHOST="$2"
shift # past argument
shift # past value
;;
--proxyPort)
PROXYPORT="$2"
shift # past argument
shift # past value
;;
--noProxy)
NOPROXY="$2"
shift # past argument
shift # past value
;;
--verbose)
VERBOSE=true
shift # past argument
;;
--help)
HELP=true
shift # past argument
;;
--debug)
DEBUG=true
shift # past argument
;;
*) # unknown option
REMAINING+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${REMAINING[@]}" # restore positional parameters
# ------------------
# Displaying debug information if needed
# ------------------
if [[ -n $DEBUG ]]; then
echo "------------------"
echo "oc-postgre-backup [DEBUG]"
echo "------------------"
echo "URL = ${URL}"
echo "TOKEN = ${TOKEN}"
echo "NAMESPACE = ${NAMESPACE}"
echo "POD = ${POD}"
echo "USERNAME = ${USERNAME}"
echo "DBNAME = ${DBNAME}"
echo "OC = ${OC}"
echo "PROXYHOST = ${PROXYHOST}"
echo "PROXYPORT = ${PROXYPORT}"
echo "NOPROXY = ${NOPROXY}"
echo "VERBOSE = ${VERBOSE}"
echo "HELP = ${HELP}"
echo "DEBUG = ${DEBUG}"
echo "------------------"
fi
# ------------------
# Validating mandatory parameters are provided
# ------------------
log "Checking if [url] parameter was provided..."
if ! test -n "$URL"; then
echo "ERROR! [url] parameter is mandatory!"
exit
fi
log "[url] is found!"
log "Checking if [token] parameter was provided..."
if ! test -n "$TOKEN"; then
echo "ERROR! [token] parameter is mandatory!"
exit
fi
log "[token] is found!"
log "Checking if [namespace] parameter was provided..."
if ! test -n "$NAMESPACE"; then
echo "ERROR! [namespace] parameter is mandatory!"
exit
fi
log "[namespace] is found!"
log "Checking if [pod] parameter was provided..."
if ! test -n "$POD"; then
echo "ERROR! [pod] parameter is mandatory!"
exit
fi
log "[pod] is found!"
log "Checking if [username] parameter was provided..."
if ! test -n "$USERNAME"; then
echo "ERROR! [username] parameter is mandatory!"
exit
fi
log "[username] is found!"
log "Checking if [dbname] parameter was provided..."
if ! test -n "$DBNAME"; then
echo "ERROR! [dbname] parameter is mandatory!"
exit
fi
log "[dbname] is found!"
# ------------------
# Validating thirdparties are installed
# ------------------
log "Validating thirdparties are installed..."
log "Searching for pg_dump..."
if ! command -v pg_dump &> /dev/null
then
echo "[pg_dump] is required, please install it."
exit
fi
log "[pg_dump] found!"
log "Searching for oc..."
if [[ -n $OC ]]; then
log "Searching for custom oc installation..."
if ! command -v $OC &> /dev/null
then
echo "[${OC}] can't be found, please check the parameter you provided."
exit
else
OCBINARY=$OC
fi
else
log "Searching for oc..."
if ! command -v oc &> /dev/null
then
echo "[oc] is required, please install it."
exit
else
OCBINARY=oc
fi
fi
log "[oc] found!"
log "Using ${OCBINARY}."
# ------------------
# Configuring proxy
# ------------------
log "Checking if proxy needs to be configured..."
if [[ -n $PROXYHOST ]]; then
log "Configuring proxy with provided information..."
log "Storing backup of actual proxy configuration..."
BAK_PROXY=$PROXY
BAK_HTTP_PROXY=$HTTP_PROXY
BAK_HTTPS_PROXY=$HTTPS_PROXY
BAK_NO_PROXY=$NO_PROXY
log "Backup of [${BAK_PROXY} / ${BAK_HTTP_PROXY} / ${BAK_HTTPS_PROXY} / ${BAK_NO_PROXY}] done."
log "Setting up provided proxy configuration..."
export PROXY="${PROXYHOST}:${PROXYPORT}"
export HTTP_PROXY=$PROXY
export HTTPS_PROXY=$PROXY
export proxy=$PROXY
export http_proxy=$PROXY
export https_proxy=$PROXY
if [[ -n $NOPROXY ]]; then
export NO_PROXY=$NOPROXY
export no_proxy=$NO_PROXY
fi
log "Proxy configured!"
log "PROXY = ${PROXY}"
log "NO_PROXY = ${NO_PROXY}"
fi
# ------------------
# Searching for database pod
# ------------------
log "Connecting to openshift cluster..."
$OCBINARY login https://$URL --insecure-skip-tls-verify --token=$TOKEN > /dev/null
log "Connected!"
log "Connecting to namespace..."
$OCBINARY project "${NAMESPACE}" > /dev/null
log "Connected!"
log "Identifying pod name from provided pattern (${POD})"
PODNAME=`$OCBINARY get pods -o custom-columns=POD:.metadata.name --no-headers | grep "${POD}"`
log "Found pod running the database: ${PODNAME}!"
# ------------------
# Setting up port-forwarding
# ------------------
log "Setting up port-forwarding to the database..."
$OCBINARY port-forward $PODNAME 15432:5432 > /dev/null &
OCPID=`pidof $OCBINARY`
log "Port forwarding now running at [${OCPID}]"
sleep 5 # waiting a bit for port-forwarding to be effective. it is mandatory.
# ------------------
# Dumping the database to a file
# ------------------
FILENAME="dump_$(date +%Y%m%d_%H%M%S).sql"
log "Dumping the database content in [${FILENAME}]..."
pg_dump --host=localhost --port=15432 --username=$USERNAME --dbname=$DBNAME --format=p --file=./$FILENAME > /dev/null
DUMP_STATUS=$?
if [ $DUMP_STATUS -ne "0" ]; then
echo "ERROR! [pg_dump] exited with code [${DUMP_STATUS}]."
fi
log "Done!"
# ------------------
# Killing the port-forwarding before leaving
# ------------------
log "Killing port-forwarding before leaving..."
kill $OCPID
log "Killed!"
# ------------------
# Restoring proxy configuration if needed
# ------------------
if [[ -n $BAK_PROXY ]]; then
log "Restoring proxy configuration back to normal"
export PROXY=$BAK_PROXY
export HTTP_PROXY=$BAK_HTTP_PROXY
export HTTPS_PROXY=$BAK_HTTPS_PROXY
export NO_PROXY=$BAK_NO_PROXY
export proxy=$PROXY
export http_proxy=$HTTP_PROXY
export https_proxy=$HTTPS_PROXY
export no_proxy=$NO_PROXY
log "Proxy restored!"
fi
# ------------------
# Restoring proxy configuration if needed
# ------------------
echo "[oc-postgre-backup] - My job is done!"
exit $DUMP_STATUS # We use the return of pg_dump as exit code to indicate an error if dump is not successful.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment