2016-05-01 11:46:09 +08:00
#! /bin/sh
2018-03-16 23:58:50 +08:00
set +e
2016-05-01 11:46:09 +08:00
if [ "$1" = "configure" ]; then
2018-03-13 22:28:11 +08:00
2017-06-22 05:20:50 +08:00
. /etc/zm/zm.conf
2018-03-14 22:44:43 +08:00
for CONFFILE in /etc/zm/conf.d/*.conf; do
. "$CONFFILE"
done
2017-06-22 05:20:50 +08:00
# The logs can contain passwords, etc... so by setting group root, only www-data can read them, not people in the www-data group
chown www-data:root /var/log/zm
chown www-data:www-data /var/lib/zm
if [ -z "$2" ]; then
2018-03-15 23:04:41 +08:00
chown www-data:www-data /var/cache/zoneminder /var/cache/zoneminder/* /usr/share/zoneminder/www/cache
2017-06-22 05:20:50 +08:00
fi
2018-03-14 22:44:43 +08:00
if [ ! -e "/etc/apache2/mods-enabled/cgi.load" ] && [ "$(command -v a2enmod)" != "" ]; then
2017-06-22 05:20:50 +08:00
echo "The cgi module is not enabled in apache2. I am enabling it using a2enmod cgi."
a2enmod cgi
fi
2016-05-01 11:46:09 +08:00
2017-11-23 04:36:04 +08:00
if [ "$ZM_DB_HOST" = "localhost" ]; then
2016-05-01 11:46:09 +08:00
2018-03-14 22:44:43 +08:00
if [ -e "/lib/systemd/system/mysql.service" ] || [ -e "/lib/systemd/system/mariadb.service" ]; then
2017-11-21 00:56:05 +08:00
# Ensure zoneminder is stopped
deb-systemd-invoke stop zoneminder.service || exit $?
2018-03-14 22:44:43 +08:00
2018-03-22 23:32:22 +08:00
#
2018-03-15 23:04:41 +08:00
# Get mysql started if it isn't running
2017-06-22 05:20:50 +08:00
#
2018-03-15 23:04:41 +08:00
2018-03-22 23:32:22 +08:00
if [ -e "/lib/systemd/system/mariadb.service" ]; then
2018-03-15 23:04:41 +08:00
DBSERVICE="mariadb.service"
else
DBSERVICE="mysql.service"
fi
2018-04-15 11:07:33 +08:00
echo "Detected db service is $DBSERVICE"
2018-03-22 23:32:22 +08:00
if systemctl is-failed --quiet $DBSERVICE; then
echo "$DBSERVICE is in a failed state; it will not be started."
echo "If you have already resolved the problem preventing $DBSERVICE from running,"
echo "run sudo systemctl restart $DBSERVICE then run sudo dpkg-reconfigure zoneminder."
exit 1
fi
2018-03-15 23:04:41 +08:00
2018-03-22 23:32:22 +08:00
if ! systemctl is-active --quiet mysql.service mariadb.service; then
2018-03-15 23:04:41 +08:00
# Due to /etc/init.d service autogeneration, mysql.service always returns the status of mariadb.service
# However, mariadb.service will not return the status of mysql.service.
deb-systemd-invoke start $DBSERVICE
fi
2018-03-22 23:32:22 +08:00
2018-03-15 23:04:41 +08:00
# Make sure systemctl status exit code is 0; i.e. the DB is running
2018-03-22 23:32:22 +08:00
if systemctl is-active --quiet "$DBSERVICE"; then
2017-06-22 05:20:50 +08:00
mysqladmin --defaults-file=/etc/mysql/debian.cnf -f reload
# test if database if already present...
if ! $(echo quit | mysql --defaults-file=/etc/mysql/debian.cnf zm > /dev/null 2> /dev/null) ; then
2018-04-15 11:07:33 +08:00
echo "Creating zm db"
2017-06-22 05:20:50 +08:00
cat /usr/share/zoneminder/db/zm_create.sql | mysql --defaults-file=/etc/mysql/debian.cnf
# This creates the user.
2018-01-30 00:44:03 +08:00
echo "grant lock tables,alter,drop,select,insert,update,delete,create,index,alter routine,create routine, trigger,execute on ${ZM_DB_NAME}.* to '${ZM_DB_USER}'@localhost identified by \"${ZM_DB_PASS}\";" | mysql --defaults-file=/etc/mysql/debian.cnf mysql
2016-05-01 11:46:09 +08:00
else
2018-04-15 11:07:33 +08:00
echo "Updating permissions"
2018-01-30 00:44:03 +08:00
echo "grant lock tables,alter,drop,select,insert,update,delete,create,index,alter routine,create routine, trigger,execute on ${ZM_DB_NAME}.* to '${ZM_DB_USER}'@localhost;" | mysql --defaults-file=/etc/mysql/debian.cnf mysql
2018-03-16 23:58:50 +08:00
fi
2018-04-15 11:07:33 +08:00
zmupdate.pl --nointeractive
zmupdate.pl --nointeractive -f
# Add any new PTZ control configurations to the database (will not overwrite)
zmcamtool.pl --import >/dev/null 2>&1
2017-06-22 05:20:50 +08:00
else
2018-03-15 23:04:41 +08:00
echo 'NOTE: MySQL/MariaDB not running; please start mysql and run dpkg-reconfigure zoneminder when it is running.'
2017-06-22 05:20:50 +08:00
fi
2016-05-01 11:46:09 +08:00
else
2018-03-14 22:44:43 +08:00
echo 'MySQL/MariaDB not found; assuming remote server.'
2016-05-01 11:46:09 +08:00
fi
2017-06-22 05:20:50 +08:00
else
2018-03-14 22:44:43 +08:00
echo "Not doing database upgrade due to remote db server ($ZM_DB_HOST)."
2017-06-22 05:20:50 +08:00
fi
2018-03-14 22:44:43 +08:00
echo "Done Updating; starting ZoneMinder."
deb-systemd-invoke restart zoneminder.service
2017-06-22 05:20:50 +08:00
2016-05-01 11:46:09 +08:00
fi
#DEBHELPER#