Mysqldump using PHP

I’ve run into this more than once: you need to pull a dump of a large database from hosting, but there’s no remote access to the mysql engine. PhpMyAdmin might not handle the job if the database is a few hundred megabytes. So what do you do?

You can get a database dump using php, without shell_exec. That is, the mysqldump binary itself never runs.

All the source is available in the GitHub repository

The main file is src/Ifsnop/Mysqldump/mysqldump.php

To simplify the process I put together a small archive.

Download and unpack it

wget http://www.tech-notes.net/wp-content/uploads/2015/04/php-mysqldump.zip  
unzip php-mysqldump.zip

Then upload the mysqldump folder via ftp to the site folder on the server and open:
http://www.site_name/mysqldump

The index file shows a form where you fill in the connection parameters.

  • dbname
  • dbuser
  • password
  • dbhost

You can also hit it with curl, passing all the connection parameters:

curl -X POST http://www.site_name/mysqldump/index.php?dbname=database\&dbuser=mysql_user\&password=mysql_password\&dbhost=host_address

After it runs, the database dump will be sitting in the files folder named database.sql

You can download this file and restore it on the new server:

wget http://www.site_name/mysqldump/files/database.sql