Restore MySQL dump (backup) using PHP

The other day, I was looking for an easy way to restore a MySQL dump (or backup, whatever you like to call it) in PHP. I’ve previously used a segment of the code from PHP MySQL Backup V 2.2 for this, but it didn’t seem to support FULLTEXT indicies that well. So, I searched around, but couldn’t find anything. I even asked on the PHP IRC channel, and they suggested to use shell_exec to call mysql (unfortunately, I’ve disabled shell_exec for security reasons). Looking closer, I noticed that this was actually quite easy to do. Here’s the code I wrote to restore a phpMyAdmin MySQL dump (not sure if it works with mysqldump dumps):


< ?php
/*
* Restore MySQL dump using PHP
* (c) 2006 Daniel15
* Last Update: 9th December 2006
* Version: 0.1
*
* Please feel free to use any part of this, but please give me some credit :-)
*/

// Name of the file
$filename = 'test.sql';
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = '';
// Database name
$mysql_database = 'test';
 
//////////////////////////////////////////////////////////////////////////////////////////////
 
// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
 
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line_num => $line) {
  // Only continue if it's not a comment
  if (substr($line, 0, 2) != '--' && $line != '') {
    // Add this line to the current segment
    $templine .= $line;
    // If it has a semicolon at the end, it's the end of the query
    if (substr(trim($line), -1, 1) == ';') {
      // Perform the query
      mysql_query($templine) or print('Error performing query \'<b>' . $templine . '</b>\': ' . mysql_error() . '<br /><br />');
      // Reset temp variable to empty
      $templine = '';
    }
  }
}
 
?>

See? How easy is that? :D

Update: This also works for mysqldump dumps :)

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • Slashdot
  • Technorati
  • del.icio.us
  • StumbleUpon
  • Furl
  • Reddit
  • YahooMyWeb
  • Spurl
  • Netscape
  • NewsVine

11 Responses to “Restore MySQL dump (backup) using PHP”

  1. furkan Says:

    thanks.. i was thinking ‘why this thing saying there is an error’, i was giving whole file to mysql_query with file_get_contents :)

  2. steve Says:

    Plain text? Not with line numbers it isn’t.

    Nice script, but houw about a “download it here” link.

  3. Daniel15 Says:

    I don’t really like the current syntax highlighing script I’ve got at the moment, so I’ve removed it (it’s just plain text now).
    I’ll try to remember to add a “Download it here” link…

  4. Rahul Says:

    superb code. This code helped me alot.
    keep giving these types of help

  5. Cristiano Says:

    Really a great code, helped me a lot, but as the database is growing am having trouble restoring.
    The delay creates an error of time limit on the browser of 60 seconds and does not update.
    Now I am using set_time_limit and ignore_user_abort and is helping a bit.
    The script is the mysqldump with approximately 90mb.
    It improve it?

  6. Daniel15 Says:

    I’m not sure how to improve it for large dump files, as I’ve never had a MySQL dump that big. Perhaps you should consider using BigDump for it.

  7. Sampaguita Says:

    I’d suggest to take MySQL Dumper for big databases. I already used it for several large scale MySQL dumps (up to 1.8GB) and it works just fine. :)

  8. Huy Says:

    THANK YOU THANK YOU THANK YOU VERY MUCH!!!!

    My project is due in next 1 hour and I was very struggling with writting an installer. Your piece of code is really helpful to me, saved my day!

    Thank you!!!!

  9. Master Yoda Says:

    Thanks Daniel. Making the same mistake, I was. using file_get_contents.
    A great help you are.
    May the force be with u.

  10. Tt Says:

    really cool mannn

  11. speedovation Says:

    it’s really nice code.
    Yea its easy when u look at code but take hours to develop something like this.I’ve wasted my whole day finding something good to restore database.

    Thank you so much.

Leave a Reply