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