Database dump from a MySQL pod


Context

I got a request from a colleague to download a database dump from a MySQL pod running in one of our review environments in Kubernetes for further testing.

Solution

The following steps allow you to generate a MySQL dump from a Kubernetes pod:

Alternative 1

  1. Shell into the pod
kubectl exec --stdin --tty <mysql_pod_name> -- bash
  1. Generate database dump within the pod using mysqldump (will prompt for the password)
mysqldump -u <user> db_name -p > db_name_dump.sql # Path should be writable by the user
  1. Compress the file (using tar)
tar -cvzf db_name_dump.tar.gz db_name_dump.sql
  1. After leaving the shell session in the pod, copy the dump from the pod to your local machine
kubectl cp <mysql_pod_name>:/path/to/db_name_dump.tar.gz ./db_name_dump.tar.gz

Alternative 2

We don’t need to exec into the pod to generate the dump, instead we can proceed with the following steps:

  1. Execute mysqldump within the context of kubectl exec and save the output in a file in your local machine
kubectl exec <mysql_pod_name> -- mysqldump -u <user> db_name -p > db_name_dump.sql > ./db_name_dump.sql
  1. Compress the file (using tar)
tar -cvzf db_name_dump.tar.gz db_name_dump.sql

References

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.