Redisign of whole script 'get_mail_domains.sh'.

This commit is contained in:
Christoph 2017-11-03 13:40:40 +01:00
parent c8de12dd2a
commit d13dd49877
2 changed files with 191 additions and 8 deletions

View File

@ -0,0 +1,40 @@
# ----------------------------------------------------
# ---
# - Parameter Settings for script 'get_mail_domains.sh'.
# ---
# ----------------------------------------------------
# - db_type
# -
# - Type of Postfix Database
# -
# - Possible values are 'pgsql' (PostgeSQL) or 'mysql' (MySQL)
# -
# - Defaults to: db_type="pgsql"
# -
#db_type="pgsql"
# - db_name
# -
# - Database name for the postfix database
# -
# - Defaults to: db_name="postfix"
# -
#db_name="postfix"
# - mysql_credential_args (root access to MySQL Database)
# -
# - Only used if db_type=mysql
# -
# - Example
# - mysql_credential_args="--login-path=local"
# - mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf" (Debian default)
# - mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
# -
# - Defaults to:
# - '/etc/mysql/debian.cnf' if MySQL is installed from debian package system
# - '/usr/local/mysql/sys-maint.cnf' otherwise
# -
#mysql_credential_args=""

View File

@ -1,18 +1,161 @@
#!/bin/sh #!/bin/sh
mysql=false script_dir="$(dirname $(realpath $0))"
mysql_credentilas="--login-path=local" conf_dir="${script_dir}/conf"
mysql_db=postfix conf_file="${conf_dir}/get_mail_domains.conf"
_date="$(date +%Y-%m-%d)"
tmp_dir="$(mktemp -d)"
log_messages="${tmp_dir}/get_mail_domains.log"
out_file="${script_dir}/log/mail-domains-${_date}.list"
#---------------------------------------
#-----------------------------
# Setting Defaults
#-----------------------------
#---------------------------------------
DEFAULT_db_type="pgsql"
DEFAULT_db_name="postfix"
#---------------------------------------
#-----------------------------
# Base Function(s)
#-----------------------------
#---------------------------------------
clean_up() {
# Perform program exit housekeeping
rm -rf $tmp_dir
exit $1
}
echononl(){
echo X\\c > /tmp/shprompt$$
if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then
echo "$*\\c" 1>&2
else
echo -e -n "$*" 1>&2
fi
rm /tmp/shprompt$$
}
warn (){
echo ""
echo -e " [ \033[33m\033[1mWarning\033[m ]: $*"
echo ""
}
error (){
echo ""
echo -e " [ \033[31m\033[1mError\033[m ]: $*"
echo ""
}
fatal(){
echo ""
echo -e "[ \033[31m\033[1mFehler\033[m ]: $*"
echo -e "\n Script was interupted!\n"
echo
clean_up 1
}
echo_ok() {
echo -e "\033[75G[ \033[32mok\033[m ]"
}
echo_failed() {
echo -e "\033[75G[ \033[1;31mfailed\033[m ]"
}
echo_skipped() {
echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]"
}
trap clean_up SIGHUP SIGINT SIGTERM
clear
echo ""
echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .."
clear
echo ""
echo -e "\033[32mRunning script \033[1m"$(basename $0)"\033[m .."
echo ""
echo "" echo ""
if $mysql ; then echo ""
domains=$(mysql $mysql_credentilas $mysql_db -N -s -e "SELECT domain FROM domain ORDER by domain") echononl " Loading default Configuration values from $(basename ${conf_file}).."
if [[ ! -f "$conf_file" ]]; then
echo_skipped
else else
domains=$(su - postgres -c"psql postfix -t -q -c\"SELECT domain FROM domain ORDER by domain\"") source "${conf_file}" > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fi
fi fi
echo -e "$domains"
if [[ "$db_type" != "pgsql" ]] && [[ "$db_type" != "mysql" ]]; then
fatal "Unknown Database Type '$db_type' for Password Database (Parameter db_type)"
fi
[[ -n "$db_name" ]] || db_name="$DEFAULT_db_name"
if [[ "$db_type" = "mysql" ]]; then
if [[ -z "$mysql_credential_args" ]]; then
if [[ -f "/etc/mysql/debian.cnf" ]]; then
mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf"
elif [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
else
fatal "No credentials for access to MySQL is given!"
fi
fi
fi
echo "" echo ""
echononl " Create output directory '$(dirname "$out_file")'.."
if [[ ! -d "$(dirname "$out_file")" ]] ; then
mkdir "$(dirname "$out_file")"
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
fi
else
echo_skipped
fi
echo ""
echononl " Collect supported domains at this server.."
if $mysql ; then
domains=$(mysql $mysql_credential_args "$db_name" -N -s -e \
"SELECT domain FROM domain ORDER by domain" 2> "$log_messages")
else
domains=$(su - postgres -c"psql \"$db_name\" -t -q -c\"SELECT domain FROM domain ORDER by domain\"")
fi
if [[ $? -eq 0 ]]; then
echo_ok
else
echo_failed
error "$(cat "$log_messages")"
clean_up 1
fi
exit 0 echo -e "\n\n \033[37m\033[1mMail Domains supported by this server ("$(hostname -f)"):\033[m\n"
echo "Mail Domains supported by this server ("$(hostname -f)"):" > $out_file
echo "" >> $out_file
for domain in $domains ; do
[[ "$domain" = "ALL" ]] && continue
echo -e " \033[32m$domain\033[m"
echo " $domain" >> $out_file
done
echo "" >> $out_file
echo ""
clean_up 0