{{- if .Values.backup.enabled }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ .Release.Name }}-backup-script
stringData:
  backup.sh: |
    #!/bin/sh
    set -e
    set -o pipefail
    
    JOB_ID="job-$(uuidgen|cut -f1 -d-)"
    TABLE_LIST=$(clickhouse-client --host "$CLICKHOUSE_HOST" -q 'SHOW TABLES;' | grep -v '^.inner.' || true)
    echo DB_LIST=$(echo "$TABLE_LIST" | shuf) # shuffle list
    echo "Job ID: $JOB_ID"
    echo "Target repo: $REPO_PREFIX"
    echo "Cleanup strategy: $CLEANUP_STRATEGY"
    echo "Start backup for:"
    echo "$TABLE_LIST"
    echo
    echo "Backup started at `date +%Y-%m-%d\ %H:%M:%S`"
    for table in $TABLE_LIST; do
      (
        set -x
        restic -r "s3:${REPO_PREFIX}/$table" cat config >/dev/null 2>&1 || \
          restic -r "s3:${REPO_PREFIX}/$table" init --repository-version 2
        restic -r "s3:${REPO_PREFIX}/$table" unlock --remove-all >/dev/null 2>&1 || true # no locks, k8s takes care of it
        clickhouse-client --host "$CLICKHOUSE_HOST" -q "SHOW CREATE TABLE ${table}" | awk '{gsub(/\\n/, "\n")} {gsub(/\\'\''/, "'\''")} 1' | \
          restic -r "s3:${REPO_PREFIX}/$table" backup --tag "$JOB_ID" --tag index --stdin --stdin-filename index.sql
        clickhouse-client --host "$CLICKHOUSE_HOST" -q "SELECT * FROM ${table} FORMAT TabSeparated" | \
          restic -r "s3:${REPO_PREFIX}/$table" backup --tag "$JOB_ID" --tag data --stdin --stdin-filename data.tsv
        restic -r "s3:${REPO_PREFIX}/$table" tag --tag "$JOB_ID" --set "completed"
      )
    done
    echo "Backup finished at `date +%Y-%m-%d\ %H:%M:%S`"
    
    echo
    echo "Run cleanup:"
    echo
    
    echo "Cleanup started at `date +%Y-%m-%d\ %H:%M:%S`"
    for db in $DB_LIST; do
      (
        set -x
        # keep completed snapshots only
        restic forget -r "s3:${REPO_PREFIX}/$db" --group-by=tags --keep-tag "completed" --tag index
        restic forget -r "s3:${REPO_PREFIX}/$db" --group-by=tags --keep-tag "completed" --tag data
        restic forget -r "s3:${REPO_PREFIX}/$db" --group-by=tags $CLEANUP_STRATEGY --tag index
        restic forget -r "s3:${REPO_PREFIX}/$db" --group-by=tags $CLEANUP_STRATEGY --tag data
        restic prune -r "s3:${REPO_PREFIX}/$db"
      )
    done
    echo "Cleanup finished at `date +%Y-%m-%d\ %H:%M:%S`"
{{- end }}
