apiVersion: v1
kind: ConfigMap
metadata:
  name: mongo-backup-configmap
  labels:
    app: galoy
data:
  backup.sh: |
    echo "backup script starts"
    export BUCKET_NAME="{{ .Values.mongobackup.bucketName }}"
    MONGO_TOOLS_VERSION="{{ .Values.mongobackup.mongoToolsVersion }}"
    apk add --update --no-cache mongodb-tools=${MONGO_TOOLS_VERSION} --repository=http://dl-cdn.alpinelinux.org/alpine/edge/community
    set -e
    LOG_TIME=$(date +%s)
    BACKUP_NAME="$NETWORK-$LOG_TIME.gz"
    echo "Activating service account"
    gcloud auth activate-service-account --key-file=$GOOGLE_APPLICATION_CREDENTIALS
    echo "Backing up mongodb"
    mongodump --host=$MONGODB_ADDRESS --port=$MONGODB_PORT --username=$MONGODB_USER --password=$MONGODB_PASSWORD --gzip --archive=$BACKUP_NAME -d=$MONGODB_DB --readPreference=secondary
    echo "Uploading backup $BACKUP_NAME to dropbox"
    curl -X POST https://content.dropboxapi.com/2/files/upload --header "Authorization: Bearer $DROPBOX_ACCESS_TOKEN" --header "Dropbox-API-Arg: {\"path\": \"/mongo/$BACKUP_NAME\"}" --header "Content-Type: application/octet-stream" --data-binary $BACKUP_NAME
    echo "Uploading backup $BACKUP_NAME to gcs"
    gsutil cp $BACKUP_NAME gs://$BUCKET_NAME/$BACKUP_NAME 2>&1
    echo "Uploaded backup successfully"
