#!/bin/bash 
# Simple backupscript for MySQL databases
# Author: Lars Vogdt
# BSD3 Clause License
#

PATH=/bin:/usr/bin
MAILX='/usr/bin/mail'
INFLUXDB_OPTS='-portable'
INFLUXD='/usr/bin/influxd'
INFLUX_INSPECT='/usr/bin/influx_inspect'
SEND_NSCA_CONFIG='/etc/send_nsca.cfg'
SEND_NSCA_BIN='/usr/bin/send_nsca'
SYSTEMCTL='/usr/bin/systemctl'
USE_NSCA='no'
USE_EMAIL='yes'
SEND_NSCA_HOSTNAME=$(hostname -s)
NAGIOSHOST='localhost'
NAGIOS_SERVICE_NAME='InfluxDB backup'
DISTCONFIG='/etc/sysconfig/influxdb-backupscript'
##################################################
# Default/Fallback values
# Don't change them here! Use $DISCONFIG instead
##################################################
BACKUPDIR='/root/backup/influxdb'
DUMP_OPTIONS="\
-portable"
FQHOSTNAME=`hostname -f`
HOST=$(hostname -s 2>/dev/null)
LOGNAME='influxdb-backupscript'
LOGFILE="/var/log/${LOGNAME}.log"
EMAIL='root@localhost'
START_BACKUP='yes'
FORCE_BACKUP='no'
VERIFY_DB='no'
DEBUG='no'
RETENTION=14
COMPRESS_EXE='/usr/bin/xz'
COMPRESS_AFTER_UNLOCK='yes'
##################################################
umask 027
unset LANG;

cleanup_and_exit(){
    test -n "$TMPFILE" -a -f "$TMPFILE" && rm "$TMPFILE"
    exit $1
}

LOG(){
    local MESSAGE="$1"
    local LOG_DATE=$(date "+%b %d %H:%M:%S")
    if [ -z "$LOGFILE" ]; then
        HANDLE_MESSAGE "ERROR: LOGFILE is not defined" 1
        cleanup_and_exit 1
    fi
    if [ ! -d "$LOGDIR" ]; then
        mkdir -p "$LOGDIR" || exit 1
        echo "$LOG_DATE $HOST $LOGNAME[$$]: function LOG created $LOGDIR" > "$LOGFILE"
    fi
    echo "$LOG_DATE $HOST $LOGNAME[$$]: $MESSAGE" >> $LOGFILE || DEBUG="yes"
    if [ "$DEBUG" = "yes" ]; then
        echo "DEBUG:    $MESSAGE"
    fi    
}

SEND_NSCA(){
    local MESSAGE="$1"
    local EXIT_CODE="$2"
    echo -e "$SEND_NSCA_HOSTNAME\t$NAGIOS_SERVICE_NAME\t$EXIT_CODE\t$MESSAGE" | $SEND_NSCA_BIN -H $NAGIOSHOST -c $SEND_NSCA_CONFIG 1>/dev/null
}

SEND_EMAIL(){
    local MESSAGE="$1"
    local EXIT_CODE="$2"
    case $EXIT_CODE in 
        0) MESSAGE="OK: $MESSAGE" ;;
        1) MESSAGE="ERROR: $MESSAGE" ;;
        2) MESSAGE="WARNING: $MESSAGE" ;;
        3) MESSAGE="UNKNOWN: $MESSAGE" ;;
    esac
    echo "$MESSAGE" | $MAILX -s "[$LOGNAME] on $FQHOSTNAME" $EMAIL
}

HANDLE_MESSAGE(){
    local MESSAGE="$1"
    local EXIT_CODE="$2"
    case "$USE_NSCA" in
        [Yy][Ee][Ss]) SEND_NSCA "$MESSAGE" "$EXIT_CODE" ;;
    esac
    case "$USE_EMAIL" in
        [Yy][Ee][Ss]) SEND_EMAIL "$MESSAGE" "$EXIT_CODE" ;;
    esac
}

test_influxdb_alive(){
    local tmpfile="$1"
    if [ -x "$SYSTEMCTL" ]; then
        if $($SYSTEMCTL status influxdb 1>/dev/null 2>>"$tmpfile"); then 
            if [ -s "$tmpfile" ]; then
                HANDLE_MESSAGE "$(cat $tmpfile)" 1
                LOG "$(cat "$tmpfile")"
                cleanup_and_exit 1
            fi
        else
            HANDLE_MESSAGE "InfluxDB not running" 1
            LOG "ERROR: InfluxDB seems not up and running"
            cleanup_and_exit 1
        fi
    else 
        HANDLE_MESSAGE "Could not execute $SYSTEMCTL" 1
        LOG "ERROR: Could not execute $SYSTEMCTL"
        cleanup_and_exit 1
    fi
}

verifyDB() {
    LOG "Starting verification of the databases/tables"
    $INFLUX_INSPECT verify --dir "/var/lib/influxdb/"
}

print_help(){
    echo "Usage: $(basename $0) [-c <configfile>]"
    echo 
    echo "       -d              : print debug output"
    echo "       -f              : force backup (incl. remove backups in same directory)"
    echo "       -h              : print this message"
    echo "       -c <configfile> : use the given config file instead of $DISTCONFIG"
    echo
    echo " The target of this script is to create backups of your InfluxDB databases"
    echo " on a regular daily bases."
    echo " Please find the configuration details in $DISTCONFIG - if you want to"
    echo " test something, the option -f might be useful to use another file containing"
    echo " the configuration."
    echo
    echo " PLEASE NOTE: "
    echo "  Please have a look at /usr/share/doc/packages/influxdb-backupscript/README.SUSE"
    echo "  as this contains information for password protected databases and further"
    echo "  details."
    exit 0
}

create_dir(){
    local directory="$1"
    mkdir -p "$directory" 2>"$TMPFILE" || { 
        HANDLE_MESSAGE "$(cat $TMPFILE)" 1
        LOG "$(cat "$TMPFILE")"
        cleanup_and_exit 1
    }
}

trap cleanup_and_exit 0 1 2 3 7 13 15

while getopts 'dhfc:'  OPTION ; do
    case $OPTION in
        d) DEBUG='yes'
        ;;
        f) START_BACKUP='yes'
           FORCE_BACKUP='yes'
        ;;
        h) print_help
        ;;
        c) DISTCONFIG="$OPTARG"
        ;;
    esac
done
shift $(( OPTIND - 1 ))

for binary in "$INFLUXD" "$SYSTEMCTL" ; do
    if [ ! -x "$binary" ]; then
        LOG "ERROR: bnary $binary can not be executed"
        HANDLE_MESSAGE "Binary: $binary can not be executed" 1
    fi
done
case $USE_NSCA in 
    [Yy][Ee][Ss]) 
        if [ ! -x "$SEND_NSCA_BIN" ]; then 
            LOG "ERROR: $SEND_NSCA_BIN can not be executed"
            cleanup_and_exit 1
        fi
        if [ ! -r "$SEND_NSCA_CONFIG" ]; then
            LOG "ERROR: can not read $SEND_NSCA_CONFIG" 
            cleanup_and_exit 1
        fi
    ;;
esac
case "$USE_EMAIL" in
    [Yy][Ee][Ss]) 
        if [ ! -x "$MAILX" ]; then
            LOG "ERROR: $MAILX can not be executed"
        fi
    ;;
esac

# source our config
if [ -f "$DISTCONFIG" ]; then
    . "$DISTCONFIG"
    LOGDIR=$(dirname "$LOGFILE")
else
    echo "$DISTCONFIG not found - using defaults" >&2
    LOGDIR=$(dirname "$LOGFILE")
    LOG "$DISTCONFIG not found - using defaults"
fi

case "$START_BACKUP" in
  [Yy]*)
    DATE=$(date "+%Y%m%d")
    BEGIN_SECONDS=$(date +%s)
    TMPFILE=$(mktemp /tmp/influxdb-backupscript-XXXXXX)
    if ! test -d "$BACKUPDIR" -a "$FORCE_BACKUP" != 'yes' ; then
        case "$CREATE_BACKUPDIR" in 
            [Nn][Oo])
                HANDLE_MESSAGE "$BACKUPDIR does not exist. Exiting as CREATE_BACKUPDIR is set to 'no' in $DISTCONFIG" 1
                LOG "ERROR: $BACKUPDIR does not exist. Exiting as CREATE_BACKUPDIR is set to 'no' in $DISTCONFIG"
                cleanup_and_exit 1
            ;;
            [Ss][Kk][Ii][Pp])
                cleanup_and_exit 0
            ;;
            *)
                create_dir "$BACKUPDIR"
            ;;
        esac
    fi
    if [ -d "$BACKUPDIR/$DATE" ]; then
        if [ "$FORCE_BACKUP" == 'yes' ]; then
            LOG "$BACKUPDIR/$DATE already exists - cleaning up, as option --force is used"
            rm -rf "$BACKUPDIR/$DATE"
            create_dir "$BACKUPDIR/$DATE"
        else
            HANDLE_MESSAGE "$BACKUPDIR/$DATE exists"
            LOG "$BACKUPDIR/$DATE already exists - aborting backup"
            cleanup_and_exit 1
        fi
    else
        create_dir "$BACKUPDIR/$DATE"
    fi
    pushd "$BACKUPDIR/$DATE" 1>/dev/null
    if [ -r /etc/influxdb/config.toml ]; then
        LOG "Creating backup of InfluxDB configuration"
        if [ -d /etc/influxdb ]; then
            CONFIG_FILES="$(ls /etc/influxdb/*.toml 2>/dev/null)"
            if [ -n "$CONFIG_FILES" ]; then
               LOG "Backing up config files in /etc/influxdb/"
               create_dir "$BACKUPDIR/$DATE/etc/influxdb"
               for file in $CONFIG_FILES; do
                   cp "$file" "$BACKUPDIR/$DATE/etc/influxdb"
               done
            fi
        fi
    fi
    test_influxdb_alive "$TMPFILE"
    # start the real backup
    LOG "Creating backup in $BACKUPDIR/$DATE"
    $INFLUXD backup $INFLUXDBOPTS "$BACKUPDIR/$DATE" >> "$LOGFILE"
    # finished
    case "$VERIFY_DB" in
        [Yy][Ee][Ss])
            verifyDB
        ;;
    esac
    if [ -s "$TMPFILE" ]; then
        HANDLE_MESSAGE "InfluxDB: $(cat $TMPFILE)" 1
        LOG "$(cat "$TMPFILE")"
        cleanup_and_exit 1
    fi
    popd 1>/dev/null
    chmod 750 "$BACKUPDIR/$DATE"
    if [ -n "$RETENTION" -a "$RETENTION" -gt 0 ]; then
        LOG "removing backups older than $RETENTION days"
        find "$BACKUPDIR" -ctime +$RETENTION -print0 | xargs -0 rm -rf {} | grep -v "No such file or directory" 
    fi
    END_SECONDS=$(date +%s)
    RUNTIME_RAW_SECONDS=$[$END_SECONDS-$BEGIN_SECONDS]
    RUNTIME_HOURS=$[$RUNTIME_RAW_SECONDS/3600]
    RUNTIME_MINUTES=$[$[$RUNTIME_RAW_SECONDS%3600]/60]
    RUNTIME_SECONDS=$[$[$RUNTIME_RAW_SECONDS%3600]%60]
    LOG "backup finished. Duration (hh:mm:ss): $RUNTIME_HOURS:$RUNTIME_MINUTES:$RUNTIME_SECONDS"
    case $USE_NSCA in
       [Yy][Ee][Ss])
           SEND_NSCA "Finished: $(date); Duration (hh:mm:ss): $RUNTIME_HOURS:$RUNTIME_MINUTES:$RUNTIME_SECONDS | time=$RUNTIME_RAW_SECONDS;" 0
       ;;
    esac
    cleanup_and_exit 0
  ;;
  *)
    cleanup_and_exit 0
  ;;
esac
cleanup_and_exit 0
