Skip to content

Instantly share code, notes, and snippets.

@shoaibali
Created June 2, 2018 23:41
Show Gist options
  • Save shoaibali/03b5ae3ab84c2e97f8e95586d8a49daa to your computer and use it in GitHub Desktop.
Save shoaibali/03b5ae3ab84c2e97f8e95586d8a49daa to your computer and use it in GitHub Desktop.
php script to migrate database from one server to another, does not work on large databases/tables
<?php
/* Credit: Copied from https://www.howtoforge.com/community/threads/php-script-to-copy-mysql-tables-from-one-server-to-another-by-database.58303/
* However fixed up for it to actually work! Also datapted it to work with Mysqli instead of Mysql deprecated library
*/
ini_set('memory_limit', '10000M'); // breaks on large databases
// old server mysql id
$DBIUser = 'root';
$DBIPass = 'myolddatabasepassword';
// new server mysql id
$NewUser = 'root';
$NewPass = '';
// server names
$oldServer = 'localhost';
$newServer = 'localhost';
$dbnew = "mynewdatabase_production";
if ($argv[0] > " ")
{
$dbname = $argv[1];
echo "Starting copy of the $argv[1] database.\n";
$dbpre = mysqli_connect($oldServer, $DBIUser, $DBIPass, $dbname, "3307"); // 3307 is what old database runs on
// mysqli_select_db($dbname, $dbpre);
$sql = "SHOW TABLES FROM $dbname";
echo $sql."\n";
$result = mysqli_query($dbpre, $sql);
if (!$result)
{
echo "DB Error, could not list tables\n";
echo 'MySQL Error: ' . mysqli_error($dbpre);
exit;
}
$dbtbl = mysqli_connect($oldServer, $DBIUser, $DBIPass, $dbname, "3307");
// mysqli_select_db($dbname, $dbpre);
$dbnew = mysqli_connect($newServer, $NewUser, $NewPass, $dbnew, "3306"); // 3306 is the new local database
// mysqli_select_db("mysql", $dbnew);
$res2 = mysqli_query($dbnew, "CREATE DATABASE IF NOT EXISTS ".$dbname);
if (!$res2)
{
echo "DB Error, could not create database\n";
echo 'MySQL Error: ' . mysqli_error($dbnew);
exit;
}
// mysqli_select_db($dbname, $dbnew);
if($result === FALSE)
{
die(mysqli_error($dbpre));
}
$f = fopen($dbname.'.log', 'w');
fwrite($f, "Copy all tables in database $dbname on server $oldServer to new database on server $newServer.\n\n");
while ($row = mysqli_fetch_row($result))
{
echo "Table: {$row[0]}\n";
fwrite($f, "Table ".$row[0]."\n");
$tableinfo = mysqli_fetch_array(mysqli_query($dbtbl, "SHOW CREATE TABLE $row[0] "));
$createsyntax = "CREATE TABLE IF NOT EXISTS ";
$createsyntax .= substr($tableinfo[1], 13);
//echo $row[0]."\n";
mysqli_query($dbnew, " $createsyntax ");
$res = mysqli_query($dbpre, "SELECT * FROM $row[0] "); // select all rows
$oldcnt = mysqli_num_rows($res);
echo "Count: ".$oldcnt." - ";
$errors = 0;
while ($roz = mysqli_fetch_array($res, MYSQLI_ASSOC) )
{
$query = "INSERT INTO $dbname.$row[0] (".implode(", ",array_keys($roz)).") VALUES (";
$cnt = 0;
foreach (array_values($roz) as $value)
{
if ($cnt == 0)
{
$cnt++;
} else
{
$query .= ",";
}
$query .= "'";
$query .= mysqli_real_escape_string($dbpre, $value);
$query .= "'";
}
$query .= ")";
$look = mysqli_query($dbnew, $query);
if ($look === false)
{
// write insert to log on error
$errors = $errors + 1;
fwrite($f, mysqli_error($dbnew)." - ".$query."\n");
}
}
$sql = "select count(*) as cnt from $dbname.$row[0] ";
$res = mysqli_query($dbnew, $sql );
$roz = mysqli_fetch_array($res);
echo $roz['cnt']." - Errors: ".$errors."\n";
fwrite($f, "Old Record Count: ".$oldcnt." - New Record Count: ".$roz['cnt']." - Errors: ".$errors."\n");
fwrite($f,"End table copy for table $row[0].\n\n");
}
fclose($f);
} else
{
var_dump($argv);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment