vmail-stack/bin/vmail-domains-get.sh

53 lines
1.8 KiB
Bash
Raw Permalink Normal View History

2021-02-10 16:16:23 -08:00
#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
2022-08-22 13:34:20 -07:00
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
2021-02-10 16:16:23 -08:00
2021-04-02 12:02:50 -07:00
# load include file
source $(dirname $0)/vmail.sh
2021-02-10 16:16:23 -08:00
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Get domain data from vmail database."
echo ""
2021-10-15 15:17:29 -07:00
echo "usage: $thisfilename [-d <domain>|-g <glob>] [OPTIONS]"
2021-02-10 16:16:23 -08:00
echo ""
echo " -h Print this help."
2021-10-15 15:17:29 -07:00
echo " -d Domain to be queried."
echo " -g Glob - Wildcard search instead of specific domain search."
2023-11-27 09:13:59 -08:00
echo " -c Output in csv format."
2021-10-15 15:17:29 -07:00
echo " -t Use tabs instead of tables for output, do not display column headers."
2021-02-10 16:16:23 -08:00
echo ""
2021-10-15 15:17:29 -07:00
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'."
2021-02-10 16:16:23 -08:00
exit
}
2021-10-15 15:17:29 -07:00
vmail:getoptions "$@"
2021-02-10 16:16:23 -08:00
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-e"
2021-10-15 15:17:29 -07:00
if [[ -n $tab ]]; then
dbcmdopts="-s -N $dbcmdopts"
2021-02-10 16:16:23 -08:00
fi
2022-11-29 16:33:15 -08:00
dbquery="SELECT domain, status, mbox_limit, (SELECT COUNT(*) FROM vm_mboxes WHERE vm_mboxes.domain = vm_domains.domain) as mbox_allocated, mbox_quota_default, mbox_ratelimit_default from vm_domains"
2021-02-10 16:16:23 -08:00
# build query
2021-10-15 15:17:29 -07:00
if [[ -n $domain ]]; then
dbquery="$dbquery WHERE domain='$domain'"
elif [[ -n $glob ]]; then
2022-11-29 16:33:15 -08:00
dbquery="$dbquery WHERE domain LIKE '%$glob%'"
2021-02-10 16:16:23 -08:00
fi
2022-08-03 10:57:48 -07:00
# sort results by domain name
dbquery="$dbquery ORDER BY domain"
2021-02-10 16:16:23 -08:00
# execute mysql query
2023-11-27 09:13:59 -08:00
eval $dbcmd $dbcmdopts "\"$dbquery;\"" $csv