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