#!/bin/bash
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
# vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
#
#                          |\_
# B A C K U P N I N J A   /()/
#                         `\|
#
# Copyright (C) 2004-05 riseup.net -- property is theft.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#

#####################################################
## FUNCTIONS

function setupcolors () {
   BLUE="\033[34;01m"
   GREEN="\033[32;01m"
   YELLOW="\033[33;01m"
   PURPLE="\033[35;01m"
   RED="\033[31;01m"
   OFF="\033[0m"
   CYAN="\033[36;01m"
   COLORS=($BLUE $GREEN $YELLOW $RED $PURPLE $CYAN)
}

function colorize () {
   if [ "$usecolors" == "yes" ]; then
      local typestr=`echo "$@" | /bin/sed 's/\(^[^:]*\).*$/\1/'`
      [ "$typestr" == "Debug" ] && type=0
      [ "$typestr" == "Info" ] && type=1
      [ "$typestr" == "Warning" ] && type=2
      [ "$typestr" == "Error" ] && type=3
      [ "$typestr" == "Fatal" ] && type=4
      [ "$typestr" == "Halt" ] && type=5
      color=${COLORS[$type]}
      endcolor=$OFF
      echo -e "$color$@$endcolor"
   else
      echo -e "$@"
   fi
}

# We have the following message levels:
# 0 - debug - blue
# 1 - normal messages - green
# 2 - warnings - yellow
# 3 - errors - red
# 4 - fatal - purple
# 5 - halt - cyan
# First variable passed is the error level, all others are printed

# if 1, echo out all warnings, errors, or fatal
# used to capture output from handlers
echo_debug_msg=0

usecolors=yes

function printmsg() {
   [ ${#@} -gt 1 ] || return

   type=$1
   shift
   if [ $type == 100 ]; then
      typestr=`echo "$@" | /bin/sed 's/\(^[^:]*\).*$/\1/'`
      [ "$typestr" == "Debug" ] && type=0
      [ "$typestr" == "Info" ] && type=1
      [ "$typestr" == "Warning" ] && type=2
      [ "$typestr" == "Error" ] && type=3
      [ "$typestr" == "Fatal" ] && type=4
      [ "$typestr" == "Halt" ] && type=5
      typestr=""
   else
      types=(Debug Info Warning Error Fatal Halt)
      typestr="${types[$type]}: "
   fi

   print=$[4-type]

   if [ $echo_debug_msg == 1 ]; then
      echo -e "$typestr$@" >&2
   elif [ $debug ]; then
      colorize "$typestr$@" >&2
   fi

   if [ $print -lt $loglevel ]; then
      logmsg "$typestr$@"
   fi
}

function logmsg() {
   if [ -w "$logfile" ]; then
      echo -e `LC_ALL=C date "+%h %d %H:%M:%S"` "$@" >> $logfile
   fi
}

function passthru() {
   printmsg 100 "$@"
}
function debug() {
   printmsg 0 "$@"
}
function info() {
   printmsg 1 "$@"
}
function warning() {
   printmsg 2 "$@"
}
function error() {
   printmsg 3 "$@"
}
function fatal() {
   printmsg 4 "$@"
   exit 2
}
function halt() {
   printmsg 5 "$@"
   exit 2
}

msgcount=0
function msg {
   messages[$msgcount]=$1
   let "msgcount += 1"
}

#
# enforces very strict permissions on configuration file $file.
#

function check_perms() {
   local file=$1
   debug "check_perms $file"
   local perms
   local owners

   perms=($(/usr/bin/stat -L --format='%A' $file))
   debug "perms: $perms"
   local gperm=${perms:4:3}
   debug "gperm: $gperm"
   local wperm=${perms:7:3}
   debug "wperm: $wperm"

   owners=($(/usr/bin/stat -L --format='%g %G %u %U' $file))
   local gid=${owners[0]}
   local group=${owners[1]}
   local owner=${owners[2]}

   if [ "$owner" != 0 ]; then
      echo "Configuration files must be owned by root! Dying on file $file"
      fatal "Configuration files must be owned by root! Dying on file $file"
   fi

   if [ "$wperm" != '---' ]; then
      echo "Configuration files must not be world writable/readable! Dying on file $file"
      fatal "Configuration files must not be world writable/readable! Dying on file $file"
   fi

   if [ "$gperm" != '---' ]; then
      case "$admingroup" in
         $gid|$group) :;;

         *)
           if [ "$gid" != 0 ]; then
              echo "Configuration files must not be writable/readable by group $group! Use the admingroup option in backupninja.conf. Dying on file $file"
              fatal "Configuration files must not be writable/readable by group $group! Use the admingroup option in backupninja.conf. Dying on file $file"
           fi
         ;;
         esac
   fi
}

