vhost-stack/bin/vhost-mysql-db-get.sh

66 lines
1.5 KiB
Bash
Raw Normal View History

2023-06-15 17:11:42 -07:00
#!/bin/bash
#
# vhost-stack
# https://git.stack-source.com/msb/vhost-stack
# Copyright (c) 2023 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# 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 <domain>] [-c] [-h]"
echo ""
echo " -h Print this help."
echo " -d <domain> 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