noLimitDumpRestore.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. // source: https://stackoverflow.com/a/25749714
  3. // by Grain
  4. // your config
  5. $filename = 'yourGigaByteDump.sql';
  6. $dbHost = 'localhost';
  7. $dbUser = 'user';
  8. $dbPass = '__pass__';
  9. $dbName = 'dbname';
  10. $maxRuntime = 8; // less then your max script execution limit
  11. $deadline = time()+$maxRuntime;
  12. $progressFilename = $filename.'_filepointer'; // tmp file for progress
  13. $errorFilename = $filename.'_error'; // tmp file for erro
  14. mysql_connect($dbHost, $dbUser, $dbPass) OR die('connecting to host: '.$dbHost.' failed: '.mysql_error());
  15. mysql_select_db($dbName) OR die('select db: '.$dbName.' failed: '.mysql_error());
  16. ($fp = fopen($filename, 'r')) OR die('failed to open file:'.$filename);
  17. // check for previous error
  18. if( file_exists($errorFilename) ){
  19. die('<pre> previous error: '.file_get_contents($errorFilename));
  20. }
  21. // activate automatic reload in browser
  22. echo '<html><head> <meta http-equiv="refresh" content="'.($maxRuntime+2).'"><pre>';
  23. // go to previous file position
  24. $filePosition = 0;
  25. if( file_exists($progressFilename) ){
  26. $filePosition = file_get_contents($progressFilename);
  27. fseek($fp, $filePosition);
  28. }
  29. $queryCount = 0;
  30. $query = '';
  31. while( $deadline>time() AND ($line=fgets($fp, 1024000)) ){
  32. if(substr($line,0,2)=='--' OR trim($line)=='' ){
  33. continue;
  34. }
  35. $query .= $line;
  36. if( substr(trim($query),-1)==';' ){
  37. if( !mysql_query($query) ){
  38. $error = 'Error performing query \'<strong>' . $query . '\': ' . mysql_error();
  39. file_put_contents($errorFilename, $error."\n");
  40. exit;
  41. }
  42. $query = '';
  43. file_put_contents($progressFilename, ftell($fp)); // save the current file position for
  44. $queryCount++;
  45. }
  46. }
  47. if( feof($fp) ){
  48. echo 'dump successfully restored!';
  49. }else{
  50. echo ftell($fp).'/'.filesize($filename).' '.(round(ftell($fp)/filesize($filename), 2)*100).'%'."\n";
  51. echo $queryCount.' queries processed! please reload or wait for automatic browser refresh!';
  52. }