Files
db-backup/scripts/dburl-parser.sh

41 lines
1.0 KiB
Bash
Executable File

#! /bin/sh
#
# Parses a database URL in the format schema://user:
#
DB_URL="$1";
SCHEMA="${DB_URL%//*}";
SCHEMALESS_URL=${DB_URL#*//};
LEFT_HAND=${SCHEMALESS_URL%@*};
if test ! ${LEFT_HAND} = ${SCHEMALESS_URL}; then
USERNAME=$(echo ${LEFT_HAND} | cut -d ':' -f 1);
if test ! ${USERNAME} = ${LEFT_HAND}; then
PASSWORD=$(echo ${LEFT_HAND} | cut -d ':' -f 2);
fi
fi
RIGHT_HAND=${SCHEMALESS_URL#*@};
HOST=$(echo ${RIGHT_HAND} | cut -d ':' -f 1 | cut -d '/' -f 1);
if test -n "$(echo ${RIGHT_HAND} | grep -o ':')"; then
PORT=$(echo ${RIGHT_HAND} | cut -d ':' -f 2 | cut -d '/' -f 1);
fi
if test -n "$(echo ${RIGHT_HAND} | grep -o '/')"; then
DATABASE=$(echo ${RIGHT_HAND} | cut -d ':' -f 2 | cut -d '/' -f 2);
fi
# throw an error if DB_URL does not contains at least a HOST
if test -z "${HOST}"; then
echo "database URL providen was invalid" && exit 1;
fi
cat << HEREDOC
DB_SCHEMA=${SCHEMA%:}
DB_USERNAME=${USERNAME}
DB_PASSWORD=${PASSWORD}
DB_HOST=${HOST}
DB_PORT=${PORT}
DB_NAME=${DATABASE}
HEREDOC