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