# simple lowercase function
function tolower() {
   echo "$1" | tr '[:upper:]' '[:lower:]'
}

# simple to integer function
function toint() {
   echo "$1" | /bin/sed 's/[^0-9]//g;s/^0\+//'
}

#
# function isnow(): returns 1 if the time/day passed as $1 matches
# the current time/day.
#
# format is <day> at <time>:
#   sunday at 16
#   8th at 01
#   everyday at 22
#

# we grab the current time once, since processing
# all the configs might take more than an hour.
nowtime=`LC_ALL=C date +%H`
nowday=`LC_ALL=C date +%-d`
nowdayofweek=`LC_ALL=C date +%A`
nowdayofweek=`tolower "$nowdayofweek"`

function isnow() {
   local when="$1"
   set -- $when

   [ "$when" == "manual" ] && return 0

   whendayofweek=$1; at=$2; whentime=$3;
   whenday=`toint "$whendayofweek"`
   whendayofweek=`tolower "$whendayofweek"`
   whentimeoriginal=$whentime
   whentime=`echo "$whentime" | /bin/sed 's/:[0-9][0-9]$//' | /bin/sed -r 's/^([0-9])$/0\1/'`

   if [ "$whendayofweek" == "everyday" -o "$whendayofweek" == "daily" ]; then
       whendayofweek=$nowdayofweek
   fi

   if [ "$whenday" == "" ]; then
       if [ "$whendayofweek" != "$nowdayofweek" ]; then
           whendayofweek=${whendayofweek%s}
           if [ "$whendayofweek" != "$nowdayofweek" ]; then
               if [ "$whendayofweek" != "monday" -a "$whendayofweek" != "tuesday" -a "$whendayofweek" != "wednesday" -a "$whendayofweek" != "thursday" -a "$whendayofweek" != "friday" -a "$whendayofweek" != "saturday" -a "$whendayofweek" != "sunday" ]; then
                   warning "The day in the 'when' option in the configuration is malformed. Please read the README for examples."
               fi
               if [ "$at" != "at" ]; then
                   warning "The 'when' option in the configuration doesn't contain an 'at'. Please read the README for examples."
               fi
               if [[ "$whentimeoriginal" != @([0-9][0-9])?(:[0-9][0-9]) ]]; then
                   warning "The time in the 'when' option in the configuration is malformed or does not exist. Please read the README for examples."
               fi
               return 0
           fi
       fi
   elif [ "$whenday" != "$nowday" ]; then
       if [ "$whenday" -lt 1 -o "$whenday" -gt 31 ]; then
           warning "The numeric value of the day in the 'when' option in the configuration does not match a day. Please read the README for examples."
       fi
       if [ -z "$whentime" ]; then
           warning "The 'when' variable in the configuration file is malformed. You either didn't specify the day or the time. Please read the README for eamples."
       fi
       if [ "$at" != "at" ]; then
           warning "The 'when' option in the configuration doesn't contain an 'at'. Please read the README for examples."
       fi
       return 0
   elif [ -z "$whentime" ]; then
       warning "The 'when' variable in the configuration file is malformed. You either didn't specify the day or the time. Please read the README for examples."
       return 0
   fi

   if [ "$at" != "at" ]; then
       warning "The 'when' option in the configuration doesn't contain an 'at'. Please read the README for examples."
       return 0
   fi

   if [[ "$whentimeoriginal" != @([0-9][0-9])?(:[0-9][0-9]) ]]; then
       warning "The time in the 'when' option in the configuration is malformed. Please read the README for examples."
       return 0
   elif [ "$whentime" != "$nowtime" ]; then
       return 0
   fi

   return 1
}

