Skip to content

Instantly share code, notes, and snippets.

@benjamw
Last active August 29, 2015 14:10
Show Gist options
  • Save benjamw/bd65bb0ff4f4c7106d61 to your computer and use it in GitHub Desktop.
Save benjamw/bd65bb0ff4f4c7106d61 to your computer and use it in GitHub Desktop.
Scripts for splitting and recombining large SQL dumps into more manageable pieces.
<?php
/*
Combine multiple SQL files using PHP command line.
Will combine to current working directory
Best used with files that were split using the SQL splitter: split.php
Will prepend and append the _root_.sql and _end_.sql files if found
Arguments:
1- sql starting filename to combine (inclusive)
2- sql ending filename to combine (inclusive) (optional, defaults to end of list)
Usage:
>php combine.php start_table.sql zee_last_table.sql
*/
$start = $argv[1]; // inclusive
$end = $argv[2]; // inclusive
if ( ! $start) {
die('START FILE REQUIRED');
}
echo 'Combining files from '.$start.' to ';
if ($end) {
echo $end."\n";
}
else {
echo 'end of file list'."\n";
}
$parts = pathinfo($start);
$path = $parts['dirname'].DIRECTORY_SEPARATOR;
$ext = $parts['extension'];
$outfile = $path.'_combined_.'.$ext;
$out = fopen($outfile, 'w');
// start file with root contents
if (is_readable('../__root_.sql')) {
echo "Processing ../__root_.sql ...\n";
$fileh = fopen('../__root_.sql', 'r');
while ($buffer = fread($fileh, 1024)) {
fwrite($out, $buffer);
}
fclose($fileh);
}
if (is_readable('__root_.sql')) {
echo "Processing __root_.sql ...\n";
$fileh = fopen('__root_.sql', 'r');
while ($buffer = fread($fileh, 1024)) {
fwrite($out, $buffer);
}
fclose($fileh);
}
if (is_readable('_root_.sql')) {
echo "Processing _root_.sql ...\n";
$fileh = fopen('_root_.sql', 'r');
while ($buffer = fread($fileh, 1024)) {
fwrite($out, $buffer);
}
fclose($fileh);
}
$dirh = opendir($path);
$started = $ending = false;
while (false !== ($file = readdir($dirh))) {
if (('_' === $file{0}) || ('.' === $file{0})) {
echo "Skipping {$file}\n";
continue;
}
if ($ending) {
echo "... Stopping\n";
break;
}
if ( ! $started && ($start !== $file)) {
echo "Not started, skipping {$file}\n";
continue;
}
$started = true;
if ($end === $file) {
$ending = true;
}
echo "Processing {$file} ...\n";
$fileh = fopen($file, 'r');
while ($buffer = fread($fileh, 1024)) {
fwrite($out, $buffer);
}
fclose($fileh);
fwrite($out, "\n\n");
}
if (is_readable('__end_.sql')) {
echo "Processing __end_.sql ...\n";
$fileh = fopen('__end_.sql', 'r');
while ($buffer = fread($fileh, 1024)) {
fwrite($out, $buffer);
}
fclose($fileh);
}
if (is_readable('../__end_.sql')) {
echo "Processing ../__end_.sql ...\n";
$fileh = fopen('../__end_.sql', 'r');
while ($buffer = fread($fileh, 1024)) {
fwrite($out, $buffer);
}
fclose($fileh);
}
fclose($out);
closedir($dirh);
die('DONE');
<?php
/*
Split SQL files using PHP command line.
Will split to current working directory
Arguments:
1- sql filename to split
2- split type: (optional, defaults to "table")
"table"- split the file into separate [table].sql files (default)
"db", "database"- split file into separate [database].sql files
"both"- split file into separate [table].sql files in their own [database] folder
Usage:
>php split.php sqldump.sql both
*/
$in = fopen($argv[1], 'r');
$type = ( ! empty($argv[2])) ? $argv[2] : 'table';
if ( ! $in) {
die('FILE ('.$argv[1].') NOT FOUND\nArguments:\n1. sql filename\n2. "table", [empty] - splits into table files in current directory: ./[table].sql\n "db", "database" - splits into database files in current directory: ./[database].sql\n "both" - splits into table files in database folders in current directory: ./[database]/[table].sql');
}
$parts = pathinfo($argv[1]);
$path = $parts['dirname'].DIRECTORY_SEPARATOR;
$dir = '';
$ext = $parts['extension'];
switch ($type) {
case 'both' :
echo "Processing {$argv[1]} into ./[database]/[table].sql structure\n";
$outfile = $path.'__root_.'.$ext;
$out = fopen($outfile, 'w');
echo "Starting {$outfile} ...\n";
while ($line = fgets($in)) {
if (preg_match('%^-- Current Database: `([^`]+)`%i', $line, $match)) {
mkdir($match[1]);
$dir = $match[1].DIRECTORY_SEPARATOR;
echo "\n------\nDATABASE: {$match[1]}\n------\n\n";
fclose($out);
$outfile = $path.$dir.'_root_.'.$ext;
$out = fopen($outfile, 'w');
echo "Starting {$outfile} ...\n";
}
if (preg_match('%^-- Table structure for table `([^`]+)`%i', $line, $match)) {
fclose($out);
$outfile = $path.$dir.$match[1].'.'.$ext;
$out = fopen($outfile, 'w');
echo "Starting {$outfile} ...\n";
}
fwrite($out, $line);
}
break;
case 'db' :
case 'database' :
echo "Processing {$argv[1]} into ./[database].sql structure\n";
$outfile = $path.'__root_.'.$ext;
$out = fopen($outfile, 'w');
echo "Starting {$outfile} ...\n";
while ($line = fgets($in)) {
if (preg_match('%^-- Current Database: `([^`]+)`%i', $line, $match)) {
fclose($out);
$outfile = $path.$match[1].'.'.$ext;
$out = fopen($outfile, 'w');
echo "Starting {$outfile} ...\n";
}
fwrite($out, $line);
}
break;
default :
echo "Processing {$argv[1]} into ./[table].sql structure\n";
$outfile = $path.'_root_.'.$ext;
$out = fopen($outfile, 'w');
echo "Starting {$outfile} ...\n";
while ($line = fgets($in)) {
if (preg_match('%^-- Table structure for table `([^`]+)`%i', $line, $match)) {
fclose($out);
$outfile = $path.$match[1].'.'.$ext;
$out = fopen($outfile, 'w');
echo "Starting {$outfile} ...\n";
}
fwrite($out, $line);
}
break;
}
fclose($in);
fclose($out);
die('DONE');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment