diff --git a/bin/vhost-mysql-db-get.sh b/bin/vhost-mysql-db-get.sh new file mode 100755 index 0000000..99c4646 --- /dev/null +++ b/bin/vhost-mysql-db-get.sh @@ -0,0 +1,65 @@ +#!/bin/bash +# +# vhost-stack +# https://git.stack-source.com/msb/vhost-stack +# Copyright (c) 2023 Matthew Saunders Brown +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +# load include file +source $(dirname $0)/vhost.sh + +help() +{ + thisfilename=$(basename -- "$0") + echo "Get MySQL database connection information." + echo "" + echo "usage: $thisfilename [-d ] [-c] [-h]" + echo "" + echo " -h Print this help." + echo " -d Get MySQL DB info for specified domain, otherwise return DB info for all domains." + echo " -c CVS - Output in cvs format, instead of tabbed table." + exit +} + +vhost:getoptions "$@" + +# create newline var +NL=$'\n' + +if [[ -n $domain ]]; then + if [[ -d /srv/www/$domain ]]; then + virtualhostArray=($domain) + else + echo "ERROR: $domain not found" + exit 1 + fi +else + vhost::set-virtualhostArray +fi + +output="virtualhost hostname database username password" + +for v in "${virtualhostArray[@]}" +do + if [[ -f /srv/www/$v/.my.cnf ]]; then + + hostname=`grep ^host= /srv/www/$v/.my.cnf |cut -d = -f 2` + database=`grep ^database= /srv/www/$v/.my.cnf |cut -d = -f 2` + username=`grep ^user= /srv/www/$v/.my.cnf |cut -d = -f 2` + password=`grep ^password= /srv/www/$v/.my.cnf |cut -d = -f 2` + + output="$output${NL}$v $hostname $database $username $password" + + else + + output="$output${NL}$v" + + fi + +done + +if [[ $cvs ]]; then + echo "$output" | tr " " "," +else + echo "$output" | column -t +fi