Last active
August 29, 2015 14:23
-
-
Save dcpesses/d77304c135ae5b820c62 to your computer and use it in GitHub Desktop.
Splits a LARGE sql dump into individual sql files of each database.
This file contains 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 | |
/** | |
* mysql_splitdump.php | |
* | |
* Splits a LARGE sql dump into individual sql files of each database | |
* | |
* Based off of example code from | |
* http://www.codediesel.com/php/splitting-large-mysql-dump-files/ | |
* | |
*/ | |
set_time_limit(600); | |
ini_set("auto_detect_line_endings", true); | |
// Source SQL Dump | |
$dump_file = "mysql-dump.sql"; | |
// Subdirectory to store split files | |
$dump_directory = "./sql-dump-split/"; | |
// Naming convention of each db file | |
// (replacing %s with the database name) | |
$split_file = "mysql-dump-db-%s.sql"; | |
$line_count = 0; | |
$file_count = 1; | |
$total_lines = 0; | |
$file_mod=""; | |
$handle = @fopen($dump_file, "r"); | |
$buffer = ""; | |
if ($handle) { | |
$dbname = ""; | |
while ( ($line = fgets($handle) ) !== false) { | |
// Split at new database lines | |
if (preg_match("/^-- Current Database\: \`(.+?)\`$/i", $line)) { | |
$file_dbname = $file_count."-".$dbname; | |
$file_name = $dump_directory . sprintf($split_file, $file_dbname); | |
echo "Writing to $file_name...\n"; | |
$out_write = @fopen($file_name, "w+"); | |
fputs($out_write, $buffer); | |
fclose($out_write); | |
// Save database name for next set of entries | |
$dbname = trim(preg_replace("/^-- Current Database\: \`(.+?)\`$/i", '$1', $line)); | |
echo "Found $dbname...\n"; | |
$buffer = ''; | |
$line_count = 0; | |
$file_count++; | |
} | |
// Send lines to buffer | |
$buffer .= $line; | |
$line_count++; | |
} | |
if ($buffer) { | |
// Write out the remaining buffer | |
$file_dbname = $file_count."-".$dbname; | |
$file_name = $dump_directory . sprintf($split_file, $file_dbname); | |
echo "Writing to $file_name...\n"; | |
$out_write = @fopen($file_name, "w+"); | |
fputs($out_write, $buffer); | |
fclose($out_write); | |
} | |
fclose($handle); | |
echo "done!"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment