#!/usr/bin/env bash
set -euo pipefail
INSTALL_DIR=/opt/telos-evm-3
LOG_DIR="$INSTALL_DIR/logs"
KEY=/root/.ssh/storagebox_ed25519
REMOTE_USER="${STORAGEBOX_USER:-u563713}"
REMOTE_HOST="${STORAGEBOX_HOST:-u563713.your-storagebox.de}"
REMOTE_PORT="${STORAGEBOX_PORT:-23}"
REMOTE_BASE="${STORAGEBOX_BASE:-mainnet-reth-archival}"
SSH_OPTS=(-i "$KEY" -p "$REMOTE_PORT" -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ConnectTimeout=20)
RSYNC_SSH="ssh -i $KEY -p $REMOTE_PORT -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ConnectTimeout=20"
mkdir -p "$LOG_DIR"
exec >>"$LOG_DIR/storagebox-backup.log" 2>&1
echo "[$(date -Is)] backup start"
if ! ssh "${SSH_OPTS[@]}" "$REMOTE_USER@$REMOTE_HOST" 'mkdir -p mainnet-reth-archival/current' ; then
  echo "[$(date -Is)] storagebox auth/path not ready; add $(cat /root/.ssh/storagebox_ed25519.pub) to the storagebox authorized_keys"
  exit 0
fi
reth_state="$(systemctl is-active telos-evm3-reth.service || true)"
consensus_state="$(systemctl is-active telos-evm3-consensus.service || true)"
restart_reth=0
restart_consensus=0
if [ "$reth_state" = active ]; then restart_reth=1; fi
if [ "$consensus_state" = active ]; then restart_consensus=1; fi
restart_services() {
  if [ "$restart_reth" = 1 ]; then systemctl start telos-evm3-reth.service || true; fi
  if [ "$restart_consensus" = 1 ]; then systemctl start telos-evm3-consensus.service || true; fi
}
trap restart_services EXIT
cat > "$INSTALL_DIR/BACKUP-MANIFEST.txt" <<MANIFEST
host=$(hostname -f 2>/dev/null || hostname)
created_at=$(date -Is)
install_dir=$INSTALL_DIR
reth_binary=$($INSTALL_DIR/telos-reth-v2/target/release/telos-reth --version 2>/dev/null || true)
consensus_binary=$($INSTALL_DIR/telos-consensus-client/target/release/telos-consensus-client --version 2>/dev/null || true)
MANIFEST
rsync -a --delete --partial --inplace -e "$RSYNC_SSH" "$INSTALL_DIR/etc/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_BASE/current/etc/"
rsync -a --delete --partial --inplace -e "$RSYNC_SSH" "$INSTALL_DIR/bin/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_BASE/current/bin/"
rsync -a --partial --inplace -e "$RSYNC_SSH" "$INSTALL_DIR/BACKUP-MANIFEST.txt" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_BASE/current/BACKUP-MANIFEST.txt"
rsync -a --delete --partial --inplace -e "$RSYNC_SSH" "$INSTALL_DIR/telos-consensus-client-data/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_BASE/current/telos-consensus-client-data/"
rsync -a --delete --partial --inplace -e "$RSYNC_SSH" "$INSTALL_DIR/telos-reth-data/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_BASE/current/telos-reth-data/"
if [ "$restart_consensus" = 1 ]; then systemctl stop telos-evm3-consensus.service; fi
if [ "$restart_reth" = 1 ]; then systemctl stop telos-evm3-reth.service; fi
rsync -a --delete --partial --inplace -e "$RSYNC_SSH" "$INSTALL_DIR/telos-consensus-client-data/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_BASE/current/telos-consensus-client-data/"
rsync -a --delete --partial --inplace -e "$RSYNC_SSH" "$INSTALL_DIR/telos-reth-data/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_BASE/current/telos-reth-data/"
echo "[$(date -Is)] backup complete"
