Created
August 31, 2017 10:34
-
-
Save adnan360/92adc8c155f463ccf5aec0dc5c83f8f9 to your computer and use it in GitHub Desktop.
No limit mysql dump import tool (pauses and resumes automatically, great for big sql files)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// source: https://stackoverflow.com/a/25749714 | |
// by Grain | |
// your config | |
$filename = 'yourGigaByteDump.sql'; | |
$dbHost = 'localhost'; | |
$dbUser = 'user'; | |
$dbPass = '__pass__'; | |
$dbName = 'dbname'; | |
$maxRuntime = 8; // less then your max script execution limit | |
$deadline = time()+$maxRuntime; | |
$progressFilename = $filename.'_filepointer'; // tmp file for progress | |
$errorFilename = $filename.'_error'; // tmp file for erro | |
mysql_connect($dbHost, $dbUser, $dbPass) OR die('connecting to host: '.$dbHost.' failed: '.mysql_error()); | |
mysql_select_db($dbName) OR die('select db: '.$dbName.' failed: '.mysql_error()); | |
($fp = fopen($filename, 'r')) OR die('failed to open file:'.$filename); | |
// check for previous error | |
if( file_exists($errorFilename) ){ | |
die('<pre> previous error: '.file_get_contents($errorFilename)); | |
} | |
// activate automatic reload in browser | |
echo '<html><head> <meta http-equiv="refresh" content="'.($maxRuntime+2).'"><pre>'; | |
// go to previous file position | |
$filePosition = 0; | |
if( file_exists($progressFilename) ){ | |
$filePosition = file_get_contents($progressFilename); | |
fseek($fp, $filePosition); | |
} | |
$queryCount = 0; | |
$query = ''; | |
while( $deadline>time() AND ($line=fgets($fp, 1024000)) ){ | |
if(substr($line,0,2)=='--' OR trim($line)=='' ){ | |
continue; | |
} | |
$query .= $line; | |
if( substr(trim($query),-1)==';' ){ | |
if( !mysql_query($query) ){ | |
$error = 'Error performing query \'<strong>' . $query . '\': ' . mysql_error(); | |
file_put_contents($errorFilename, $error."\n"); | |
exit; | |
} | |
$query = ''; | |
file_put_contents($progressFilename, ftell($fp)); // save the current file position for | |
$queryCount++; | |
} | |
} | |
if( feof($fp) ){ | |
echo 'dump successfully restored!'; | |
}else{ | |
echo ftell($fp).'/'.filesize($filename).' '.(round(ftell($fp)/filesize($filename), 2)*100).'%'."\n"; | |
echo $queryCount.' queries processed! please reload or wait for automatic browser refresh!'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment