Skip to content

Instantly share code, notes, and snippets.

@micc83
Created June 5, 2017 13:17
Show Gist options
  • Save micc83/fe6b5609b3a280e5516e2a3e9f633675 to your computer and use it in GitHub Desktop.
Save micc83/fe6b5609b3a280e5516e2a3e9f633675 to your computer and use it in GitHub Desktop.
Simple PHP script to dump a MySql database
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$database = 'db';
$user = 'user';
$pass = 'pass';
$host = 'localhost';
$dir = dirname(__FILE__) . '/dump.sql';
echo "<h3>Backing up database to `<code>{$dir}</code>`</h3>";
exec("mysqldump --user={$user} --password={$pass} --host={$host} {$database} --result-file={$dir} 2>&1", $output);
var_dump($output);
@paulpwo
Copy link

paulpwo commented Feb 1, 2020

Just use mysqldump-php within your php code. Check out the examples or the wiki, but it is as easy as:

error_reporting(E_ALL);

require_once __DIR__ . '/vendor/autoload.php';
include_once(dirname(__FILE__) . '/vendor/mysqldump-php/src/Ifsnop/main.inc.php');

use Ifsnop\Mysqldump as IMysqldump;

try {
    $dump = new IMysqldump\Mysqldump('mysql:host=localhost;dbname=testdb', 'username', 'password');
    $dump->start('storage/work/dump.sql');
} catch (\Exception $e) {
    echo 'mysqldump-php error: ' . $e->getMessage();
}

Work fine!

@dougkusanagi
Copy link

dougkusanagi commented Mar 6, 2020

If you're in windows like me and don't want create a ENVIRONMENT VARIABLE, you can use the mysql dir before the dump command

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

define('DS', DIRECTORY_SEPARATOR);

$database = 'etsy';
$user = 'root';
$pass = '';
$host = 'localhost';
$dir = dirname(__FILE__) . DS . 'dump.sql';

$mysqlDir = 'D:'.DS.'wamp64'.DS.'bin'.DS.'mysql'.DS.'mysql8.0.18'.DS.'bin';    // Paste your mysql directory here and be happy
$mysqldump = $mysqlDir.DS.'mysqldump';

echo "<h3>Backing up database to `<code>{$dir}</code>`</h3>";
exec("{$mysqldump} --user={$user} --password={$pass} --host={$host} {$database} --result-file={$dir} 2>&1", $output);

var_dump($output);

@aphiwemagama
Copy link

The script works well locally, now how can I use it externally?

@RoneoOrg
Copy link

RoneoOrg commented Jun 1, 2021

Thanks a lot for this script.

I improved it with a bit of bash and a desktop launcher, it's written in French but this may interest someone:

https://roneo.org/ovh-sans-ssh-sauvegarde-database-sql-script-bash/

@AlbertEinsteinGlitchPoint
Copy link

AlbertEinsteinGlitchPoint commented Oct 11, 2021

Hi guys,

first of all let me thank you for the amazing simple script.. it works great, but i needed to add some features to fit my project, such as save file from input form in php, where we select de name of the backup and post it to the execut backup script, and then download file in webrowser directly. attached below is a working example of the code.

And yeahhh keep up the good work lads.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

include_once("conect-db.php");

$name_backup = $_REQUEST['name_backup'];

$command='mysqldump --opt -h' .$databaseHost .' -u' .$databaseUsername .' -p' .$databasePassword .' ' .$databaseName .' > ' .$name_backup;
exec($command,$output,$worked);

$file_name = basename($name_backup);
//save the file by using base name
$fn = file_put_contents($file_name,file_get_contents($name_backup));
header("Expires: 0");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/file");
header('Content-length: '.filesize($file_name));
header('Content-disposition: attachment; filename="'.basename($file_name).'"');
readfile($file_name);

switch($worked){
case 0:
echo 'The database ' .$databaseName .' was successfully stored in the following path '.getcwd().'./' .$name_backup .'';
break;
case 1:
echo 'An error occurred when exporting ' .$databaseName .' zu '.getcwd().'/' .$mysqlExportPath .'';
break;
case 2:
echo 'An export error has occurred, please check the following information:

MySQL Database Name:' .$databaseName .'
MySQL User Name:' .$databaseUsername .'
MySQL Password:NOTSHOWN
MySQL Host Name:' .$databaseHost .'
';
break;
}
?>

@pthavarasa
Copy link

@smitione
Copy link

smitione commented Oct 20, 2022

Hi, first at all, GREAT SCRIPT...! now i have to questions: 1)- how can i zip the dump? 2)- How can i for the download after finish? Suggestions or ideas? Best regards.

@micc83
Copy link
Author

micc83 commented Oct 21, 2022

Hi @smitione look here, you should find everything you're looking for: https://stackoverflow.com/a/4369173

@hanita0o0
Copy link

thanksssss alot i had very struggle for this command and now i find it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment