ganapathi / rpms / mariadb

Forked from rpms/mariadb 4 years ago
Clone

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

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