function usage() {
   cat << EOF
$0 usage:
This script allows you to coordinate system backup by dropping a few
simple configuration files into /etc/backup.d/. Typically, this
script is run hourly from cron.

The following options are available:
-h, --help           This usage message
-V, --version        Show backupninja version number
-d, --debug          Run in debug mode, where all log messages are
                     output to the current shell.
-f, --conffile FILE  Use FILE for the main configuration instead
                     of /etc/backupninja.conf
-t, --test           Test run mode. This will test if the backup
                     could run, without actually preforming any
                     backups. For example, it will attempt to authenticate
                     or test that ssh keys are set correctly.
-n, --now            Perform actions now, instead of when they might
                     be scheduled. No output will be created unless also
                     run with -d.
    --run FILE       Execute the specified action file and then exit.
                     Also puts backupninja in debug mode.

When in debug mode, output to the console will be colored:
EOF
   usecolors=yes
   colorize "Debug: Debugging info (when run with -d)"
   colorize "Info: Informational messages (verbosity level 4)"
   colorize "Warning: Warnings (verbosity level 3 and up)"
   colorize "Error: Errors (verbosity level 2 and up)"
   colorize "Fatal: Errors which halt a given backup action (always shown)"
   colorize "Halt: Errors which halt the whole backupninja run (always shown)"
}

##
## this function handles the running of a backup action
##
## these globals are modified:
## halts, fatals, errors, warnings, actions_run, errormsg
##

function process_action() {
   local file="$1"
   local suffix="$2"
   local run="no"
   setfile $file

   # skip over this config if "when" option
   # is not set to the current time.
   getconf when "$defaultwhen"
   if [ "$processnow" == 1 ]; then
      info ">>>> starting action $file (because of --now)"
      run="yes"
   elif [ "$when" == "hourly" ]; then
      info ">>>> starting action $file (because 'when = hourly')"
      run="yes"
   else
      IFS=$'\t\n'
      for w in $when; do
         IFS=$' \t\n'
         isnow "$w"
         ret=$?
         IFS=$'\t\n'
         if [ $ret == 0 ]; then
            debug "skipping $file because current time does not match $w"
         else
            info ">>>> starting action $file (because current time matches $w)"
            run="yes"
         fi
      done
      IFS=$' \t\n'
   fi
   debug $run
   [ "$run" == "no" ] && return

   # Prepare for lock creation
   if [ ! -d /var/lock/backupninja ]; then
      mkdir /var/lock/backupninja
   fi
   lockfile=`echo $file | /bin/sed 's,/,_,g'`
   lockfile=/var/lock/backupninja/$lockfile

   local bufferfile=`maketemp backupninja.buffer`
   echo "" > $bufferfile

   # start locked section : avoid concurrent execution of the same backup
   # uses a construct specific to shell scripts with flock. See man flock for details
   {
      debug "executing handler in locked section controlled by $lockfile"
      flock -x -w 5 200
      # if all is good, we acquired the lock
      if [ $? -eq 0 ]; then

         let "actions_run += 1"

         # call the handler:
         echo_debug_msg=1
         (
            . $scriptdirectory/$suffix $file
         ) 2>&1 | (
            while read a; do
               echo $a >> $bufferfile
               [ $debug ] && colorize "$a"
            done
         )
         retcode=${PIPESTATUS[0]}
         echo_debug_msg=0
         debug "handler returned exit code $retcode"

      else
         # a backup is probably ongoing already, so display an error message
         debug "failed to acquire lock $lockfile"
         echo "Fatal: Could not acquire lock $lockfile. A backup is probably already running for $file." >>$bufferfile
      fi
   } 200> $lockfile
   # end of locked section

   _warnings=`cat $bufferfile | grep "^Warning: " | wc -l`
   _errors=`cat $bufferfile | grep "^Error: " | wc -l`
   _fatals=`cat $bufferfile | grep "^Fatal: " | wc -l`
   _halts=`cat $bufferfile | grep "^Halt: " | wc -l`
   _infos=`cat $bufferfile | grep "^Info: " | wc -l`

   ret=`grep "\(^Info: \|^Warning: \|^Error: \|^Fatal: \|Halt: \)" $bufferfile`
   rm $bufferfile

   if [ $_halts != 0 ]; then
      msg "*halt* -- $file"
      errormsg="$errormsg\n== halt request from $file==\n\n$ret\n"
      passthru "Halt: <<<< finished action $file: FAILED"
   elif [ $_fatals != 0 ]; then
      msg "*failed* -- $file"
      errormsg="$errormsg\n== fatal errors from $file ==\n\n$ret\n"
      passthru "Fatal: <<<< finished action $file: FAILED"
   elif [ $_errors != 0 ]; then
      msg "*error* -- $file"
      errormsg="$errormsg\n== errors from $file ==\n\n$ret\n"
      error "<<<< finished action $file: ERROR"
   elif [ $_warnings != 0 ]; then
      msg "*warning* -- $file"
      errormsg="$errormsg\n== warnings from $file ==\n\n$ret\n"
      warning "<<<< finished action $file: WARNING"
   else
      msg "success -- $file"
      if [ $_infos != 0 -a "$reportinfo" == "yes" ]; then
         errormsg="$errormsg\n== infos from $file ==\n\n$ret\n"
      fi
      info "<<<< finished action $file: SUCCESS"
   fi

   let "halts += _halts"
   let "fatals += _fatals"
   let "errors += _errors"
   let "warnings += _warnings"
}

