#!/bin/bash
#
#
#

# Fail fast and be aware of exit codes
set -euo pipefail

# Define some variables
DATE=$(date +%F)
DATE_TIME=$(date +%F_%H:%M)
REMOTE_SYSTEM_USER=backupuser
DATABASE_SERVER=$1
STAGE=$2
DATABASE_ENGINE=$3
DEST_DIR=${HOME}/backups/${STAGE}/${DATABASE_ENGINE}/${DATABASE_SERVER}
METRICS_FILE=${HOME}/backup_status_${DATABASE_ENGINE}.prom
LOG_FILE=${DEST_DIR}/backup_${DATE_TIME}.log

# Create backup directory ${DEST_DIR} if not exist
mkdir -p ${DEST_DIR}

# Redirect stderr to stdout and save everything to log file
exec > ${LOG_FILE} 2>&1

# Log backup sync start time
echo "----- Start backup Sync - ${DATE_TIME} -----"

# Remove files oder than 48h in ${DEST_DIR}
find $DEST_DIR -type d -mtime +1 -print0 | xargs -I OLD_DIR -0 rm -rf "OLD_DIR"
[ "$?" != "0" ] && exit 1

echo "Removing logfiles older than 7d ..."
find $DEST_DIR -type f -mtime +7 -name "backup_*.log" -print0 | xargs -I OLD_FILES -0 rm -rf "OLD_FILES"

# Start rsync job from ${DATABASE_SERVER} to ${DEST_DIR}/
rsync -av --remove-source-files -e "ssh -o StrictHostKeyChecking=no" ${REMOTE_SYSTEM_USER}@${DATABASE_SERVER}:/backups/${DATABASE_ENGINE}/ ${DEST_DIR}/
[ "$?" -eq "0" ] && NIGHTLY_BACKUP_SUCCESSFUL="0" || NIGHTLY_BACKUP_SUCCESSFUL="1"

BACKUP_STATUS_FILE=$(ls -t1 ${DEST_DIR}/${DATE}/backup_finished_${DATE}_* | head -n1)
# Check existence of current ${BACKUP_STATUS_FILE}, which is created by AWX, in case of succesful database backup only.
[ -f ${BACKUP_STATUS_FILE} ] && NIGHTLY_BACKUP_SUCCESSFUL="0" || NIGHTLY_BACKUP_SUCCESSFUL="1"

# Add backup status to Prometheus metrics file
if [ "$NIGHTLY_BACKUP_SUCCESSFUL" -eq "0" ]; then
  echo "NIGHTLY_BACKUP_SUCCESSFUL=0 - writing METRICS_FILE"
cat <<EOF > $METRICS_FILE
# HELP nightly_backup_successful_${DATABASE_ENGINE}
# TYPE nightly_backup_successful_${DATABASE_ENGINE} gauge
nightly_backup_successful_${DATABASE_ENGINE}{stage="$STAGE"} $NIGHTLY_BACKUP_SUCCESSFUL
nightly_backup_successful_${DATABASE_ENGINE}_finished_seconds{stage="$STAGE"} `date +%s`
EOF

else
  echo "NIGHTLY_BACKUP_SUCCESSFUL=1 - removing METRICS_FILE to trigger alert"
  rm $METRICS_FILE
fi

# Log backup sync end time
echo "----- End backup Sync - ${DATE_TIME} -----"
