Yesterday I realized that when my primary test cluster became the production cluster that runs this site, I had not thought anything about how to back up the WordPress and MariaDB pods that this website runs on. If you search for options on backing up WordPress running inside of Kubernetes, nearly everything points to paid services or commands to be run directly on the worker nodes that the pods live on. Neither of these appealed to me, so I rolled my own solution, which I will outline below.
This backs up the entire WordPress HTML folder and dumps the entire database into an sql.dump file, and then compresses it all into one archive, all from the Kubernetes control plane.
First, we need to find the name of the pods that NGNIX and MariaDB are running on. I know that I created my manifests with specific names for this website. In this case, I ended up flipping dev pod to production, so they are still named dev-baremtalbridge as shown below. You will need to replace dev-baremetalbridge with the name of the WordPress pods you are working with.
kubectl get pods -n default | grep dev-baremetalbridge
Which will return this information.
dev-baremetalbridge-wordpress-7495d65c-d4x9g 1/1 Running 2 (3d20h ago) 5d21h dev-baremetalbridge-wordpress-mariadb-0 1/1 Running 0 5d21h
Now that we have the names of the pods that we need to work with, the next step is to get the endpoint data for the database. We can easily do this with the command below.
kubectl get endpoints -n default | grep dev-baremetalbridge
Which will return this information.
dev-baremetalbridge-wordpress 10.200.226.91:8443,10.200.226.91:8080 5d21h dev-baremetalbridge-wordpress-mariadb 10.200.201.223:3306 5d21h
In most cases, the MariaDB connection will be the endpoint with the port 3306.
Now we have all of the pod information that we need to do this manual backup. We will need the username, password, and database name for this WordPress instance. If you do not know these, we can retrieve them by opening a shell to the dev-baremetalbridge-wordpress-7495d65c-d4x9g pod and looking at the wp-config.php as shown in the commands below.
kubectl exec --stdin --tty dev-baremetalbridge-wordpress-7495d65c-d4x9g -- /bin/bash
Once the shell is open, then run the command below to pull all of the database configuration information from the wp-config.php file.
grep -i DB_ bitnami/wordpress/wp-config.php
This will generate an output that looks similar to this.
define( 'DB_NAME', 'bitnami_wordpress' ); define( 'DB_USER', 'notauser' ); define( 'DB_PASSWORD', 'notapassword' ); define( 'DB_HOST', 'dev-baremetalbridge-wordpress-mariadb:3306' ); define( 'DB_CHARSET', 'utf8' ); define( 'DB_COLLATE', '' );
From here, we can get the database name, user, and password required to do the database dump.
Now that we have all of the information that we need to do the manual backup. First, we will backup the entire WordPress HTML folder from the NGINX pod, archive it and compress it. We simply do this with the kubectl cp command. First, we need a place to store the backup. I will create this in /tmp, you can create this wherever you want. First, exit the shell to the pod if you are still connected to it and then run the commands below. Remember to change everything here to match the pods and endpoints you are working with.
cd /tmp mkdir baremetalbridge-backup cd baremetalbridge-backup/ mkdir html kubectl cp default/dev-baremetalbridge-wordpress-7495d65c-d4x9g:/bitnami/wordpress ./html/ mysqldump -h 10.200.201.223 -u bn_wordpress -p bitnami_wordpress > baremetalbridge.dump tar -czvf baremetalbridge.com.backup.tar.gz * rm baremetalbridge.dump rm -R html/
The only problem that may be run into running these commands is the availability of mysqldump. This is not installed by default but is a nice tool to have when working with MariaDB or any other MySQL-based database. If you do get a command not found error, follow the instructions for installing the package via whatever package manager that you use. In the case of Ubuntu 22.04, the command to install mysqldump is below.
sudo apt install mariadb-client-10.6
Once you have ran all of these commands, you will have one compressed tar archive file that includes the entire WordPress HTML folder and the MariaDB database dump file. Of course change everything in these instructions for your pod names, as I doubt you have a pod named dev-baremetalbridge. Otherwise, the whole flow is exactly the same.
Sharing the knowledge always.