Extracting MySQL databases from a disk backup, the easy way

A lot of guides online tell you that, if you want to restore a MySQL instance using only a backup of the /var/lib/mysql directory, you should recover/rebuild the entire operating system that MySQL ran on.

It’s true that you should mimic the original setup, but only with regards to the MySQL version and sometimes whether MariaDB or MySQL were used. Once you’ve got that, you can very easily spin the database up again using Docker. Assuming you know the database user+password, you can just run mysqldump as you normally would.

Below are the steps that worked for me.

1. Determine MySQL version

Find the location of the mysql or mariadb binary:

find /thebackup -type f -name mysql -or -name mariadb

I had a rough idea of what the version would be so I ran strings and searched for 8.0:

$ strings /thebackup/usr/bin/mysql | grep '8\.0'

An alternative which may work generically:

strings /thebackup/usr/bin/mysql | grep '^/build'

2. Start the database in Docker

Now that we have the exact version number (8.0.25), we can spin up a Docker container for it:

docker run --rm --name mysql-recovery -v /thebackup/var/lib/mysql:/var/lib/mysql mysql:8.0.25

Note that we’re mounting the mysql lib directory into the container using -v. If you want to play it safe, make an extra backup of /var/lib/mysql first.

If you later notice you’re running into compatibility issues, try replacing mysql:<version> with mariadb:<version>.

3. Extract data

Provided you have the right credentials, you can now extract or browse the data using command-line tools:

# Add -u ... -p... parameters as needed for the credentials
docker exec mysql-recovery mysqldump mydatabase > mydatabase-backup.sql
docker exec mysql-recovery mysql

Tags: , , , | August 30th, 2022 | Posted in Uncategorized

Comments are closed.