DEV Community

Cover image for Build a docker image for backup Mysql database to AWS S3
Khoi Doan (Osbkca)
Khoi Doan (Osbkca)

Posted on

Build a docker image for backup Mysql database to AWS S3

In this tutorial, we will build a docker image for cronjob on Kubernetes to auto backup Mysql database.

Let’s build a docker image with Dockerfile:

FROM mariadb:latest

WORKDIR /app
RUN apt-get update && apt-get install -y curl unzip python3 python3-venv

RUN ln -s /usr/bin/python3 /usr/bin/python
RUN curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip" && unzip awscli-bundle.zip && ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws
COPY backup_to_s3.sh ./backup_to_s3.sh

ENTRYPOINT [ "./backup_to_s3.sh" ]
Enter fullscreen mode Exit fullscreen mode

In this Dockerfile, we use mariadb:latest to call mysqldump . In addition, we also install aws-cli to be able to interact with AWS using the command line.

File backup_to_s3.sh

#!/bin/bash

# Dump and upload to S3
FILE_NAME=backup-$(date "+%Y-%m-%d-%H%M%S").sql
mysqldump -h $DATABASE_HOST -P 3306 -u $DATABASE_USER --password=$DATABASE_PASSWORD $DATABASE_NAME > $FILE_NAME
aws s3 cp $FILE_NAME s3://$AWS_S3_BUCKET/$FILE_NAME
Enter fullscreen mode Exit fullscreen mode

Don’t forget run chmod a+x backup_to_s3.sh

docker build -t backup_mysql:latest .
Enter fullscreen mode Exit fullscreen mode

I have posted this docker image to my Docker Hub account osbkca/backup_mysql

Deploy a CronJob to Kubernetes
mysql-cronjob-backup.yaml

apiVersion: batch/v1
kind: CronJob
metadata:
  labels:
    app: auto-backup-mysql
  name: auto-backup-mysql
spec:
  jobTemplate:
    spec:
      template:
        spec:
          affinity: {}
          containers:
          - env:
            - name: DATABASE_HOST
              value: <change_value>
            - name: DATABASE_USER
              value: <change_value>
            - name: DATABASE_PASSWORD
              value: <change_value>
            - name: DATABASE_NAME
              value: <change_value>
            - name: AWS_S3_BUCKET
              value: <change_value>
                        - name: AWS_ACCESS_KEY_ID
              value: <change_value>
            - name: AWS_SECRET_ACCESS_KEY
              value: <change_value>
            image: osbkca/backup_mysql:latest
            imagePullPolicy: Always
            name: backup-container

  schedule: 12 09 * * *
  successfulJobsHistoryLimit: 3
  suspend: false
Enter fullscreen mode Exit fullscreen mode

To deploy:

kubectl apply -f mysql-cronjob-backup.yaml
Enter fullscreen mode Exit fullscreen mode

Enjoy it! More Info here

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay