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