kind: ConfigMap
apiVersion: v1
metadata:
  name: {{ template "backup-exporter.name" . }}
  namespace: backup-exporter
  labels:
    app: {{ template "backup-exporter.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
data:
  backup.sh: |
    set -e
    gcloud config set account {{ .Values.serviceAccountName }}
    SNAPSHOTS=$(gcloud compute snapshots list --sort-by=~creationTimestamp,STATUS --limit={{ .Values.replicaCount }} --format="value[separator=';'](name,status)" | cut -f1 -d\;)
    DESTINATION_BUCKET={{ .Values.destinationBucket }}
    TIMESTAMP=$(date +%s)
    for snapshot in ${SNAPSHOTS}; do
      gcloud -q compute images create image-disk-${snapshot} --source-snapshot ${snapshot}
      gcloud -q --verbosity=debug compute images export --log-location={{ .Values.logBucket }} --destination-uri ${DESTINATION_BUCKET}/${TIMESTAMP}-${snapshot}.raw.gz --image image-disk-${snapshot}
      gcloud -q compute images delete image-disk-${snapshot}
    done

kind: ConfigMap
apiVersion: v1
metadata:
  name: {{ template "backup-exporter.name" . }}
  namespace: backup-exporter
  labels:
    app: {{ template "backup-exporter.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
data:
  backup.sh: |
    set -e
    gcloud config set account {{ .Values.serviceAccountName }}
    SNAPSHOTS=$(for i in `seq 0 $(({{ .Values.replicaCount }}-1))`; do gcloud compute snapshots list --sort-by=~creationTimestamp,STATUS --limit=1 --format="value[separator=';'](name,status)" --filter="name~'pv-database-storage-couchdb-couchdb-$i-*'" | cut -f1 -d\; ; done)
    DESTINATION_BUCKET={{ .Values.destinationBucket }}
    TIMESTAMP=$(date +%s)
    for snapshot in ${SNAPSHOTS}; do
      gcloud -q compute images create image-disk-${snapshot} --source-snapshot ${snapshot}
      gcloud -q --verbosity=debug compute images export --log-location={{ .Values.logBucket }} --destination-uri ${DESTINATION_BUCKET}/${TIMESTAMP}-${snapshot}.raw.gz --image image-disk-${snapshot}
      gcloud -q compute images delete image-disk-${snapshot}
    done

Select the images based on the ID of the snapshots
Select the images based on the ID of the snapshots
