Skip to content

Instantly share code, notes, and snippets.

@tomgidden
Last active November 17, 2015 14:26
Show Gist options
  • Save tomgidden/510cd63cf9ab4ddc1116 to your computer and use it in GitHub Desktop.
Save tomgidden/510cd63cf9ab4ddc1116 to your computer and use it in GitHub Desktop.
Invoke `mysql` command using parameters from Joomla configuration.php
#!/bin/bash
YELLOW="\033[1;33m"
GREEN="\033[1;32m"
RED="\033[1;31m"
NO_COLOUR="\033[0m"
DIR=$PWD
FN=
while [[ -n $DIR && $DIR != '/' ]]; do
if [[ -d $DIR/httpdocs && -f $DIR/httpdocs/configuration.php ]]; then
FN=$DIR/httpdocs/configuration.php
elif [[ -f $DIR/configuration.php ]]; then
FN=$DIR/configuration.php
fi
if [[ -n $FN ]]; then
BUF=$(php -r "ob_start();require('$FN');ob_end_clean();\$c=new JConfig();foreach (array('host','user','password','db','dbprefix') as \$k){print 'JMYSQL_'.\$k.'='.\$c->\$k.chr(10);}")
eval $BUF
## Uncomment if there are issues with localhost going to a UNIX socket.
## Alternatively add --protocol=TCP to the $CMD.
##
## A better solution is to use 'localhost' in your Joomla config
## for UNIX socket and '127.0.0.1' for TCP.
##
# [[ $JMYSQL_host == 'localhost' ]] && JMYSQL_host=127.0.0.1
CMD=mysql
CMD+=$(printf " -u%q" $JMYSQL_user)
CMD+=$(printf " -p%q" $JMYSQL_password)
CMD+=$(printf " -h%q" $JMYSQL_host)
CMD+=$(printf " %q" $JMYSQL_db)
echo
echo -e "${GREEN}Joomla Table prefix: ${YELLOW}$JMYSQL_dbprefix${NO_COLOUR}"
echo
echo -e "${RED}$CMD${NO_COLOUR}"
echo
eval $CMD
exit
fi
DIR=$(dirname "$DIR")
done
echo "configuration.php not found in current path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment