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

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