Duplicating a database using PHP and mysqldump

Note i’m using VPS so there are no restrictions – you will only be able to do this on some shared hosts.

<?php

error_reporting(E_ALL);
include ‘../config/database.php’;
include ‘../vendors/MySQLConnection.php’; // personal wrapper, should be obvious what it’s doing
$config = new DATABASE_CONFIG();
$db_test = new MySQLConnection(
$config->test[‘host’],
$config->test[‘login’],
$config->test[‘password’],
$config->test[‘database’]
);

ob_start();
system(‘mysqldump -h ‘.$config->default[‘host’].’ -u ‘.$config->default[‘login’].’ -p’.$config->default[‘password’].’ --compact --add-drop-table ‘.$config->default[‘database’]);
$s = ob_get_clean();
$a = split(‘;’,$s);
function t(&$s){$s=trim($s);}
array_walk($a,’t’);
foreach ($a as $sql) {
$db_test->query($sql);
}

?>

Leave a Reply