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