#!/usr/bin/env bash
set -e

BACKUP_DIR="./backup"
TIMESTAMP=$(date +"%Y%m%d-%H%M")
BACKUP_FILE="${BACKUP_DIR}/opengist-${TIMESTAMP}.tar.gz"

mkdir -p "${BACKUP_DIR}"

echo "📦 Creating backup: ${BACKUP_FILE}"

POD=$(kubectl get pod -l app=opengist -o jsonpath='{.items[0].metadata.name}')

if [ -z "${POD}" ]; then
  echo "❌ OpenGist pod not found"
  exit 1
fi

kubectl exec "${POD}" -- \
  tar czf - /opengist > "${BACKUP_FILE}"

echo "✅ Backup completed: ${BACKUP_FILE}"

