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

4b4994
#!/bin/sh
4b4994
4b4994
# This script creates the mysql data directory during first service start.
4b4994
# In subsequent starts, it does nothing much.
4b4994
4b4994
source "`dirname ${BASH_SOURCE[0]}`/mysql-scripts-common"
4b4994
4b4994
# If two args given first is user, second is group
4b4994
# otherwise the arg is the systemd service file
4b4994
if [ "$#" -eq 2 ]
4b4994
then
4b4994
    myuser="$1"
4b4994
    mygroup="$2"
4b4994
else
4b4994
    # Absorb configuration settings from the specified systemd service file,
4b4994
    # or the default service if not specified
4b4994
    SERVICE_NAME="$1"
4b4994
    if [ x"$SERVICE_NAME" = x ]
4b4994
    then
4b4994
        SERVICE_NAME=@DAEMON_NAME@.service
4b4994
    fi
4b4994
4b4994
    myuser=`systemctl show -p User "${SERVICE_NAME}" |
4b4994
      sed 's/^User=//'`
4b4994
    if [ x"$myuser" = x ]
4b4994
    then
4b4994
        myuser=mysql
4b4994
    fi
4b4994
4b4994
    mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
4b4994
      sed 's/^Group=//'`
4b4994
    if [ x"$mygroup" = x ]
4b4994
    then
4b4994
        mygroup=mysql
4b4994
    fi
4b4994
fi
4b4994
4b4994
# Set up the errlogfile with appropriate permissions
f72fa0
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
f72fa0
    case $(basename "$errlogfile") in
f72fa0
        mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
f72fa0
        *) ;;
f72fa0
    esac
f72fa0
else
f72fa0
    # Provide some advice if the log file cannot be created by this script
f72fa0
    errlogdir=$(dirname "$errlogfile")
4b4994
    if ! [ -d "$errlogdir" ] ; then
f72fa0
        echo "The directory $errlogdir does not exist." >&2
f72fa0
        exit 1
f72fa0
    elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
f72fa0
        echo "The log file $errlogfile cannot be written, please, fix its permissions." >&2
f72fa0
        echo "The daemon will be run under $myuser:$mygroup" >&2
f72fa0
        exit 1
4b4994
    fi
4b4994
fi
4b4994
f72fa0
f72fa0
f72fa0
export LC_ALL=C
f72fa0
f72fa0
# Returns content of the specified directory
f72fa0
# If listing files fails, fake-file is returned so which means
f72fa0
# we'll behave like there was some data initialized
f72fa0
# Some files or directories are fine to be there, so those are
f72fa0
# explicitly removed from the listing
f72fa0
# @param <dir> datadir
f72fa0
list_datadir ()
f72fa0
{
f72fa0
    ( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
f72fa0
    -e '^lost+found$' \
f72fa0
    -e '\.err$' \
f72fa0
    -e '^\.bash_history$'
f72fa0
}
f72fa0
f72fa0
# Checks whether datadir should be initialized
f72fa0
# @param <dir> datadir
f72fa0
should_initialize ()
f72fa0
{
f72fa0
    test -z "$(list_datadir "$1")"
f72fa0
}
f72fa0
f72fa0
# Make the data directory if doesn't exist or empty
f72fa0
if should_initialize "$datadir" ; then
4b4994
4b4994
    # Now create the database
f72fa0
    echo "Initializing @NICE_PROJECT_NAME@ database" >&2
f72fa0
    @bindir@/mysql_install_db --rpm --datadir="$datadir" --user="$myuser" >&2
4b4994
    ret=$?
4b4994
    if [ $ret -ne 0 ] ; then
4b4994
        echo "Initialization of @NICE_PROJECT_NAME@ database failed." >&2
4b4994
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured." >&2
f72fa0
        echo "Note, that you may need to clean up any partially-created database files in $datadir" >&2
4b4994
        exit $ret
4b4994
    fi
4b4994
    # upgrade does not need to be run on a fresh datadir
4b4994
    echo "@VERSION@-MariaDB" >"$datadir/mysql_upgrade_info"
f72fa0
else
f72fa0
    if [ -d "$datadir/mysql/" ] ; then
f72fa0
        # mysql dir exists, it seems data are initialized properly
f72fa0
        echo "Database @NICE_PROJECT_NAME@ is probably initialized in $datadir already, nothing is done."
f72fa0
        echo "If this is not the case, make sure the $datadir is empty before running `basename $0`."
f72fa0
    else
f72fa0
        # if the directory is not empty but mysql/ directory is missing, then
f72fa0
        # print error and let user to initialize manually or empty the directory
f72fa0
        echo "Database @NICE_PROJECT_NAME@ is not initialized, but the directory $datadir is not empty, so initialization cannot be done." >&2
f72fa0
        echo "Make sure the $datadir is empty before running `basename $0`." >&2
f72fa0
        exit 1
f72fa0
    fi
4b4994
fi
4b4994
4b4994
exit 0