Created
April 29, 2023 10:45
-
-
Save hawkkiller/b9b0f3ef8a33f89987c5d279e729cbd8 to your computer and use it in GitHub Desktop.
mysql backup to s3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {{ if .Values.mysql.backup.enabled }} | |
| apiVersion: batch/v1 | |
| kind: CronJob | |
| metadata: | |
| name: {{ .Release.Name }}-mysql-backup | |
| spec: | |
| schedule: {{ .Values.mysql.backup.schedule | quote }} | |
| jobTemplate: | |
| spec: | |
| ttlSecondsAfterFinished: {{ .Values.mysql.backup.ttlSecondsAfterFinished }} | |
| template: | |
| spec: | |
| restartPolicy: OnFailure | |
| containers: | |
| - name: backup | |
| image: amazon/aws-cli:amd64 | |
| env: | |
| - name: S3_REGION | |
| valueFrom: | |
| secretKeyRef: | |
| name: {{ .Values.mysql.backup.awsSecretRef }} | |
| key: S3_REGION | |
| - name: S3_BUCKET | |
| valueFrom: | |
| secretKeyRef: | |
| name: {{ .Values.mysql.backup.awsSecretRef }} | |
| key: S3_BUCKET | |
| - name: AWS_ACCESS_KEY_ID | |
| valueFrom: | |
| secretKeyRef: | |
| name: {{ .Values.mysql.backup.awsSecretRef }} | |
| key: S3_ACCESS_KEY_ID | |
| - name: AWS_SECRET_ACCESS_KEY | |
| valueFrom: | |
| secretKeyRef: | |
| name: {{ .Values.mysql.backup.awsSecretRef }} | |
| key: S3_SECRET_ACCESS_KEY | |
| - name: MYSQL_HOST | |
| value: {{ include "mysql.host" . | quote }} | |
| - name: MYSQL_USER | |
| value: "root" | |
| {{/* valueFrom:*/}} | |
| {{/* secretKeyRef:*/}} | |
| {{/* name: {{ .Values.mysql.connection.privateSecretRef }}*/}} | |
| {{/* key: mysql-username*/}} | |
| - name: MYSQL_PASSWORD | |
| valueFrom: | |
| secretKeyRef: | |
| name: {{ .Values.mysql.connection.privateSecretRef }} | |
| key: mysql-root-password | |
| {{/* key: mysql-password*/}} | |
| - name: RETENTION_DAYS | |
| value: "{{ .Values.mysql.backup.retentionDays }}" | |
| - name: MYSQL_DATABASE | |
| valueFrom: | |
| secretKeyRef: | |
| name: {{ .Values.mysql.connection.privateSecretRef }} | |
| key: mysql-database | |
| command: | |
| - /bin/sh | |
| - -c | |
| - | | |
| #!/bin/sh | |
| set -e | |
| cat /etc/os-release | |
| echo ${MYSQL_HOST} | |
| echo ${MYSQL_USER} | |
| echo ${MYSQL_PASSWORD} | |
| echo ${MYSQL_DATABASE} | |
| # install tar, gzip | |
| yum -y install tar gzip | |
| # Install the MySQL Community Repo for mysqldump | |
| yum -y install https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm | |
| # Install the mysqldump package | |
| yum -y install mysql-community-client | |
| # Check if mysql is successfully installed | |
| mysql --version | |
| DATE_FORMAT=$(date +"%Y-%m-%d-%H-%M-%S") | |
| # local backup dir | |
| LOCAL_DIR="/backup/dbbackup" | |
| # remote backup dir | |
| REMOTE_DIR="s3://${S3_BUCKET}/{{- .Values.mysql.backup.bucketPath -}}" | |
| # create dir | |
| mkdir -p ${LOCAL_DIR}/${DATE_FORMAT} | |
| # create dump | |
| mysqldump \ | |
| -h ${MYSQL_HOST} \ | |
| -u ${MYSQL_USER} \ | |
| -p${MYSQL_PASSWORD} \ | |
| --single-transaction ${MYSQL_DATABASE} | gzip -9 > ${LOCAL_DIR}/${MYSQL_DATABASE}-${DATE_FORMAT}.sql.gz | |
| # copy to aws s3 | |
| aws s3 cp ${LOCAL_DIR}/${MYSQL_DATABASE}-${DATE_FORMAT}.sql.gz ${REMOTE_DIR}/${DATE_FORMAT}/ | |
| # delete old backups from aws | |
| OLDEST_DATE=$(date -d "-${RETENTION_DAYS} days" +%Y-%m-%d) | |
| aws s3api list-objects --bucket "${S3_BUCKET}" --prefix "{{- .Values.mysql.backup.bucketPath -}}" --query "Contents[?LastModified<='${OLDEST_DATE}'].{Key: Key}" --output text | while read -r object_key; do | |
| aws s3 rm "s3://${S3_BUCKET}/${object_key}" | |
| done | |
| {{ end }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment