Blame SOURCES/mysql-prepare-db-dir.sh

80384c
#!/bin/sh
80384c
80384c
# This script creates the mysql data directory during first service start.
80384c
# In subsequent starts, it does nothing much.
80384c
80384c
source "`dirname ${BASH_SOURCE[0]}`/mysql-scripts-common"
80384c
80384c
# If two args given first is user, second is group
80384c
# otherwise the arg is the systemd service file
80384c
if [ "$#" -eq 2 ]
80384c
then
80384c
    myuser="$1"
80384c
    mygroup="$2"
80384c
else
80384c
    # Absorb configuration settings from the specified systemd service file,
80384c
    # or the default service if not specified
80384c
    SERVICE_NAME="$1"
80384c
    if [ x"$SERVICE_NAME" = x ]
80384c
    then
80384c
        SERVICE_NAME=@DAEMON_NAME@.service
80384c
    fi
80384c
80384c
    myuser=`systemctl show -p User "${SERVICE_NAME}" |
80384c
      sed 's/^User=//'`
80384c
    if [ x"$myuser" = x ]
80384c
    then
80384c
        myuser=mysql
80384c
    fi
80384c
80384c
    mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
80384c
      sed 's/^Group=//'`
80384c
    if [ x"$mygroup" = x ]
80384c
    then
80384c
        mygroup=mysql
80384c
    fi
80384c
fi
80384c
80384c
# Set up the errlogfile with appropriate permissions
743cec
if [ ! -e "$errlogfile" -a ! -L "$errlogfile" ]; then
743cec
    touch "$errlogfile"
743cec
    chown "$myuser:$mygroup" "$errlogfile"
743cec
    chmod 0640 "$errlogfile"
743cec
fi
743cec
su - $myuser -s /bin/bash -c "touch '$errlogfile'"
80384c
ret=$?
80384c
# Provide some advice if the log file cannot be touched
80384c
if [ $ret -ne 0 ] ; then
80384c
    errlogdir=$(dirname $errlogfile)
80384c
    if ! [ -d "$errlogdir" ] ; then
80384c
        echo "The directory $errlogdir does not exist."
80384c
    elif [ -f "$errlogfile" ] ; then
80384c
        echo "The log file $errlogfile cannot be touched, please, fix its permissions."
80384c
    else
80384c
        echo "The log file $errlogfile could not be created."
80384c
    fi
80384c
    echo "The daemon will be run under $myuser:$mygroup"
80384c
    exit 1
80384c
fi
80384c
[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"
80384c
743cec
# Make sure $datadir is there with correct permissions
743cec
if [ ! -e "$datadir" -a ! -L "$datadir" ]; then
743cec
    mkdir -p "$datadir" || exit 1
80384c
    chown "$myuser:$mygroup" "$datadir"
80384c
    chmod 0755 "$datadir"
743cec
fi
743cec
[ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
80384c
743cec
if [ ! -d "$datadir/mysql" ] ; then
80384c
    # Now create the database
80384c
    echo "Initializing @NICE_PROJECT_NAME@ database"
743cec
    su - $myuser -s /bin/bash -c "@bindir@/mysql_install_db --rpm --datadir='$datadir' --user='$myuser'"
80384c
    ret=$?
80384c
    if [ $ret -ne 0 ] ; then
80384c
        echo "Initialization of @NICE_PROJECT_NAME@ database failed." >&2
80384c
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured." >&2
743cec
        echo "Note, that you may need to clean up any partially-created database files in $datadir" >&2
80384c
        exit $ret
80384c
    fi
80384c
    # upgrade does not need to be run on a fresh datadir
743cec
    su - $myuser -s /bin/bash -c "echo '@VERSION@' > '$datadir/mysql_upgrade_info'"
80384c
fi
80384c
80384c
exit 0