Zips and tars data + move them into outbox for copy to Google storage or other target.

#!/usr/bin/env bash

function echo_log() {
  echo "$(date +"%Y-%m-%d %H:%M:%S.%N"): $1"
}


homedir=$( dirname "${BASH_SOURCE[0]}")
cd $homedir
. ./.env

[ -z "$IDB_BACKUP_DIR" ] && echo_log "$(basename $0): ERROR: variable IDB_BACKUP_DIR empty or undefined" && exit 1
[ -z "$IDB_OUTBOX_DIR" ] && echo_log "$(basename $0): ERROR: variable IDB_OUTBOX_DIR empty or undefined" && exit 1

curdate=$(date +%Y%m%d%H%M)

echo_log "============= InfluxDB backup ============="

for db in $(influx -execute "show databases" -format csv|grep databases|grep -v "_internal"|cut -d',' -f2); do
  backupdir="${IDB_BACKUP_DIR}/influxdb_${db}_${curdate}"
  backuptar="${backupdir}.tar"
  echo_log "starting backup - database: $db, backupdir: $backupdir"
  influxd backup -database $db $backupdir

  echo_log "starting tar"
  rm ${backuptar}
  tar cvfz ${backuptar} ${backupdir}
  if [ $? -ne 0 ]; then
    echo_log "$(basename $0): ERROR: tar failed"
    exit 1
  fi
  rm -rf ${backupdir}
  echo_log "tar done"
  mv ${backuptar} ${IDB_OUTBOX_DIR}

done

echo_log "DONE"