vmail-stack/bin/vmail-domains-get.sh
Matthew Saunders Brown 68708dd199 add ORDER BY domain
2022-08-03 10:57:48 -07:00

52 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# MIT License Copyright (c) 2021 Matthew Saunders Brown
# load include file
source $(dirname $0)/vmail.sh
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Get domain data from vmail database."
echo ""
echo "usage: $thisfilename [-d <domain>|-g <glob>] [OPTIONS]"
echo ""
echo " -h Print this help."
echo " -d Domain to be queried."
echo " -g Glob - Wildcard search instead of specific domain search."
echo " -c Output in cvs format."
echo " -t Use tabs instead of tables for output, do not display column headers."
echo ""
echo " Domain/Glob is optional. If nothing specified all domains will be queried."
echo " Domain search is for an exact match."
echo " Use -g instead of -d for glog (wildcard) search. e.g:"
echo " $thisfilename -g stack # search for all domains that contain 'stack'."
exit
}
vmail:getoptions "$@"
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
if [[ -n $tab ]]; then
dbcmdopts="-s -N $dbcmdopts"
fi
dbquery="SELECT domain, status, mbox_limit, (SELECT COUNT(*) FROM vm_mboxes WHERE vm_mboxes.domain_id = vm_domains.id) as mbox_allocated, mbox_quota_default, mbox_ratelimit_default FROM vm_domains"
# build query
if [[ -n $domain ]]; then
dbquery="$dbquery WHERE domain='$domain'"
elif [[ -n $glob ]]; then
dbquery="$dbquery WHERE domain LIKE '%$domain%'"
fi
# sort results by domain name
dbquery="$dbquery ORDER BY domain"
# execute mysql query
eval $dbcmd $dbcmdopts "\"$dbquery;\"" $cvs