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

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