Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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);
@Clipso

This comment has been minimized.

Copy link

@Clipso Clipso commented Sep 27, 2018

Hi,

Excellent script. Thanks a lot ..
By the way, if you i want to just dump the tables structures of the DB. How to do that? thanks in advance.

@ChrisToxz

This comment has been minimized.

Copy link

@ChrisToxz ChrisToxz commented Oct 20, 2018

@Zierk59

This comment has been minimized.

Copy link

@Zierk59 Zierk59 commented Dec 18, 2018

Error occur while running above script please help me to solve this.

array (size=2)
0 => string ''mysqldump' is not recognized as an internal or external command,' (length=65)
1 => string 'operable program or batch file.' (length=31)

You have to add MySQL_Installation_Path/bin to PATH. For example if you are using MariaDB and your installation path is:

  • C:/MariaDB

Well you have to add C:/MariaDB/bin to system path.

@jdepagne

This comment has been minimized.

Copy link

@jdepagne jdepagne commented Feb 11, 2019

thanks a lot

@ifsnop

This comment has been minimized.

Copy link

@ifsnop ifsnop commented Feb 21, 2019

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();
}
@JimmyNongo

This comment has been minimized.

Copy link

@JimmyNongo JimmyNongo commented May 21, 2019

OK MERCI

@paulpwo

This comment has been minimized.

Copy link

@paulpwo 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

This comment has been minimized.

Copy link

@dougkusanagi 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 befor 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

This comment has been minimized.

Copy link

@aphiwemagama aphiwemagama commented Feb 25, 2021

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

@RoneoOrg

This comment has been minimized.

Copy link

@RoneoOrg 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/

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