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

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