#!/bin/sh

# We have to re-enable SCL environment, because /sbin/service
# clears almost all environment variables.
# Since X_SCLS is cleared as well, we lose information about other
# collections enabled.
. /opt/rh/rh-mariadb102/service-environment
for sclname in $RH_MARIADB102_SCLS_ENABLED ; do
    . /opt/rh/$sclname/enable
    export X_SCLS="$X_SCLS $sclname"
done

# we want start daemon only inside "scl enable" invocation
if ! scl_enabled $sclname ; then
    echo "Collection $sclname has to be listed in /opt/rh/rh-mariadb102/service-environment"
    exit 1
fi


status=$(/sbin/e-smith/config getprop mariadb status)
fixtables=$(/sbin/e-smith/config getprop mariadb autofixtables || echo "disabled")
if [ "$fixtables" = "enabled" ]; then
	/sbin/e-smith/config delprop mariadb failsbackup
fi
failsbackup=$(/sbin/e-smith/config getprop mariadb failsbackup || echo "disabled")


onfailure () {
    db=$1
    message=""
    if [ "$failsbackup" = "enabled" ]; then
        exit 1;
	message="There was an error trying to dump database $db, please fix this db. We stop there without backups."
	echo $message
	echo $message | /usr/bin/mail -s "error on backup of $db MariaDB 102 database" admin
    fi
    message="$message \nThere was an error trying to dump database $db, please check for table errors in this db. Forcing a backup of the corrupted DB."
    /opt/rh/rh-mariadb102/root/usr/bin/mysqldump --socket=/var/lib/mysql/mariadb102.sock --force --ignore-table=mysql.event --ignore-table=mysql.user --single-transaction --add-drop-table -QB "$db" -r /home/e-smith/db/mariadb102/"$db"-failed.dump || message="$message \nFailed to force backup of corrupted db $db as $db-failed.dump" >&2
    if [ "$fixtables" = "enabled" ]; then
	repair="failure"
	message="$message \nTrying to auto-repair the db and do a backup after..."
	/opt/rh/rh-mariadb102/root/usr/bin/mysqlcheck --socket=/var/lib/mysql/mariadb102.sock -s --auto-repair -c "$db"  &&  \
   	/opt/rh/rh-mariadb102/root/usr/bin/mysqldump --socket=/var/lib/mysql/mariadb102.sock --ignore-table=mysql.event --ignore-table=mysql.user --single-transaction --add-drop-table -QB "$db" -r /home/e-smith/db/mariadb102/"$db".dump && repair="success"
	message="$message \n => $repair"
    fi
    echo $message
    echo $message | /usr/bin/mail -s "error on backup of $db MariaDB database" admin
}

# Do nothing if disabled
if [ "$status" = "disabled" ]
then
    echo "mysqld is disabled - no tables dumped" >&2
    exit 0
fi

# Actual mysql db backup
for db in $(/opt/rh/rh-mariadb102/root/usr/bin/mysql --socket=/var/lib/mysql/mariadb102.sock -BNre "show databases;"|egrep -vi "^information_schema$|^performance_schema$") 
do
    /opt/rh/rh-mariadb102/root/usr/bin/mysqldump --socket=/var/lib/mysql/mariadb102.sock --ignore-table=mysql.event --ignore-table=mysql.user --single-transaction --add-drop-table -QB "$db" -r /home/e-smith/db/mariadb102/"$db".dump || onfailure $db
done

# Backup the users and privileges
# backup of mysql/mariadb users excluding root for later restore.
# would allow compatible backup from mariadb <103 to mariadb >= 103
# will also need to exclude mysql.user from restore in mariadb >= 103
# inspired by https://stackoverflow.com/a/56588240
/opt/rh/rh-mariadb102/root/usr/bin/mysql -sNe  " \
  SELECT \
    CONCAT( 'CREATE USER IF NOT EXISTS \'', User, '\'@\'', Host, '\' IDENTIFIED BY \'', PASSWORD, '\'\;' ) AS User \
  FROM mysql.user \
  WHERE \
    User NOT LIKE 'mysql.%' AND CONCAT( User, Host ) <> 'rootlocalhost'  \
" >/home/e-smith/db/mariadb102/mysql.privileges.dump
/opt/rh/rh-mariadb102/root/usr/bin/mysql -sNe " \
  SELECT \
    CONCAT( '\'', User, '\'@\'', Host, '\'' ) as User FROM mysql.user \
  WHERE \
    User NOT LIKE 'mysql.%' \
    AND CONCAT( User, Host ) <> 'rootlocalhost' \
" | sort | while read u ; 
 do echo "-- $u">> /home/e-smith/db/mariadb102/mysql.privileges.dump
 /opt/rh/rh-mariadb102/root/usr/bin/mysql -sNe "show grants for $u" | sed 's/$/;/' >> /home/e-smith/db/mariadb102/mysql.privileges.dump
done
echo >> /home/e-smith/db/mariadb102/mysql.privileges.dump
echo "FLUSH PRIVILEGES;" >> /home/e-smith/db/mariadb102/mysql.privileges.dump