#####################################################
## MAIN

setupcolors
conffile="/etc/backupninja.conf"
loglevel=3
version="1.2.2"

## process command line options

while [ $# -ge 1 ]; do
   case $1 in
      -h|--help) usage;;
      -V|--version) echo "backupninja $version";;
      -d|--debug) debug=1; export BACKUPNINJA_DEBUG=yes;;
      -t|--test) test=1;debug=1;;
      -n|--now) processnow=1;;
      -f|--conffile)
         if [ -f $2 ]; then
            conffile=$2
         else
            echo "-f|--conffile option must be followed by an existing filename"
            fatal "-f|--conffile option must be followed by an existing filename"
            usage
         fi
         # we shift here to avoid processing the file path
         shift
         ;;
      --run)
         debug=1
         if [ -f $2 ]; then
            singlerun=$2
            processnow=1
         else
            echo "--run option must be followed by a backupninja action file"
            fatal "--run option must be followed by a backupninja action file"
            usage
         fi
         shift
         ;;
      *)
         debug=1
         error "Unknown option $1"
         usage
         exit 3
         ;;
   esac
   shift
done

#if [ $debug ]; then
#   usercolors=yes
#fi

## Load and confirm basic configuration values

# bootstrap
if [ ! -r "$conffile" ]; then
   echo "Configuration file $conffile not found."
   fatal "Configuration file $conffile not found."
fi

# find $libdirectory
libdirectory=`grep '^libdirectory' $conffile | /usr/bin/awk '{print $3}'`
if [ -z "$libdirectory" ]; then
   if [ -d "/usr/lib/backupninja" ]; then
      libdirectory="/usr/lib/backupninja"
   else
      echo "Could not find entry 'libdirectory' in $conffile."
      fatal "Could not find entry 'libdirectory' in $conffile."
   fi
else
   if [ ! -d "$libdirectory" ]; then
      echo "Lib directory $libdirectory not found."
      fatal "Lib directory $libdirectory not found."
   fi
fi

# include shared functions
. $libdirectory/tools

setfile $conffile

