ba7104
#!/bin/sh
ba7104
ba7104
# This script creates the mysql data directory during first service start.
ba7104
# In subsequent starts, it does nothing much.
ba7104
ba7104
source "`dirname ${BASH_SOURCE[0]}`/mysql-scripts-common"
ba7104
ba7104
export LC_ALL=C
ba7104
ba7104
# Returns content of the specified directory
ba7104
# If listing files fails, fake-file is returned so which means
ba7104
# we'll behave like there was some data initialized
ba7104
# Some files or directories are fine to be there, so those are
ba7104
# explicitly removed from the listing
ba7104
# @param <dir> datadir
ba7104
list_datadir ()
ba7104
{
ba7104
    ( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
ba7104
    -e '^lost+found$' \
ba7104
    -e '\.err$' \
ba7104
    -e '^.bash_history$'
ba7104
}
ba7104
ba7104
# Checks whether datadir should be initialized
ba7104
# @param <dir> datadir
ba7104
should_initialize ()
ba7104
{
ba7104
    test -z "$(list_datadir "$1")"
ba7104
}
ba7104
ba7104
# If two args given first is user, second is group
ba7104
# otherwise the arg is the systemd service file
ba7104
if [ "$#" -eq 2 ]
ba7104
then
ba7104
    myuser="$1"
ba7104
    mygroup="$2"
ba7104
else
ba7104
    # Absorb configuration settings from the specified systemd service file,
ba7104
    # or the default service if not specified
ba7104
    SERVICE_NAME="$1"
ba7104
    if [ x"$SERVICE_NAME" = x ]
ba7104
    then
ba7104
        SERVICE_NAME=@DAEMON_NAME@.service
ba7104
    fi
ba7104
ba7104
    myuser=`systemctl show -p User "${SERVICE_NAME}" |
ba7104
      sed 's/^User=//'`
ba7104
    if [ x"$myuser" = x ]
ba7104
    then
ba7104
        myuser=mysql
ba7104
    fi
ba7104
ba7104
    mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
ba7104
      sed 's/^Group=//'`
ba7104
    if [ x"$mygroup" = x ]
ba7104
    then
ba7104
        mygroup=mysql
ba7104
    fi
ba7104
fi
ba7104
ba7104
# Set up the errlogfile with appropriate permissions
ba7104
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
ba7104
    case $(basename "$errlogfile") in
ba7104
        mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
ba7104
        *) ;;
ba7104
    esac
ba7104
else
ba7104
    # Provide some advice if the log file cannot be created by this script
ba7104
    errlogdir=$(dirname "$errlogfile")
ba7104
    if ! [ -d "$errlogdir" ] ; then
ba7104
        echo "The directory $errlogdir does not exist." >&2
ba7104
        exit 1
ba7104
    elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
ba7104
        echo "The log file $errlogfile cannot be written, please, fix its permissions." >&2
ba7104
        echo "The daemon will be run under $myuser:$mygroup" >&2
ba7104
        exit 1
ba7104
    fi
ba7104
fi
ba7104
ba7104
# Make the data directory if doesn't exist or empty
ba7104
if should_initialize "$datadir" ; then
ba7104
    # First, make sure $datadir is there with correct permissions
ba7104
    # (note: if it's not, and we're not root, this'll fail ...)
ba7104
    if [ ! -e "$datadir" -a ! -h "$datadir" ]
ba7104
    then
ba7104
        mkdir -p "$datadir" || exit 1
ba7104
    fi
ba7104
    chown "$myuser:$mygroup" "$datadir"
ba7104
    chmod 0755 "$datadir"
ba7104
    [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir"
ba7104
ba7104
    # Now create the database
ba7104
    echo "Initializing @NICE_PROJECT_NAME@ database" >&2
ba7104
    # Avoiding deletion of files not created by mysql_install_db is
ba7104
    # guarded by time check and sleep should help work-arounded
ba7104
    # potential issues on systems with 1 second resolution timestamps
ba7104
    # https://bugzilla.redhat.com/show_bug.cgi?id=1335849#c19
ba7104
    INITDB_TIMESTAMP=`LANG=C date -u`
ba7104
    sleep 1
ba7104
    @bindir@/mysql_install_db --rpm --datadir="$datadir" --user="$myuser" --skip-test-db >&2
ba7104
    ret=$?
ba7104
    if [ $ret -ne 0 ] ; then
ba7104
        echo "Initialization of @NICE_PROJECT_NAME@ database failed." >&2
ba7104
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured or there is some problem with permissions of $datadir." >&2
ba7104
        # Clean up any partially-created database files
ba7104
        if [ ! -e "$datadir/mysql/user.frm" ] && [ -d "$datadir" ] ; then
ba7104
            echo "Initialization of @NICE_PROJECT_NAME@ database was not finished successfully." >&2
ba7104
            echo "Files created so far will be removed." >&2
ba7104
            find "$datadir" -mindepth 1 -maxdepth 1 -newermt "$INITDB_TIMESTAMP" \
ba7104
                 -not -name "lost+found" -exec rm -rf {} +
ba7104
            if [ $? -ne 0 ] ; then
ba7104
                echo "Removing of created files was not successfull." >&2
ba7104
                echo "Please, clean directory $datadir manually." >&2
ba7104
            fi
ba7104
        else
ba7104
            echo "However, part of data has been initialized and those will not be removed." >&2
ba7104
            echo "Please, clean directory $datadir manually." >&2
ba7104
        fi
ba7104
        exit $ret
ba7104
    fi
ba7104
    # upgrade does not need to be run on a fresh datadir
ba7104
    echo "@VERSION@-MariaDB" >"$datadir/mysql_upgrade_info"
ba7104
else
ba7104
    if [ -d "$datadir/mysql/" ] ; then
ba7104
        # mysql dir exists, it seems data are initialized properly
ba7104
        echo "Database @NICE_PROJECT_NAME@ is probably initialized in $datadir already, nothing is done."
ba7104
        echo "If this is not the case, make sure the $datadir is empty before running `basename $0`."
ba7104
    else
ba7104
        # if the directory is not empty but mysql/ directory is missing, then
ba7104
        # print error and let user to initialize manually or empty the directory
ba7104
        echo "Database @NICE_PROJECT_NAME@ is not initialized, but the directory $datadir is not empty, so initialization cannot be done." >&2
ba7104
        echo "Make sure the $datadir is empty before running `basename $0`." >&2
ba7104
        exit 1
ba7104
    fi
ba7104
fi
ba7104
ba7104
exit 0