Our blog

 

Updated MySQL Dump Splitter and Cleaner

My MySQL dump splitting script has come in handy countless times. Recently I also needed to remove keys from an SQL dump to enable me to import a dump that had keys associated with text columns.

Here is the updated script

*note - now updated again to strip out any drop table statements if splitting

PHP:
  1. <?php
  2. /*MySQL Dump Split to Tables
  3. * script supplied by Edmonds Commerce
  4. * www.edmondscommerce.co.uk
  5. * if you like it, please link back with a do-follow
  6. *
  7. * Now updated with better regex and also the ability to strip out keys other than the primary
  8. */
  9. echo '<h1>MySQL Dump Split to Tables + Remove Keys</h1>';
  10. if(!isset($_GET['dump_file'])){
  11.     echo '<form><b>Dump File Path:</b> <input name="dump_file"> <input type="submit" value="go"><select name="operation"><option value="split">split by tables</option><option value="keys">remove keys other than primary</option><option value="both">both</option></select></form>';
  12. }else{
  13.     if(preg_match('%keys|both%', $_GET['operation'])){
  14.         $contents = file_get_contents($_GET['dump_file']);
  15.         $pattern = '%,(s+?)KEY(.*?))%si';
  16.         $nokeys = preg_replace($pattern, '', $contents);       
  17.         $_GET['dump_file'] = 'nokeys.sql';
  18.         file_put_contents('nokeys.sql', $nokeys);
  19.     }
  20.     if(preg_match('%split|both%',$_GET['operation'])){
  21.         echo '<h3>Splitting File</h3>';
  22.         flush();
  23.         $handle = fopen($_GET['dump_file'], "r") or die('failed openeing source file ' . $_GET['dump_file']);
  24.         if ($handle) {
  25.             while (!feof($handle)) {
  26.                 $line = fgets($handle)
  27.                 if(preg_match('%^drop table(.+?);%i', $line)){
  28.                     continue;
  29.                 }               
  30.                 if(stristr($line, 'CREATE TABLE')){
  31.                     echo '. ';
  32.                     if(isset($out)){
  33.                         fclose($out);
  34.                         unset($out);
  35.                     }
  36.                     preg_match('%CREATE TABLE (`|)(w+)(`|)(s+)($%i', $line, $matches);
  37.                     $table_name = $matches[2];
  38.                     $out = fopen('output/'.$table_name . '.sql', 'w') or die('failed to create file ' . $table_name . '.sql');
  39.                     fwrite($out, $line . "n") or die('failed writing to file ' . $table_name . '.sql');
  40.                 }else{
  41.                     if(isset($out)){
  42.                         fwrite($out, $line . "n");
  43.                     }
  44.                 }      
  45.             }
  46.             fclose($out);
  47.             fclose($handle);
  48.         }else{
  49.             echo 'no handle on file - does it exist - permissions...';
  50.         }
  51.     }
  52. }
  53. echo '<h1>Done</h1>'
  54. ?>

More...

Updated MySQL Dump Splitter and Cleaner | Edmonds Commerce Blog
Branedy » Blog Archive » WordPress MySQL exercises
Python Script to Backup MySql Databases (WordPress or other databases)
Importing Large MySQL Databases With BigDump
MySQL Upgrade: 1and1 is not the best host in the world | TheGarage ...

More Reading:

2 Comments

Math
October 21st, 2009

Hi,

Thanks, Very nice stuff. But I cant understand what does this regular expression does?
if(preg_match('%split|both%',$_GET['operation']))

 

admin
October 21st, 2009

it lets us do one or both operations by specifying with the operation or both as the $_GET['operation']

 

 

Leave a Reply