Category: mysql

Split SQL dump into smaller files

In larger projects database can grow to impressive sizes of several gigabytes. I recently found myself needing to restore a table from a backup. But my texteditor was unable to open the dump.sql files of several gigabytes. So I whipped up a small PHP script that splits the file into small chunks of 1 MB each.

The table I was looking for was not that big, so the the CREATE and INSERT statements related to the table would end up into one file. A small ‘Find in files’ search would then reveal the file to check.

This method will work for any type of file, not only .sql files.

<?php
$fp = fopen('dump.sql', 'r');

$iter = 1;
while (!feof($fp)) {
    $fpOut = fopen($iter . '.sql', 'w');
    fputs($fpOut, fread($fp, 1024*1024));
    fclose($fpOut);
    $iter++;
}

Leave a Comment