# get global config options (second param is the default)
getconf configdirectory /etc/backup.d
getconf scriptdirectory /usr/share/backupninja
getconf reportdirectory
getconf reportprom
getconf prom_textfile_dir /var/lib/prometheus/node-exporter
getconf reportemail
getconf reporthost
getconf reportspace
getconf reportsuccess yes
getconf reportinfo no
getconf reportuser
getconf reportwarning yes
getconf loglevel 3
getconf when "Everyday at 01:00"
defaultwhen=$when
getconf logfile /var/log/backupninja.log
getconf usecolors "yes"
getconf SLAPCAT /usr/sbin/slapcat
getconf LDAPSEARCH /usr/bin/ldapsearch
getconf RDIFFBACKUP /usr/bin/rdiff-backup
getconf CSTREAM /usr/bin/cstream
getconf MYSQLADMIN /usr/bin/mysqladmin
getconf MYSQL /usr/bin/mysql
getconf MYSQLHOTCOPY /usr/bin/mysqlhotcopy
getconf MYSQLDUMP /usr/bin/mysqldump
getconf PSQL /usr/bin/psql
getconf PGSQLDUMP /usr/bin/pg_dump
getconf PGSQLDUMPALL /usr/bin/pg_dumpall
getconf PGSQLUSER postgres
getconf GZIP /bin/gzip
getconf GZIP_OPTS --rsyncable
getconf RSYNC /usr/bin/rsync
getconf DSYNC /usr/bin/dsync
getconf DOVEADM /usr/bin/doveadm
getconf admingroup root
getconf reportwrap 1000

if [ ! -d "$configdirectory" ]; then
   echo "Configuration directory '$configdirectory' not found."
   fatal "Configuration directory '$configdirectory' not found."
fi

[ -f "$logfile" ] || touch $logfile

if [ "$UID" != "0" ]; then
   echo "`basename $0` can only be run as root"
   exit 3
fi

## Process each configuration file

# by default, don't make files which are world or group readable.
umask 077

# these globals are set by process_action()
halts=0
fatals=0
errors=0
warnings=0
actions_run=0
errormsg=""

if [ "$singlerun" ]; then
   files=$singlerun
else
   files=`find $configdirectory -follow -mindepth 1 -maxdepth 1 -type f ! -name '.*.swp' ! -name '*~' | sort -n`

   if [ -z "$files" ]; then
      info "No backup actions configured in '$configdirectory', run ninjahelper!"
   fi
fi

