#!/bin/bash
# Backup /mnt/misc/alvis and /mnt/misc/liza to /mnt/backups/users/
# Runs every 3 days via root crontab.
# Notifies Zabbix (item users.backup.ts, id 70379 on AgapHost) after success.

set -euo pipefail

DEST=/mnt/backups/users

mkdir -p "$DEST/alvis" "$DEST/liza"

rsync -a --delete /mnt/misc/alvis/ "$DEST/alvis/"
rsync -a --delete /mnt/misc/liza/ "$DEST/liza/"

echo "$(date): Backup complete."

# Notify Zabbix (token stored in /root/.zabbix_token)
if [[ -f /root/.zabbix_token ]]; then
  ZABBIX_TOKEN=$(cat /root/.zabbix_token)
  curl -s -X POST http://localhost:81/api_jsonrpc.php \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $ZABBIX_TOKEN" \
    -d "{\"jsonrpc\":\"2.0\",\"method\":\"history.push\",\"id\":1,\"params\":{\"itemid\":\"70379\",\"value\":\"$(date '+%Y-%m-%d %H:%M')\"}}" > /dev/null \
    && echo "Zabbix notified."
fi
