#!/usr/bin/bash

# Bash script on backup server that will obtain the Domoticz database from the host server
# and save to file:  /home/becky/DomoticzBackups/domoticz-20200528-1342.db.gz

# Domoticz parameters
#
HOST="goldserver.local"    # or explicit IP address ok; "127.0.0.1" and "localhost" may not work
PORT="8080"                # default HTTP port, could be different

# Local directory to contain downloaded database
#
DESTDIR=/home/becky/DomoticzBackups/

# Create timestamped backup files
#
TIMESTAMP=`/bin/date +%Y%m%d-%H%M`
BACKUPFILE="domoticz_$TIMESTAMP.db" # backups will be named something like "domoticz-20200622-1042.db"

# Ask Domoticz to copy the database to the local destination directory
#
curl -s "http://$HOST:$PORT/backupdatabase.php" > $DESTDIR$BACKUPFILE
curlerr=$?

# Stop if this did not work, logging error
#
if [ $curlerr -ne 0 ]; then
  logger -p err "Domoticz backup database not created (error: $curlerr)"
  exit 1
fi

# Compress the database, ".gz" will be appended to the filename
#
gzip -9 $DESTDIR$BACKUPFILE
