PHP: retore big MYSQL-database


This tutorial shows you how to restore a big MYSQL-database.

Step 1: split MYSQL file into many part
We use PHP-script below (ex: split.php)

<?
set_time_limit(600);

$dir = '/folder/contains/splitted/files';
$file = '/path/of/file.sql';

$i=1;
$handle = fopen($file,"r");
if($handle) {
	while((($buffer = fgets($handle)) !== false)) { 
		//read line and append it to the file with name $i.sql
		$newfile = fopen($dir . '/' .$i . '.sql', 'a+');
		fwrite($newfile,$buffer);		
		if(substr(trim($buffer),-3,3) === "');"){ 
			$i++;
		}		
	}	 	
}
fclose($handle);
?>

After run this PHP-script, we take a list of many MYSQL files: 1.sql, 2.sql …

Step 2: restore MYSQL
There are many ways to restore MYSQL, like BIGDUMP, MYSQLDUMPER. Here, I introduce the method used mysql command:

Create file run.php

<?
set_time_limit(600);
$dir = '/folder/contains/splitted/files';

$i = $_GET[i]?$_GET[i]:1;
$i2=$i;

//number of files you want to run once
$num = 20;

while($i<$i2+$num){
    exec("mysql --host=localhost --user=user --pass=pass database_name < $dir/$i.sql");
    ob_flush();
    $i++;
}

echo "<a href=\"?i=".($i)."\">next</a>";
?>

Leave a Reply