i-Hmx

BigDump > Restore large DB backup files

Feb 20th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.70 KB | None | 0 0
  1. <?
  2.  
  3. // BigDump ver. 0.19b from 2004-03-18
  4. // Staggered import of an large MySQL Dump (like phpMyAdmin 2.x Dump)
  5. // Even through the webservers with hard runtime limit and those in safe mode
  6. // Works fine with Internet Explorer 6.0 SP1, Mozilla 1.x and even Netscape 4.8
  7.  
  8. // Author:       Alexey Ozerov (alexey at ozerov dot de)
  9. // Copyright:    GPL (C) 2003-2004
  10. // More Infos:   http://www.ozerov.de/bigdump
  11.  
  12. // This program is free software; you can redistribute it and/or modify it under the
  13. // terms of the GNU General Public License as published by the Free Software Foundation;
  14. // either version 2 of the License, or (at your option) any later version.
  15.  
  16. // THIS SCRIPT IS PROVIDED AS IS, WITHOUT ANY WARRANTY OR GUARANTEE OF ANY KIND
  17.  
  18. // USAGE:
  19.  
  20. // 1. Adjust the database configuration in this file
  21. // 2. Drop the old tables on the target database if your dump doesn't contain "DROP TABLE"
  22. // 3. Create the working directory (e.g. dump) on your web-server
  23. // 4. If you want to upload the dump files directly from the web-browser give the scripts
  24. //    writing permissions on the working directory (chmod 777 on a Linux based system)
  25. // 5. Upload bigdump.php and your dump files (.sql, .gz) via FTP to the working directory
  26. // 6. Run the bigdump.php from your browser via URL like http://www.yourdomain.com/dump/bigdump.php
  27. // 7. BigDump will start the next import session automatically if you enable the JavaScript
  28. // 8. Wait for the script to finish, do not close the browser window
  29. // 9. IMPORTANT: Remove bigdump.php and your dump files from the web-server
  30.  
  31. // If Timeout errors still occure you may need to adjust the $linepersession setting in this file
  32.  
  33. // VERSION HISTORY
  34.  
  35. // Version 0.19b 2004-05-31
  36. // *** # comment in the text field bugfix
  37.  
  38. // Version 0.18b 2004-03-18
  39. // *** \\' in the text field bugfix
  40.  
  41. // Version 0.17b 2003-12-14
  42. // *** MySQL deprecated '-- ' comment bugfix
  43. // *** Drop user-defined proprietary comment lines
  44. // *** Improved anti-cache headers
  45.  
  46. // Version 0.16b 2003-11-20
  47. // *** Register globals bugfix
  48.  
  49. // Version 0.15b 2003-11-02
  50. // *** Dump file browser upload feature added (for dump files up to upload_max_filesize)
  51. // *** Dump file mini-browser for the working directory added
  52.  
  53. // Version 0.14b 2003-10-12
  54. // *** GZip compressed files support added (available with PHP 4.3.0+)
  55.  
  56. // Version 0.13b 2003-10-09
  57. // *** Query definition bugfix: don't stop query inside an text field ending by ';'
  58. // *** Query definition bugfix: don't trim queries since spaces can be useful :-)
  59. // *** $delaypersession setting added
  60.  
  61. // Version 0.12 2003-07-11
  62. // *** File seek by fseek() instead of line skipping should avoid time loss when skipping lines
  63. //     Thanks to Anton Hummel for this idea
  64.  
  65. // Version 0.11 2003-03-11
  66. // *** Table definition bugfix: don't stop session inside an SQL query
  67. // *** Queries statistics added
  68.  
  69. // Database configuration
  70.  
  71. $db_server   = "localhost";
  72. $db_name     = "customso_joomla";
  73. $db_username = "customso_custom";
  74. $db_password = "sarah1987";
  75.  
  76.  
  77. // Other Settings
  78.  
  79. $filename        = "";     // Specify the dump filename to suppress the file selection dialog
  80. $linespersession = 3000;   // Lines to be executed per one import session
  81. $delaypersession = 0;      // You can specify a sleep time in milliseconds after each session
  82.                            // Works only if JavaScript is activated. Use to reduce server overrun
  83.  
  84. // Allowed comment delimiters: lines starting with these strings will be dropped by BigDump
  85.  
  86. $comment[0]="#";           // Standard comment lines are dropped by default
  87. $comment[1]="-- ";
  88. // $comment[2]="---";      // Uncomment this line if using proprietary dump created by mysqldump
  89. // $comment[3]="";         // Or add your own string to leave out other proprietary things
  90.  
  91.  
  92. // *******************************************************************************************
  93. // If not familiar with PHP please don't change anything below this line
  94. // *******************************************************************************************
  95.  
  96. define ("VERSION","0.19b");
  97. define ("MAX_LINE_LENGTH",65536);
  98.  
  99. ini_set("auto_detect_line_endings", true);
  100.  
  101. header("Expires: Mon, 1 Dec 2003 01:00:00 GMT");
  102. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  103. header("Cache-Control: no-store, no-cache, must-revalidate");
  104. header("Cache-Control: post-check=0, pre-check=0", false);
  105. header("Pragma: no-cache");
  106.  
  107. ?>
  108. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  109. <html>
  110. <head>
  111. <title>BigDump ver. <? echo (VERSION); ?></title>
  112. <META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; CHARSET=iso-8859-1">
  113. <META HTTP-EQUIV="CONTENT-LANGUAGE" CONTENT="EN">
  114.  
  115. <META http-equiv="Cache-Control" content="no-cache">
  116. <META http-equiv="Pragma" content="no-cache">
  117. <META http-equiv="Expires" content="-1">
  118.  
  119. <style type="text/css">
  120. <!--
  121.  
  122. body
  123. { background-color:#FFFFF0;
  124. }
  125.  
  126. h1
  127. { font-size:25px;
  128.   line-height:28px;
  129.   font-family:Arial,Helvetica,sans-serif;
  130.   margin-top:5px;
  131.   margin-bottom:5px;
  132. }
  133.  
  134. p,td,th
  135. { font-size:14px;
  136.   line-height:18px;
  137.   font-family:Arial,Helvetica,sans-serif;
  138.   margin-top:5px;
  139.   margin-bottom:5px;
  140.   text-align:justify;
  141.   vertical-align:top;
  142. }
  143.  
  144. p.error
  145. { color:#FF0000;
  146.   font-weight:bold;
  147. }
  148.  
  149. p.success
  150. { color:#00DD00;
  151.   font-weight:bold;
  152. }
  153.  
  154. td
  155. { background-color:#F8F8F8;
  156.   text-align:left;
  157. }
  158.  
  159. td.transparent
  160. { background-color:#FFFFF0;
  161. }
  162.  
  163. th
  164. { font-weight:bold;
  165.   color:#FFFFFF;
  166.   background-color:#008080;
  167.   text-align:left;
  168. }
  169.  
  170. td.right
  171. { text-align:right;
  172. }
  173.  
  174. form
  175. { margin-top:5px;
  176.   margin-bottom:5px;
  177. }
  178.  
  179. -->
  180. </style>
  181.  
  182. </head>
  183.  
  184. <body>
  185.  
  186. <table width="780" cellspacing="0" cellpadding="0">
  187. <tr><td class="transparent">
  188.  
  189. <h1>BigDump: Staggered MySQL Dump Importer ver. <? echo (VERSION); ?></h1>
  190.  
  191. <?
  192.  
  193. $error = false;
  194. $file  = false;
  195.  
  196. // Check PHP version
  197.  
  198. if (!$error && !function_exists("version_compare"))
  199. { echo ("<p class=\"error\">PHP version 4.1.0 is required for BigDump to proceed. You have PHP ".phpversion()." installed. Sorry!</p>\n");
  200.   $error=true;
  201. }
  202.  
  203. // Calculate PHP max upload size (handle settings like 10M or 100K)
  204.  
  205. if (!$error)
  206. { $upload_max_filesize=ini_get("upload_max_filesize");
  207.   if (eregi("([0-9]+)K",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024;
  208.   if (eregi("([0-9]+)M",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024;
  209.   if (eregi("([0-9]+)G",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024*1024;
  210. }
  211.  
  212. // Handle file upload
  213.  
  214. $upload_dir=dirname($_SERVER["SCRIPT_FILENAME"]);
  215.  
  216. if (!$error && isset($_REQUEST["uploadbutton"]))
  217. { if (is_uploaded_file($_FILES["dumpfile"]["tmp_name"]) && ($_FILES["dumpfile"]["error"])==0)
  218.   {
  219.     $uploaded_filename=str_replace(" ","_",$_FILES["dumpfile"]["name"]);
  220.     $uploaded_filepath=str_replace("\\","/",$upload_dir."/".$uploaded_filename);
  221.        
  222.     if (file_exists($uploaded_filename))
  223.     { echo ("<p class=\"error\">File $uploaded_filename already exist! Delete and upload again!</p>\n");
  224.     }
  225.     else if (!@move_uploaded_file($_FILES["dumpfile"]["tmp_name"],$uploaded_filepath))
  226.     { echo ("<p class=\"error\">Error moving uploaded file ".$_FILES["dumpfile"]["tmp_name"]." to the $uploaded_filepath</p>\n");
  227.       echo ("<p>Check the directory permissions for $upload_dir (must be 777)!</p>\n");
  228.     }
  229.     else
  230.     { echo ("<p class=\"success\">Uploaded file saved as $uploaded_filename</p>\n");
  231.     }
  232.   }
  233.   else
  234.   { echo ("<p class=\"error\">Error uploading file ".$_FILES["dumpfile"]["name"]."</p>\n");
  235.   }
  236. }
  237.  
  238.  
  239. // Handle file deletion (delete only in the current directory for security reasons)
  240.  
  241. if (!$error && isset($_REQUEST["delete"]) && $_REQUEST["delete"]!=basename($_SERVER["SCRIPT_FILENAME"]))
  242. { if (@unlink(basename($_REQUEST["delete"])))
  243.     echo ("<p class=\"success\">".$_REQUEST["delete"]." was removed successfully</p>\n");
  244.   else
  245.     echo ("<p class=\"error\">Can't remove ".$_REQUEST["delete"]."</p>\n");
  246. }
  247.  
  248.  
  249. // Open the database
  250.  
  251. if (!$error)
  252. { $dbconnection = @mysql_connect($db_server,$db_username,$db_password);
  253.   if ($dbconnection)
  254.     $db = mysql_select_db($db_name);
  255.   if (!$dbconnection || !$db)
  256.   { echo ("<p class=\"error\">Database connection failed due to ".mysql_error()."</p>\n");
  257.     echo ("<p>Edit the database settings in ".$_SERVER["SCRIPT_FILENAME"]." or contact your database provider</p>\n");
  258.     $error=true;
  259.   }
  260. }
  261.  
  262.  
  263. // List uploaded files in multifile mode
  264.  
  265. if (!$error && !isset($_REQUEST["fn"]) && $filename=="")
  266. { if ($dirhandle = opendir($upload_dir))
  267.   { $dirhead=false;
  268.     while (false !== ($dirfile = readdir($dirhandle)))
  269.     { if ($dirfile != "." && $dirfile != ".." && $dirfile!=basename($_SERVER["SCRIPT_FILENAME"]))
  270.       { if (!$dirhead)
  271.         { echo ("<table cellspacing=\"2\" cellpadding=\"2\">\n");
  272.           echo ("<tr><th>Filename</td><th>Size</td><th>Date&amp;Time</td><th>Type</td><th>&nbsp;</td><th>&nbsp;</td>\n");
  273.           $dirhead=true;
  274.         }
  275.         echo ("<tr><td>$dirfile</td><td class=\"right\">".filesize($dirfile)."</td><td>".date ("Y-m-d H:i:s", filemtime($dirfile))."</td>");
  276.         if (eregi("\.gz$",$dirfile))
  277.           echo ("<td>GZip</td>");
  278.         else
  279.           echo ("<td>SQL</td>");
  280.         if (!eregi("\.gz$",$dirfile) || function_exists("gzopen"))
  281.           echo ("<td><a href=\"".$_SERVER["PHP_SELF"]."?start=1&fn=$dirfile&foffset=0&totalqueries=0\">Start Import</a> into $db_name at $db_server</td>\n");
  282.         else
  283.           echo ("<td>&nbsp;</td>\n");
  284.         echo ("<td><a href=\"".$_SERVER["PHP_SELF"]."?delete=$dirfile\">Delete file</a></td></tr>\n");
  285.       }
  286.  
  287.     }
  288.     if ($dirhead) echo ("</table>\n");
  289.     else echo ("<p>No uploaded files found in the working directory</p>\n");
  290.     closedir($dirhandle);
  291.   }
  292.   else
  293.   { echo ("<p class=\"error\">Error listing directory $upload_dir</p>\n");
  294.     $error=$true;
  295.   }
  296. }
  297.  
  298.  
  299. // Single file mode
  300.  
  301. if (!$error && !isset ($_REQUEST["fn"]) && $filename!="")
  302. { echo ("<p><a href=\"".$_SERVER["PHP_SELF"]."?start=1&fn=$filename&foffset=0&totalqueries=0\">Start Import</a> from $filename into $db_name at $db_server</p>\n");
  303. }
  304.  
  305.  
  306. // File Upload Form
  307.  
  308. if (!$error && !isset($_REQUEST["fn"]) && $filename=="")
  309. {
  310.  
  311. // Test permissions on working directory
  312.  
  313.   do { $tempfilename=time().".tmp"; } while (file_exists($tempfilename));
  314.   if (!($tempfile=@fopen($tempfilename,"w")))
  315.   { echo ("<p>Upload form disabled. Permissions for the working directory <i>$upload_dir</i> <b>must be set to 777</b> in order ");
  316.     echo ("to upload files from here. Alternatively you can upload your dump files via FTP.</p>\n");
  317.   }
  318.   else
  319.   { fclose($tempfile);
  320.     unlink ($tempfilename);
  321.  
  322.     echo ("<p>You can now upload your dump file up to $upload_max_filesize bytes (".round ($upload_max_filesize/1024/1024)." Mbytes)  ");
  323.     echo ("directly from your browser to the server. Alternatively you can upload your dump files of any size via FTP.</p>\n");
  324. ?>
  325. <form method="POST" action="<? echo ($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
  326. <input type="hidden" name="MAX_FILE_SIZE" value="$upload_max_filesize">
  327. <p>Dump file: <input type="file" name="dumpfile" accept="*/*" size=60"></p>
  328. <p><input type="submit" name="uploadbutton" value="Upload"></p>
  329. </form>
  330. <?
  331.   }
  332. }
  333.  
  334.  
  335. // Open the file
  336.  
  337. if (!$error && isset($_REQUEST["fn"]))
  338. {
  339.  
  340. // Recognize GZip filename
  341.  
  342.   if (eregi("\.gz$",$_REQUEST["fn"]))
  343.     $gzipmode=true;
  344.   else
  345.     $gzipmode=false;
  346.  
  347.   if ((!$gzipmode && !$file=fopen($_REQUEST["fn"],"rt")) || ($gzipmode && !$file=gzopen($_REQUEST["fn"],"rt")))
  348.   { echo ("<p class=\"error\">Can't open ".$_REQUEST["fn"]." for import</p>\n");
  349.     echo ("<p>You have to upload the ".$_REQUEST["fn"]." to the server</p>\n");
  350.     $error=true;
  351.   }
  352.  
  353. // Get the file size (can't do it fast on gzipped files, no idea how)
  354.  
  355.   else if ((!$gzipmode && fseek($file, 0, SEEK_END)==0) || ($gzipmode && gzseek($file, 0, SEEK_SET)==0))
  356.   { if (!$gzipmode) $filesize = ftell($file);
  357.     else $filesize = gztell($file); // Always zero, ignore
  358.   }
  359.   else
  360.   { echo ("<p class=\"error\">I can't get the filesize of ".$_REQUEST["fn"]."</p>\n");
  361.     $error=true;
  362.   }
  363. }
  364.  
  365.  
  366. // ****************************************************
  367. // START IMPORT SESSION HERE
  368. // ****************************************************
  369.  
  370. if (!$error && isset($_REQUEST["start"]) && isset($_REQUEST["foffset"]))
  371. {
  372.   echo ("<p>Processing file: ".$_REQUEST["fn"]."</p>\n");
  373.   echo ("<p>Starting at the line: ".$_REQUEST["start"]."</p>\n");
  374.  
  375. // Check $_REQUEST["foffset"] upon $filesize (can't do it on gzipped files)
  376.  
  377.   if (!$gzipmode && $_REQUEST["foffset"]>$filesize)
  378.   { echo ("<p class=\"error\">UNEXPECTED: Can't set file pointer behind the end of file</p>\n");
  379.     $error=true;
  380.   }
  381.  
  382. // Set file pointer to $_REQUEST["foffset"]
  383.  
  384.   if (!$error && ((!$gzipmode && fseek($file, $_REQUEST["foffset"])!=0) || ($gzipmode && gzseek($file, $_REQUEST["foffset"])!=0)))
  385.   { echo ("<p class=\"error\">UNEXPECTED: Can't set file pointer to offset: ".$_REQUEST["foffset"]."</p>\n");
  386.     $error=true;
  387.   }
  388.  
  389. // Start processing queries from $file
  390.  
  391.   if (!$error)
  392.   { $query="";
  393.     $queries=0;
  394.     $totalqueries=$_REQUEST["totalqueries"];
  395.     $linenumber=$_REQUEST["start"];
  396.     $inparents=false;
  397.  
  398.     while (($linenumber<$_REQUEST["start"]+$linespersession || $query!="")
  399.        && ((!$gzipmode && $dumpline=fgets($file, MAX_LINE_LENGTH)) || ($gzipmode && $dumpline=gzgets($file, MAX_LINE_LENGTH))))
  400.     {
  401.      
  402. // Handle DOS and Mac encoded linebreaks (I don't know if it will work on Win32 or Mac Servers)
  403.  
  404.       $dumpline=ereg_replace("\r\n$", "\n", $dumpline);
  405.       $dumpline=ereg_replace("\r$", "\n", $dumpline);
  406.      
  407. // DIAGNOSTIC
  408. // echo ("<p>Line $linenumber: $dumpline</p>\n");
  409.  
  410. // Skip comments and blank lines only if NOT in parents
  411.  
  412.       if (!$inparents)
  413.       { $skipline=false;
  414.         reset($comment);
  415.         foreach ($comment as $comment_value)
  416.         { if (!$inparents && (trim($dumpline)=="" || strpos ($dumpline, $comment_value) === 0))
  417.           { $skipline=true;
  418.             break;
  419.           }
  420.         }
  421.         if ($skipline)
  422.         { $linenumber++;
  423.           continue;
  424.         }
  425.       }
  426.  
  427. // Remove double back-slashes from the dumpline prior to count the quotes ('\\' can only be within strings)
  428.      
  429.       $dumpline_deslashed = str_replace ("\\\\","",$dumpline);
  430.  
  431. // Count ' and \' in the dumpline to avoid query break within a text field ending by ;
  432. // Please don't use double quotes ('"')to surround strings, it wont work
  433.  
  434.       $parents=substr_count ($dumpline_deslashed, "'")-substr_count ($dumpline_deslashed, "\\'");
  435.       if ($parents % 2 != 0)
  436.         $inparents=!$inparents;
  437.  
  438. // Add the line to query
  439.  
  440.       $query .= $dumpline;
  441.  
  442. // Execute query if end of query detected (; as last character) AND NOT in parents
  443.  
  444.       if (ereg(";$",trim($dumpline)) && !$inparents)
  445.       { if (!mysql_query(trim($query), $dbconnection))
  446.         { echo ("<p class=\"error\">Error at the line $linenumber: ". trim($dumpline)."</p>\n");
  447.           echo ("<p>Query: ".trim($query)."</p>\n");
  448.           echo ("<p>MySQL: ".mysql_error()."</p>\n");
  449.           $error=true;
  450.           break;
  451.         }
  452.         $totalqueries++;
  453.         $queries++;
  454.         $query="";
  455.       }
  456.       $linenumber++;
  457.     }
  458.   }
  459.  
  460. // Get the current file position
  461.  
  462.   if (!$error)
  463.   { if (!$gzipmode)
  464.       $foffset = ftell($file);
  465.     else
  466.       $foffset = gztell($file);
  467.     if (!$foffset)
  468.     { echo ("<p class=\"error\">UNEXPECTED: Can't read the file pointer offset</p>\n");
  469.       $error=true;
  470.     }
  471.   }
  472.  
  473. // Finish message and restart the script
  474.  
  475.   if (!$error)
  476.   { echo ("<p>Stopping at the line: ".($linenumber-1)."</p>\n");
  477.     echo ("<p>Queries performed (this session/total): $queries/$totalqueries</p>\n");
  478.     echo ("<p>Total bytes processed: $foffset (".round($foffset/1024)." KB)</p>\n");
  479.     if ($linenumber<$_REQUEST["start"]+$linespersession)
  480.     { echo ("<p class=\"success\">Congratulations: End of file reached, assuming OK</p>\n");
  481.       echo ("<p>Thank you for using! Please rate <a href=\"http://www.hotscripts.com/Detailed/20922.html\" target=\"_blank\">Bigdump at Hotscripts.com</a></p>\n");
  482.       echo ("<p>You can send me some bucks or euros as appreciation <a href=\"http://www.ozerov.de/bigdump\" target=\"_blank\">via PayPal</a></p>\n");
  483.       $error=true;
  484.     }
  485.     else
  486.     { if ($delaypersession!=0)
  487.         echo ("<p>Now I'm <b>waiting $delaypersession milliseconds</b> before starting next session...</p>\n");
  488.       echo ("<script language=\"JavaScript\" type=\"text/javascript\">window.setTimeout('location.href=\"".$_SERVER["PHP_SELF"]."?start=$linenumber&fn=".$_REQUEST["fn"]."&foffset=$foffset&totalqueries=$totalqueries\";',500+$delaypersession);</script>\n");
  489.       echo ("<noscript>\n");
  490.       echo ("<p><a href=\"".$_SERVER["PHP_SELF"]."?start=$linenumber&fn=".$_REQUEST["fn"]."&foffset=$foffset&totalqueries=$totalqueries\">Continue from the line $linenumber</a> (Enable JavaScript to do it automatically)</p>\n");
  491.       echo ("</noscript>\n");
  492.       echo ("<p>Press <a href=\"".$_SERVER["PHP_SELF"]."\">STOP</a> to abort the import <b>OR WAIT!</b></p>\n");
  493.     }
  494.   }
  495.   else
  496.     echo ("<p class=\"error\">Stopped on error</p>\n");
  497. }
  498.  
  499. if ($error)
  500.   echo ("<p><a href=\"".$_SERVER["PHP_SELF"]."\">Start from the beginning</a> (DROP the old tables before restarting)</p>\n");
  501.  
  502. if ($dbconnection) mysql_close();
  503. if ($file && !$gzipmode) fclose($file);
  504. else if ($file && $gzipmode) gzclose($file);
  505.  
  506. ?>
  507.  
  508. <p>© 2003 <a href="mailto:alexey@ozerov.de">Alexey Ozerov</a> - <a href="http://www.ozerov.de/bigdump" target="_blank">BigDump Home</a></p>
  509.  
  510. </td></tr></table>
  511.  
  512. </body>
  513. </html>
Add Comment
Please, Sign In to add comment