#!/usr/bin/env sh

# To/Die/For =)
unset TO_DIE
die () {
    RED='\033[0;31m'
    NC='\033[0m' # No Color
    echo "${RED}$1${NC}"
    exit 1
}

########################################################################

backup () {
    # SET VARIABLES #

    BACKUPDIR="/backup"
    NOW="$(date '+%Y%m%d_%H%M%S')"

    HOST="${MYSQL_HOST:-host.docker.internal}"
    PORT="${MYSQL_PORT:-3306}"
    USER="${MYSQL_USER:-root}"

    ( test -z "${MYSQL_PASS}" && ( test ! -f /password || test ! -s /password ) ) && die "Please set mysql password with one method: [MYSQL_PASS] env var OR [/password] file mount"
    ( test -n "${MYSQL_PASS}" && test -r /password && test -s /password ) && die "Please set mysql password with only one method"
    test -z "${MYSQL_PASS}" || ( echo "Setting [MYSQL_PASS] environment variable is insecure, use passwordfile [/password] instead"; PASS="${MYSQL_PASS}" ) 
    test -f /password && PASS="$(cat /password)"

    test -n "${MYSQL_FILE}" && BACKUP_PREFIX="${MYSQL_FILE}" || BACKUP_PREFIX="${HOST}_${PORT}"

    unset MYSQL_HOST MYSQL_PORT MYSQL_USER MYSQL_PASS MYSQL_FILE

    # Create final Backup Directory
    test -d "${BACKUPDIR}" || ( echo "There might be an error: no final backup dir. Will create one..."; mkdir -p "${BACKUPDIR}" )
    test -w "${BACKUPDIR}" || ( echo "There might be an error: no write access to final backup dir. Will change rights..."; chmod -Rc u+w "${BACKUPDIR}" )

    # Create temp Backup Directory
    TEMPDIR="${BACKUPDIR}/.${NOW}"
    test -d "${TEMPDIR}" || mkdir -p "${TEMPDIR}"
    test -w "${TEMPDIR}" || ( echo "There might be an error: no write access to temp backup dir. Will change rights..."; chmod -Rc u+w "${TEMPDIR}" )

    # Create temp Backup Config
    CONFIG="$(mktemp)"
    echo "[client]" > "${CONFIG}"
    test -n "${HOST}" && echo "host=${HOST}"        >> "${CONFIG}" || die "host"
    test -n "${USER}" && echo "user=${USER}"        >> "${CONFIG}" || die "user"
    test -n "${PASS}" && echo "password=${PASS}"    >> "${CONFIG}" || die "password"
    test -n "${PORT}" && echo "port=${PORT}"        >> "${CONFIG}" || die "port"
    echo "#"                                        >> "${CONFIG}"
    echo "[mysqldump]" >> "${CONFIG}"
    test -n "${HOST}" && echo "host=${HOST}"        >> "${CONFIG}" || die "host"
    test -n "${USER}" && echo "user=${USER}"        >> "${CONFIG}" || die "user"
    test -n "${PASS}" && echo "password=${PASS}"    >> "${CONFIG}" || die "password"
    test -n "${PORT}" && echo "port=${PORT}"        >> "${CONFIG}" || die "port"
    echo "#"                                        >> "${CONFIG}"

    # Backup user DBs
    for DB in $(mysql --defaults-file="${CONFIG}" -e 'show databases' -s --skip-column-names | grep -v -e information_schema -e performance_schema -e sys -e mysql); do
        mysqldump --defaults-file="${CONFIG}" --routines --triggers $DB > "$TEMPDIR/$DB.sql";
    done

    # Backup system DBs
    for DB in information_schema performance_schema sys mysql; do
        mysqldump --defaults-file="${CONFIG}" --routines --triggers --lock-tables=false $DB > "$TEMPDIR/$DB.sql";
    done

    # ZIP BACKUP #
    cd "$TEMPDIR"
    tar -zcvf "${BACKUP_PREFIX}-${NOW}.tar.gz" *.sql
    echo "---"
    du -h "${BACKUP_PREFIX}-${NOW}.tar.gz"
    cd - > /dev/null

    # MOVE BACKUP TO BACKUP DIR #
    mv -f "${TEMPDIR}/${BACKUP_PREFIX}-${NOW}.tar.gz" "${BACKUPDIR}"

    # CLEANUP #
    rm -rf "${TEMPDIR}"
}

########################################################################

help () {
    echo "Set environment variables: MYSQL_HOST MYSQL_PORT MYSQL_USER"
    echo "Set MYSQL_PASS insecurely or put password in file mount /password"
}

case $1 in
    help) shift && help $@;;
    *) backup;;
esac
