File check-sql-database.sh of Package kolab-scripts

#!/bin/bash
# KPS_CHECK_ORDER: 20

# (c) 2013 Richard Bos

PATH=/usr/bin:/usr/sbin

echo -n "Checking SQL database service... "

CHECK="OK"

# Determine the state of the SQL database service, and if not active start it.
# Keep the systemctl leg seperate from the systemV leg.  This way the systemV
# leg can be removed easily when desired in the future.
if [[ -x /usr/bin/systemctl ]] || [[ -x /bin/systemctl ]]; then

  DB_SERVICE=$(systemctl is-enabled mysql.service 2>/dev/null | grep abled)

  if [[ $DB_SERVICE != "enabled" ]]; then
    systemctl enable mysql.service 2>/dev/null
    STRING_CHECK_ENABLE="has been enabled"
    unset CHECK
  else
    STRING_CHECK_ENABLE="enabled"
  fi

  DB_SERVICE_STATE=$(systemctl is-active mysql.service)

  if [[ $DB_SERVICE_STATE != "active" ]]; then
    systemctl start mysql.service
    STRING_CHECK_START="started"
    unset CHECK
  else
    STRING_CHECK_START="active"
  fi
else

  if chkconfig -c mysql 35; then
    STRING_CHECK_ENABLE="enabled"
  else
    chkconfig mysql 35
    STRING_CHECK_ENABLE="has been enabled"
    unset CHECK
  fi

  if ps aux | grep -q "m[l]sql"; then
    STRING_CHECK_START="active"
  else
    rcmysql start 2>/dev/null
    STRING_CHECK_START="started"
    unset CHECK
  fi
fi

if [[ "$CHECK" == "OK" ]]; then
  echo OK
else
  echo
  echo "  SQL database service $STRING_CHECK_ENABLE and is $STRING_CHECK_START"
fi


echo -n "Checking for /root/.my.cnf file, required for SQL database server backups... "

if [[ -f /root/.my.cnf ]]; then
  echo OK
else

  echo # Sent the cursor to the next line

  # Is it possible to login without password
  if mysql -u root -e exit 2>/dev/null; then

    PASSWORD=
  else
    echo "  - Provide the SQL database server root password, this is needed to allow"
    echo "    backups to be made via the root account.  The account information will be"
    echo "    written to the file /root/.my.cnf, with read and write permission for root only."
    echo "    See the file /etc/logrotate.d/mysql for more information about this."
    echo

    # read password
    read -sp "  SQL database 'root' password: " PASSWORD
    echo # This echo is needed, because of the read -s argument
  fi

  # create the account information file, see /etc/logrotate.d/mysql for more
  # information about this.
  ( echo "[mysqladmin]"
    echo "password=$PASSWORD"
    echo "user=root"
  ) > /root/.my.cnf
  chmod 600 /root/.my.cnf

  echo "  The file /root/.my.cnf has been created"
fi

openSUSE Build Service is sponsored by