Community Documentation

Splitting large sql batch files when migrating to a new host

Last updated June 4, 2008. Created by renaud.richardet on June 4, 2008.
Log in to edit this page.

If you have to migrate a large sql batch file from your old server, chances are that the script in phpmyadmin will time out and only part of your database will be migrated. The script below will let you split a sql batch file in small chunks. It is intended to be run on your local web server (or CL).

<?php
print "hello<br>";

$splitEvery = 840000;
$base = "/home/ren/dev/ms/";
$ext  = '.sql';
$file_name = $base . 'db' . $ext;


// (the first) output file
$outCnt = 1;
$out = fopen($base . 'db_' . $outCnt . '.sql', "w");


// read input file
$fp = fopen($file_name, "r");
while(
$line = fgets($fp)) {

 
$strLen += strlen($line);
  if (
$strLen > $splitEvery  && preg_match("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", $line) ) {
    print
'empty line after ' . $strLen . '<br>';
   
$strLen = 0;

   
//new output file
   
fclose($out);
   
$outCnt++;
   
$out = fopen($base . 'db_' . $outCnt . '.sql', "w");
  }

 
//write to current out file
 
fwrite($out, "$line");
}

fclose($fp);

print
"done"; ?>

Comments

Alternative ways to migrate large sql batch files

How to prevent script timeout in phpMyAdmin
http://www.deuxcode.com/articles/091/how-to-prevent-script-timeout-in-ph...

BigDump: Staggered MySQL Dump Importer
http://www.ozerov.de/bigdump.php

Check out Skill Compass (an experiment for Drupal learning) and Course Planner for any subject.

About this page

Drupal version
Drupal 6.x

Reference

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here