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

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