Last active
January 8, 2019 18:18
-
-
Save 0x2539/961f4dff8af201e9722465b9b63293fa to your computer and use it in GitHub Desktop.
A bash script for health checking a PostgreSQL. See the start of the script for instructions in how to run it.
This file contains 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 | |
# How to run: | |
# 1) `chmod +x health_checking_db.sh` | |
# 2) `./health_checking_db.sh --password=123 --user=my_user --db_name=my_db --port=5432 --host=localhost --timeout=60` | |
# Notes for mac: | |
# Install libpq for psql (https://stackoverflow.com/a/49689589/1651296): | |
# `brew install libpq` | |
# `export PATH=$PATH:/usr/local/Cellar/libpq/11.1/bin` | |
# Define a timestamp function | |
timestamp() { | |
date +"%s" | |
} | |
# Define a date function | |
dateString() { | |
date +"%T" | |
} | |
startTs=$(timestamp) | |
echo "started at $(dateString) ($(timestamp))" | |
for i in "$@" | |
do | |
case ${i} in | |
--password=*) | |
password="${i#*=}" | |
;; | |
--user=*) | |
user="${i#*=}" | |
;; | |
--db_name=*) | |
db_name="${i#*=}" | |
;; | |
--port=*) | |
port="${i#*=}" | |
;; | |
--host=*) | |
host="${i#*=}" | |
;; | |
--timeout=*) | |
timeout="${i#*=}" | |
;; | |
*) | |
echo "Unknown option ${i}" | |
echo "Ended at $(dateString) ($(timestamp)) (duration: $(($(timestamp)-${startTs})) seconds)" | |
exit -1 | |
;; | |
esac | |
done | |
if [[ -z "${password}" ]]; then | |
echo "password is missing, set it as environment variable or pass it as argument ('./health_checking_db.sh --password=123 --user=my_user --db_name=my_db --port=5432 --host=localhost --timeout=60' or 'export password=123')" | |
echo "Ended at $(dateString) ($(timestamp)) (duration: $(($(timestamp)-${startTs})) seconds)" | |
exit -1 | |
fi | |
if [[ -z "${user}" ]]; then | |
echo "user is missing, set it as environment variable or pass it as argument ('./health_checking_db.sh --password=123 --user=my_user --db_name=my_db --port=5432 --host=localhost --timeout=60' or 'export user=my_user')" | |
echo "Ended at $(dateString) ($(timestamp)) (duration: $(($(timestamp)-${startTs})) seconds)" | |
exit -1 | |
fi | |
if [[ -z "${db_name}" ]]; then | |
echo "db_name is missing, set it as environment variable or pass it as argument ('./health_checking_db.sh --password=123 --user=my_user --db_name=my_db --port=5432 --host=localhost --timeout=60' or 'export db_name=my_db')" | |
echo "Ended at $(dateString) ($(timestamp)) (duration: $(($(timestamp)-${startTs})) seconds)" | |
exit -1 | |
fi | |
if [[ -z "${port}" ]]; then | |
port=5432 | |
echo "port is missing, using default '${port}'. You can set it as environment variable or pass it as argument ('./health_checking_db.sh --password=123 --user=my_user --db_name=my_db --port=5432 --host=localhost --timeout=60' or 'export port=${port}')" | |
fi | |
if [[ -z "${host}" ]]; then | |
host="localhost" | |
echo "host is missing, using default '${host}'. You can set it as environment variable or pass it as argument ('./health_checking_db.sh --password=123 --user=my_user --db_name=my_db --port=5432 --host=localhost --timeout=60' or 'export host=${host}')" | |
fi | |
if [[ -z "${timeout}" ]]; then | |
timeout=60 | |
echo "timeout is missing, using default '${timeout}' seconds. You can set it as environment variable or pass it as argument ('./health_checking_db.sh --password=123 --user=my_user --db_name=my_db --port=5432 --host=localhost --timeout=60' or 'export timeout=${timeout}')" | |
fi | |
echo "Health checking for db: '${db_name}', with host: '${host}:${port}', with timeout: '${timeout}' seconds." | |
sleepTime=0 # do not sleep the first time we call the url | |
while : | |
do | |
# check if we exceeded the timeout | |
totalTimeSpent=$(($(timestamp)-${startTs})) | |
if [[ "$totalTimeSpent" -ge "$timeout" ]]; then | |
echo "Timeout Error! Didn't receive a success in ${timeout} seconds." | |
exit -1 | |
fi | |
# check if the db is created, a combination of: | |
# https://stackoverflow.com/a/17757560/1651296 | |
# https://stackoverflow.com/a/6405296/1651296 | |
# https://stackoverflow.com/a/15101962/1651296 | |
# https://www.postgresql.org/docs/current/libpq-envars.html | |
if [[ "$(PGPASSWORD=${password} PGHOST=${host} PGPORT=${port} PGUSER=${user} PGDATABASE=${db_name} psql -tAc "SELECT 1 FROM pg_database WHERE datname='${db_name}'" )" = '1' ]] | |
then | |
echo "Success!" | |
exit 0 | |
fi | |
echo "Waiting for result" | |
sleep ${sleepTime} | |
sleepTime=1 # before calling the url next time sleep for 1 second | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment