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

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