fradelg / docker-mysql-cron-backup

Docker image to backup all your databases periodically
Apache License 2.0
305 stars 111 forks source link

Add option to RUN_ONCE in order to use this image for Kubernetes Cronjobs #65

Closed Skaronator closed 2 years ago

Skaronator commented 2 years ago

Hi,

Would it be possible to add a RUN_ONCE option to the image which means that it'll do a backup on startup (like INIT_BACKUP) but after that will exit/stop the container?

I'd like to continue using the image but with a Kubernetes Cronjob. So, the Container orchestrator takes care of scheduling the container.

fradelg commented 2 years ago

Hi,

Indeed, Kubernetes is already supported. You only have to change the command.

A CronJob like this may work (WARNING: I haven't tested it):

apiVersion: batch/v1
kind: CronJob
metadata:
  name: backup
spec:
  schedule: "0 3 * * sun"
  jobTemplate:
    spec:
      template:
        spec:
          volumes:
            - name: db_data
              host:
                secretName: mysecret
          containers:
          - name: backup
            image: fradelg/mysql-cron-backup:<my-tag>
            imagePullPolicy: IfNotPresent
            command:
            - /backup.sh
           volumes:
             - name: db_data
              hostPath:
                path: /data/backup
          env:
            - name: MYSQL_HOST
              value: my_mariadb
            - name: MYSQL_USER
              value: root
            - name: MAX_BACKUPS
              value: 15
            - name: CRON_TIME
              value: 0 3 * * *
            - name: GZIP_LEVEL
              value: 9
            - name: $MYSQL_PASS
              valueFrom:
                secretKeyRef:
                  name: mysql
                  key: password
          restartPolicy: OnFailure
          volumeMounts:
            - name: db_data
              mountPath: "/backup"
              readOnly: true
Skaronator commented 2 years ago

Hi @fradelg,

Totally forgot that I can just overwrite the command itself. And the good thing is that the bachup.sh script contains the cleanup logic as well.

Just gave this a try and it seems to work just fine :)

Thanks