1#! /bin/sh 2 3set +e 4 5create_db () { 6 echo "Checking for db" 7 mysqladmin --defaults-file=/etc/mysql/debian.cnf -f reload 8 # test if database if already present... 9 if ! $(echo quit | mysql --defaults-file=/etc/mysql/debian.cnf zm > /dev/null 2> /dev/null) ; then 10 echo "Creating zm db" 11 cat /usr/share/zoneminder/db/zm_create.sql | mysql --defaults-file=/etc/mysql/debian.cnf 12 if [ $? -ne 0 ]; then 13 echo "Error creating db." 14 exit 1; 15 fi 16 else 17 echo "Db exists." 18 fi 19} 20 21create_update_user () { 22 USER_EXISTS="$(mysql --defaults-file=/etc/mysql/debian.cnf -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$ZM_DB_USER')")" 23 if [ $USER_EXISTS -ne 1 ]; then 24 echo "Creating zm user $ZM_DB_USER" 25 # This creates the user. 26 echo "CREATE USER '${ZM_DB_USER}'@${ZM_DB_HOST} IDENTIFIED BY '${ZM_DB_PASS}';" | mysql --defaults-file=/etc/mysql/debian.cnf mysql 27 fi 28 echo "Updating permissions for user ${ZM_DB_USER}@${ZM_DB_HOST}" 29 echo "GRANT LOCK tables,alter,drop,select,insert,update,delete,create,index,alter routine,create routine,trigger,execute,REFERENCES ON ${ZM_DB_NAME}.* TO '${ZM_DB_USER}'@${ZM_DB_HOST};" | mysql --defaults-file=/etc/mysql/debian.cnf mysql 30} 31 32update_db () { 33 34 zmupdate.pl -s --nointeractive 35 if [ $? -ne 0 ]; then 36 echo "Error updating db." 37 exit 1; 38 fi 39 zmupdate.pl --nointeractive -f 40 if [ $? -ne 0 ]; then 41 echo "Error updating config." 42 exit 1; 43 fi 44 45 # Add any new PTZ control configurations to the database (will not overwrite) 46 zmcamtool.pl --import >/dev/null 2>&1 47 echo "Done Updating" 48} 49 50if [ "$1" = "configure" ]; then 51 52 . /etc/zm/zm.conf 53 for CONFFILE in /etc/zm/conf.d/*.conf; do 54 . "$CONFFILE" 55 done 56 57 # The logs can contain passwords, etc... so by setting group root, only www-data can read them, not people in the www-data group 58 chown www-data:root /var/log/zm 59 chown www-data:www-data /var/lib/zm 60 chown www-data:www-data /var/cache/zoneminder /var/cache/zoneminder/* 61 if [ ! -e "/etc/apache2/mods-enabled/cgi.load" ] && [ "$(command -v a2enmod)" != "" ]; then 62 echo "The cgi module is not enabled in apache2. I am enabling it using a2enmod cgi." 63 if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then 64 . /usr/share/apache2/apache2-maintscript-helper 65 apache2_invoke enmod cgi || exit $? 66 fi 67 fi 68 69 SYSTEMD=0 70 if [ -e "/run/systemd/system" ]; then 71 SYSTEMD=1 72 echo "detected systemd" 73 # Ensure zoneminder is stopped 74 deb-systemd-invoke stop zoneminder.service || exit $? 75 else 76 # Ensure zoneminder is stopped 77 invoke-rc.d zoneminder stop || true 78 fi 79 80 if [ "$ZM_DB_HOST" = "localhost" ]; then 81 82 if [ $SYSTEMD -eq 1 ] && ([ -e "/lib/systemd/system/mysql.service" ] || [ -e "/lib/systemd/system/mariadb.service" ]); then 83 # 84 # Get mysql started if it isn't running 85 # 86 87 if [ -e "/lib/systemd/system/mariadb.service" ]; then 88 DBSERVICE="mariadb.service" 89 else 90 DBSERVICE="mysql.service" 91 fi 92 echo "Detected db service is $DBSERVICE" 93 if systemctl is-failed --quiet $DBSERVICE; then 94 echo "$DBSERVICE is in a failed state; it will not be started." 95 echo "If you have already resolved the problem preventing $DBSERVICE from running," 96 echo "run sudo systemctl restart $DBSERVICE then run sudo dpkg-reconfigure zoneminder." 97 exit 1 98 fi 99 100 if ! systemctl is-active --quiet mysql.service mariadb.service; then 101 # Due to /etc/init.d service autogeneration, mysql.service always returns the status of mariadb.service 102 # However, mariadb.service will not return the status of mysql.service. 103 deb-systemd-invoke start $DBSERVICE 104 fi 105 106 # Make sure systemctl status exit code is 0; i.e. the DB is running 107 if systemctl is-active --quiet "$DBSERVICE"; then 108 create_db 109 create_update_user 110 update_db 111 else 112 echo 'NOTE: MySQL/MariaDB not running; please start mysql and run dpkg-reconfigure zoneminder when it is running.' 113 fi 114 115 elif [ -e "/etc/init.d/mysql" ]; then 116 # 117 # Get mysql started if it isn't 118 # 119 if ! $(/etc/init.d/mysql status >/dev/null 2>&1); then 120 service mysql start 121 fi 122 if $(/etc/init.d/mysql status >/dev/null 2>&1); then 123 create_db 124 create_update_user 125 update_db 126 else 127 echo 'NOTE: MySQL/MariaDB not running; please start mysql and run dpkg-reconfigure zoneminder when it is running.' 128 fi 129 130 else 131 echo 'MySQL/MariaDB not found; assuming remote server.' 132 fi 133 fi 134 135else 136 echo "Not doing database upgrade due to remote db server ($ZM_DB_HOST)." 137fi 138 139if [ $SYSTEMD -eq 1 ]; then 140 deb-systemd-invoke restart zoneminder.service 141#else 142 #service zoneminder start || true 143fi 144 145#DEBHELPER# 146