From 3083dc6e29724942c1b1ccc965ca797b3a80d1d7 Mon Sep 17 00:00:00 2001 From: Matthew Saunders Brown Date: Thu, 27 Feb 2025 15:46:37 -0800 Subject: [PATCH] do mysql connection check for all scripts, found out hard way vmail-purge-dirs.sh wipes out all email data if it can not connect to mysql --- bin/vmail.sh | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/bin/vmail.sh b/bin/vmail.sh index 1ce089b..abcad67 100755 --- a/bin/vmail.sh +++ b/bin/vmail.sh @@ -30,29 +30,24 @@ else fi fi -# Only Debian-exim commands do not require MySQL running, (almost) all others do: -if [[ "$USER" != "Debian-exim" ]]; then +# check that MYSQL_CONNECTION_INFO_FILE exists and is readable +if [ ! -f "$MYSQL_CONNECTION_INFO_FILE" ]; then + echo "ERROR: MySQL connection info file ($MYSQL_CONNECTION_INFO_FILE) does not exist or is not readable." + exit 1 +fi - # check that MYSQL_CONNECTION_INFO_FILE exists and is readable - if [ ! -f "$MYSQL_CONNECTION_INFO_FILE" ]; then - echo "ERROR: MySQL connection info file ($MYSQL_CONNECTION_INFO_FILE) does not exist or is not readable." - exit 1 - fi - - # check that MySQL is running - if ! systemctl --quiet is-active mariadb.service - then - echo "MySQL (MariaDB) is not running." - exit 1 - fi - - # check vmail MySQL connection/authentication - if ! mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e '\q' - then - echo "MySQL connection failed. Check auth info in $MYSQL_CONNECTION_INFO_FILE" - exit 1 - fi +# check that MySQL is running +if ! systemctl --quiet is-active mariadb.service +then + echo "MySQL (MariaDB) is not running." + exit 1 +fi +# check vmail MySQL connection/authentication +if ! mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE -e '\q' +then + echo "MySQL connection failed. Check auth info in $MYSQL_CONNECTION_INFO_FILE" + exit 1 fi # functions