for file in $files; do
   [ -f "$file" ] || continue
   [ "$halts" = "0" ] || continue

   check_perms ${file%/*} # check containing dir
   check_perms $file
   base=`basename $file`
   suffix="${base##*.}"
   if [ "${base:0:1}" == "0" -o "$suffix" == "disabled" -o "$suffix" == "$base" ]; then
      info "Skipping $file"
      continue
   fi

   if [ -e "$scriptdirectory/$suffix" ]; then
      process_action $file $suffix
   else
      error "Can't process file '$file': no handler script for suffix '$suffix'"
      msg "*missing handler* -- $file"
   fi
done

## reporting

if [ $actions_run == 0 ]; then doit=0
elif [ "$reportemail" == "" -a "$reportprom" == "" ]; then doit=0
elif [ $fatals != 0 ]; then doit=1
elif [ $errors != 0 ]; then doit=1
elif [ $halts != 0 ]; then doit=1
elif [ "$reportsuccess" == "yes" ]; then doit=1
elif [ "$reportwarning" == "yes" -a $warnings != 0 ]; then doit=1
else doit=0
fi

if [ $doit == 1 ]; then
   if [ ! -z "$reportprom" ]; then
      if [ -d "$prom_textfile_dir" ]; then
         debug "reporting to prometheus"
         hostname=`hostname`

         # set some defaults, so there aren't empty strings
         [ ! -z $warnings ] || warnings=0
         [ ! -z $errors ] || errors=0
         [ ! -z $fatals ] || fatals=0
         [ ! -z $halts ] || halts=0
         [ ! -z $actions_run ] || actions_run=0

         output_file="${prom_textfile_dir}/backupninja.prom"
         tmp_file="${output_file}.$$"
         trap "rm -f $tmp_file 2>/dev/null" EXIT INT TERM
         cat <<EOF > "$tmp_file"
# HELP backupninja_warnings Number of warnings reported by Backupninja
# TYPE backupninja_warnings gauge
# HELP backupninja_errors Number of errors reported by Backupninja
# TYPE backupninja_errors gauge
# HELP backupninja_fatals Number of fatals reported by Backupninja
# TYPE backupninja_fatals gauge
# HELP backupninja_halts Number of halts reported by Backupninja
# TYPE backupninja_halts gauge
# HELP backupninja_actions Number of actions run by Backupninja
# TYPE backupninja_actions gauge
backupninja_warnings{host="$hostname"} $warnings
backupninja_errors{host="$hostname"} $errors
backupninja_fatals{host="$hostname"} $fatals
backupninja_halts{host="$hostname"} $halts
backupninja_actions{host="$hostname"} $actions_run
EOF
         if [ $? -gt 0 ]; then
            rm -f "$tmp_file" 2>/dev/null
            error "could not write metrics to ${prom_textfile_dir}!"
            let "errors +-1"
         else
            mv -f "$tmp_file" "$output_file"
            chmod 0644 "$output_file"
         fi
      else
         error "$prom_textfile_dir does not exist!"
         let "errors +-1"
      fi
   fi
   if [ ! -z "$reportemail" ]; then
      if [ -x "$(which mail 2>/dev/null)" ]; then
         debug "send report to $reportemail"
         hostname=`hostname`
         [ $warnings == 0 ] || subject="WARNING"
         [ $errors == 0 ] || subject="ERROR"
         [ $fatals == 0 ] || subject="FAILED"
         [ $halts == 0 ] || subject="HALTED"

         {
            for ((i=0; i < ${#messages[@]} ; i++)); do
               echo ${messages[$i]}
            done
            echo -e "$errormsg"
            if [ "$reportspace" == "yes" ]; then
               previous=""
               for i in $(ls "$configdirectory"); do
                  backuploc=$(grep ^directory "$configdirectory"/"$i" | /usr/bin/awk '{print $3}')
                  if [ "$backuploc" != "$previous" -a -n "$backuploc" -a -d "$backuploc" ]; then
                     df -h "$backuploc"
                     previous="$backuploc"
                  fi
               done
            fi
         } | fold -s -w "$reportwrap" | mail -s "backupninja: $hostname $subject" $reportemail
      else
         error "Unable to locate mail executable, email report not sent!"
         let "errors += 1"
      fi
   fi
fi
if [ $actions_run != 0 ]; then
   info "FINISHED: $actions_run actions run. $fatals fatal. $errors error. $warnings warning."
   if [ "$halts" != "0" ]; then
      info "Backup was halted prematurely.  Some actions may not have run."
   fi
fi

if [ -n "$reporthost" ]; then
   if [ -z "$reportuser" ] || [ -z "$reportdirectory" ]; then
      error "Unable to send report, reportuser and reportdirectory must be specified."
      let "errors += 1"
   elif [ ! -x "$(which rsync 2>/dev/null)" ]; then
      error "Unable to locate rsync executable, report could not be sent to ${reporthost}."
      let "errors += 1"
   else
      info "Sending $logfile to $reportuser@$reporthost:$reportdirectory"
      execstr="rsync -qt $logfile $reportuser@$reporthost:$reportdirectory"
      debug $execstr
      output=$(eval $execstr 2>&1)
      ret=$?
      if [ $ret -ne 0 ]; then
         error $output
         error "An error was encountered attempting to send report to ${reporthost}."
         let "errors += 1"
      fi
   fi
fi

# unset exported envvars
unset BACKUPNINJA_DEBUG

# return exit code
[ $halts == 0 ] || exit 2
[ $fatals == 0 ] || exit 2
[ $errors == 0 ] || exit 1
[ "$reportwarning" == "yes" -a $warnings != 0 ] && exit 1

exit 0
