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

47f670
#!/bin/sh
47f670
47f670
# This script creates the mysql data directory during first service start.
47f670
# In subsequent starts, it does nothing much.
47f670
#
47f670
# This script is meant to be run as non-root user either during initscript
47f670
# or systemd service execution, before starting the mysqld daemon.
47f670
# Running it as root may have some security risks, because it touches files
47f670
# that can be symlinks pointing to unexpected locations.
47f670
#
47f670
# On the other hand, when using non-standard locations for datadir and logfile,
47f670
# this script might not be able to create the files and the daemon won't start
47f670
# properly. A solution for that is to created the locations for datadir and
47f670
# logfile with correct ownership before starting the daemon.
47f670
47f670
source "`dirname ${BASH_SOURCE[0]}`/mysql-scripts-common"
47f670
47f670
# If two args given first is user, second is group
47f670
# otherwise the arg is the systemd service file
47f670
if [ "$#" -eq 2 ]
47f670
then
47f670
    myuser="$1"
47f670
    mygroup="$2"
47f670
else
47f670
    # Absorb configuration settings from the specified systemd service file,
47f670
    # or the default service if not specified
47f670
    SERVICE_NAME="$1"
47f670
    if [ x"$SERVICE_NAME" = x ]
47f670
    then
47f670
        SERVICE_NAME=@DAEMON_NAME@.service
47f670
    fi
47f670
47f670
    myuser=`systemctl show -p User "${SERVICE_NAME}" |
47f670
      sed 's/^User=//'`
47f670
    if [ x"$myuser" = x ]
47f670
    then
47f670
        myuser=mysql
47f670
    fi
47f670
47f670
    mygroup=`systemctl show -p Group "${SERVICE_NAME}" |
47f670
      sed 's/^Group=//'`
47f670
    if [ x"$mygroup" = x ]
47f670
    then
47f670
        mygroup=mysql
47f670
    fi
47f670
fi
47f670
47f670
# Set up the errlogfile with appropriate permissions
47f670
if [ ! -e "$errlogfile" -a ! -h "$errlogfile" -a x$(dirname "$errlogfile") = "x/var/log" ]; then
47f670
    case $(basename "$errlogfile") in
47f670
        mysql*.log|mariadb*.log) install /dev/null -m0640 -o$myuser -g$mygroup "$errlogfile" ;;
47f670
        *) ;;
47f670
    esac
47f670
else
47f670
    # Provide some advice if the log file cannot be created by this script
47f670
    errlogdir=$(dirname "$errlogfile")
47f670
    if ! [ -d "$errlogdir" ] ; then
47f670
        echo "The directory $errlogdir does not exist."
47f670
        exit 1
47f670
    elif [ -e "$errlogfile" -a ! -w "$errlogfile" ] ; then
47f670
        echo "The log file $errlogfile cannot be written, please, fix its permissions."
47f670
        echo "The daemon will be run under $myuser:$mygroup"
47f670
        exit 1
47f670
    fi
47f670
fi
47f670
47f670
47f670
47f670
export LC_ALL=C
47f670
47f670
# Returns content of the specified directory
47f670
# If listing files fails, fake-file is returned so which means
47f670
# we'll behave like there was some data initialized
47f670
# Some files or directories are fine to be there, so those are
47f670
# explicitly removed from the listing
47f670
# @param <dir> datadir
47f670
list_datadir ()
47f670
{
47f670
    ( ls -1A "$1" 2>/dev/null || echo "fake-file" ) | grep -v \
47f670
    -e '^lost+found$' \
47f670
    -e '\.err$' \
47f670
    -e '^\.bash_history$'
47f670
}
47f670
47f670
# Checks whether datadir should be initialized
47f670
# @param <dir> datadir
47f670
should_initialize ()
47f670
{
47f670
    test -z "$(list_datadir "$1")"
47f670
}
47f670
47f670
# Make the data directory if doesn't exist or empty
47f670
if should_initialize "$datadir" ; then
47f670
47f670
    # Now create the database
47f670
    echo "Initializing @NICE_PROJECT_NAME@ database"
47f670
    @libexecdir@/mysqld --initialize-insecure --datadir="$datadir" --user="$myuser"
47f670
    ret=$?
47f670
    if [ $ret -ne 0 ] ; then
47f670
        echo "Initialization of @NICE_PROJECT_NAME@ database failed." >&2
47f670
        echo "Perhaps @sysconfdir@/my.cnf is misconfigured." >&2
47f670
        # Clean up any partially-created database files
47f670
        if [ ! -e "$datadir/mysql/user.frm" ] ; then
47f670
            rm -rf "$datadir"/*
47f670
        fi
47f670
        exit $ret
47f670
    fi
47f670
    # upgrade does not need to be run on a fresh datadir
47f670
    echo "@VERSION@" >"$datadir/mysql_upgrade_info"
47f670
fi
47f670
47f670
exit 0