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