# License terms and conditions for Gluu Cloud Native Edition:
# https://www.apache.org/licenses/LICENSE-2.0
kind: CronJob
apiVersion: batch/v1beta1
metadata:
  name: opendj-backup-create
spec:
  schedule: "*/10 * * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        spec:
          containers:
            - name: backup
              image: gluufederation/wrends:4.2.0_01
              envFrom:
                - configMapRef:
                    name: opendj-backup-cm
              ports:
                - containerPort: 1636
                  name: ldaps
                - containerPort: 1389
                  name: ldap
                - containerPort: 8989
                  name: replication
                - containerPort: 4444
                  name: admin
              env:
                - name: LDAP_HOST
                  valueFrom:
                    configMapKeyRef:
                      name: gluu
                      key: ldap_init_host
                - name: LDAP_PORT
                  valueFrom:
                    configMapKeyRef:
                      name: gluu
                      key: ldap_init_port
                - name: LDAP_BIND_DN
                  valueFrom:
                    configMapKeyRef:
                      name: gluu
                      key: ldap_site_binddn
                - name: LDAP_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: ldap-auth
                      key: password
              #  while true; do sleep 60; ldaplog=$(cat /opt/opendj/logs/server.out); startedstr="The Directory Server has started successfully";  if [ -z "${ldaplog##*$startedstr*}" ]; then break; fi; echo "Waiting for opendj server to start"; done
              command:
              - /bin/sh
              - -c
              - |
                /app/scripts/entrypoint.sh &
                sleep 300
                RANDOM_NUM=$(cat /dev/urandom | tr -cd '0-5' | head -c 1)
                LDAP_BACKUP_FILE=backup-$RANDOM_NUM.ldif
                /opt/opendj/bin/export-ldif --hostname "$LDAP_HOST" --port 4444 --bindDN "$LDAP_BIND_DN" --bindPassword "$LDAP_PASSWORD" --backendID userRoot --ldifFile /opt/opendj/ldif/$LDAP_BACKUP_FILE --trustAll
          restartPolicy: Never