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.
Go backBecause the
mysqldump
command is wrapped insh
it will use the environment variables from the container so you don’t have to enter the password manually.