TRAFEX TRAFEX Consultancy Consultancy
Create a DB backup from a pod running MySQL on Kubernetes

Create a DB backup from a pod running MySQL on Kubernetes

August 21, 2021

When using the MySQL docker image you can easily create a DB dump with this one-line CLI command.

kubectl exec -ti \
  $(kubectl get pod -l app=mysql -o name) -- \
  sh -c 'mysqldump -u$MYSQL_USER -p$MYSQL_PASSWORD $MYSQL_DATABASE 2>/dev/null' \
  > database-`date -I`.sql

This assumes the pod running MySQL has a label app=mysql. A file named database-YYYY-MM-DD.sql will be created with the DB contents.

Because the mysqldump command is wrapped in sh it will use the environment variables from the container so you don’t have to enter the password manually.

Go back

Related content


The key components of Kubernetes autoscaling

Autoscaling is an important feature of Kubernetes. With this feature, you always have enough resources for the workload, and when a node becomes unhealthy it gets replaced without affecting the workload. But you won’t get it automatically by just deploying your Pods on Kubernetes.

Read More


Doing maintenance & debugging on Kubernetes with a support pod

Using a support pod is a simple and secure way to take a look around from within the Kubernetes cluster without interfering with the workload or expose a security risk.

Read More