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
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
a8ba852
# @param <dir> datadir
a8ba852
ls_check_datadir ()
a8ba852
{
a8ba852
    ls -A "$1" 2>/dev/null
a8ba852
    test $? -eq 0 || echo "fake-file"
a8ba852
}
a8ba852
a8ba852
# Checks whether datadir should be initialized
a8ba852
# @param <dir> datadir
dac8b4c
should_initialize ()
a8ba852
{
a8ba852
    case `ls_check_datadir "$1"` in
6409446
    ""|lost+found|*.err) true ;;
a8ba852
    *) false ;;
a8ba852
    esac
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
9c7c797
touch "$errlogfile"
f2af251
ret=$?
f2af251
# Provide some advice if the log file cannot be touched
f2af251
if [ $ret -ne 0 ] ; then
f2af251
    errlogdir=$(dirname $errlogfile)
f2af251
    if ! [ -d "$errlogdir" ] ; then
f2af251
        echo "The directory $errlogdir does not exist."
f2af251
    elif [ -f "$errlogfile" ] ; then
f2af251
        echo "The log file $errlogfile cannot be touched, please, fix its permissions."
f2af251
    else
f2af251
        echo "The log file $errlogfile could not be created."
f2af251
    fi
f2af251
    echo "The daemon will be run under $myuser:$mygroup"
f2af251
    exit 1
f2af251
fi
9c7c797
chown "$myuser:$mygroup" "$errlogfile"
9c7c797
chmod 0640 "$errlogfile"
9c7c797
[ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile"
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
85af24c
    echo "Initializing @NICE_PROJECT_NAME@ database"
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
2a7a489
    @bindir@/mysql_install_db --rpm --datadir="$datadir" --user="$myuser"
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"
9c7c797
    # In case we're running as root, make sure files are owned properly
9c7c797
    chown -R "$myuser:$mygroup" "$datadir"
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
d12b2c5
        echo "Database @NICE_PROJECT_NAME@ is not initialized, but the directory $datadir is not empty, so initialization cannot be done."
a8ba852
        echo "Make sure the $datadir is empty before running `basename $0`."
a8ba852
        exit 1
a8ba852
    fi
9c7c797
fi
9c7c797
9c7c797
exit 0