#!/bin/bash
#
# Script:  mirror_bucket_from_minio_server.sh <MINIO_URL> <STAGE> <ACCESSKEY> <SECRETKEY> <BUCKET>
#          This script is run as a cron job.
#          It mirrors an S3 bucket to the local backup directory.
#          A readonly user is used for S3 access.
# Example: mirror_bucket_from_minio_server.sh https://s3storage-minio-devnso.smardigo.digital devnso minio-readonly-devnso secretkey wordpress
# Version: 1.0
# Author:  ext.Hans-Peter.Wissenbach@netgo.de
# History: 2023.07.27 - rework previous script pull_from_minio_server.sh and add bucket
#          2023.08.09 - add mirror result and error detection

MINIO_URL=$1
STAGE=$2
ACCESSKEY=$3
SECRETKEY=$4
BUCKET=$5

LOCAL_BACKUP_DIR="${HOME}/backups/${STAGE}/bucket/${BUCKET}"
METRICS_FILE="${HOME}/metrics_${STAGE}_${BUCKET}.prom"
ALIAS="${STAGE}"

BACKUP_START=$(date +%s)

mkdir -p ${LOCAL_BACKUP_DIR}

rm -rf ${LOCAL_BACKUP_DIR}/*

mcli alias set ${ALIAS} ${MINIO_URL} ${ACCESSKEY} ${SECRETKEY}
RC="$?"

if [[ ${RC} -eq "0" ]]
then
  RESULT=$(mcli mirror ${ALIAS}/${BUCKET} ${LOCAL_BACKUP_DIR} --overwrite --newer-than 1d --json)
  echo "${RESULT}"
  TRANSFERRED=$(echo "${RESULT}" | jq '.transferred | select(. != null)')
  TRANSFERRED="${TRANSFERRED:=0}" # default 0 if not set
  STATUS=$(echo "${RESULT}" | jq -e '.status == "success"')
  RC="$?"

  mcli alias rm ${ALIAS}
fi

BACKUP_END=$(date +%s)

if [[ ${RC} -eq "0" ]] && [[ ${TRANSFERRED} > 0 ]]
then
  echo "Nightly Backup Successful - writing METRICS_FILE: ${METRICS_FILE}"
  tee <<EOF > ${METRICS_FILE}
# HELP nightly_backup_transfer_started_seconds_${STAGE}_${BUCKET} System time in seconds since epoch (1970).
# TYPE nightly_backup_transfer_started_seconds_${STAGE}_${BUCKET} gauge
nightly_backup_transfer_started_seconds_${STAGE}_${BUCKET}{stage="${STAGE}"} ${BACKUP_START}

# HELP nightly_backup_successful_${STAGE}_${BUCKET}
# TYPE nightly_backup_successful_${STAGE}_${BUCKET} gauge
nightly_backup_successful_${STAGE}_${BUCKET}{stage="${STAGE}"} 0

# HELP nightly_backup_transferred_bytes_${STAGE}_${BUCKET}
# TYPE nightly_backup_transferred_bytes_${STAGE}_${BUCKET} gauge
nightly_backup_transferred_bytes_${STAGE}_${BUCKET}{stage="${STAGE}"} ${TRANSFERRED}

# HELP nightly_backup_transfer_ended_seconds_${STAGE}_${BUCKET} System time in seconds since epoch (1970).
# TYPE nightly_backup_transfer_ended_seconds_${STAGE}_${BUCKET} gauge
nightly_backup_transfer_ended_seconds_${STAGE}_${BUCKET}{stage="${STAGE}"} ${BACKUP_END}
EOF
else
  echo "Nightly Backup Failed - writing METRICS_FILE: ${METRICS_FILE}"
  tee <<EOF > ${METRICS_FILE}
# HELP nightly_backup_transfer_started_seconds_${STAGE}_${BUCKET} System time in seconds since epoch (1970).
# TYPE nightly_backup_transfer_started_seconds_${STAGE}_${BUCKET} gauge
nightly_backup_transfer_started_seconds_${STAGE}_${BUCKET}{stage="${STAGE}"} ${BACKUP_START}

# HELP nightly_backup_successful_${STAGE}_${BUCKET}
# TYPE nightly_backup_successful_${STAGE}_${BUCKET} gauge
nightly_backup_successful_${STAGE}_${BUCKET}{stage="${STAGE}"} 1

# HELP nightly_backup_transferred_bytes_${STAGE}_${BUCKET}
# TYPE nightly_backup_transferred_bytes_${STAGE}_${BUCKET} gauge
nightly_backup_transferred_bytes_${STAGE}_${BUCKET}{stage="${STAGE}"} ${TRANSFERRED}

# HELP nightly_backup_transfer_ended_seconds_${STAGE}_${BUCKET} System time in seconds since epoch (1970).
# TYPE nightly_backup_transfer_ended_seconds_${STAGE}_${BUCKET} gauge
nightly_backup_transfer_ended_seconds_${STAGE}_${BUCKET}{stage="${STAGE}"} ${BACKUP_END}
EOF
fi

exit ${RC}
