Advertisement
DECEiFER

MySQL Restore: PHP Script - Procedural (PHP 5.5+) Syntax

May 2nd, 2013
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.94 KB | None | 0 0
  1. <?php
  2. # MySQL Restore Script - revamped by DECEiFER (Last update: May 3rd, 2013)
  3. # This script is compatible with all regular MySQL backup types
  4. # -----------------
  5. # Revamped updates:
  6. # - MySQLi now used.
  7. # - Checks the specified MySQL backup file's MIME type (PHP 5.3.0 and above only) to see if it's correct (text/plain).
  8. # - Transactional commit or rollback for InnoDB tables at the end of the operation upon MySQL success or error.
  9. # - MySQL Backup file permission modification (chmod()) to enable reads in case your backup files have permission restrictions. When done, the original permissions are automatically restored.
  10. # - Removed the "<|||||||>" delimiters as they were causing problems during restoration via phpMyAdmin and MySQL Workbench.
  11. # - The SQL file's queries are now run in one execution. There is no more need for a delimiter between each query. Backups from phpMyAdmin and MySQL Workbench can also be handled by this script.
  12. # - Fixes and improvements to error handling.
  13. # - Some variable names and other insignificant things changed/tidied up.
  14.  
  15. ini_set('max_execution_time', 900);
  16.  
  17. // Get the provided arg
  18. $file = $_GET['file'];
  19.  
  20. // Check if the URI has the required parameters
  21. if ($file == null){
  22.     echo "<script type=\"text/javascript\">window.alert('You have not provided a backup to restore.');</script>";
  23.     echo "<script type=\"text/javascript\">window.location='backup_overview.php';</script>";
  24.     die('<noscript>You have not provided a backup to restore.<br />Click <a href="backup_overview.php">here</a> if your browser doesn\'t automatically redirect you.</noscript>');
  25. }
  26.  
  27. // Include your database's settings file or replace the line below with the database's credentials
  28. if (!include('config.php')) die('Cannot load the database settings.');
  29.  
  30. // Set the filename path
  31. $filename = "backup/" . $file;
  32.  
  33. // Check if the file exists
  34. if (!file_exists($filename)) die('The specified MySQL backup file does not exist.');
  35.  
  36. // If insufficient, give the SQL Backup file appropriate permissions for reading
  37. $filePerms = substr(decoct(fileperms($filename)), 3);
  38. if ($filePerms[0] < 4) chmod($filename, 0 . intval(4 . $filePerms[1] . $filePerms[2], 8));
  39.  
  40. // Check if the file's MIME type is correct (PHP 5.3.0 and above only)
  41. if (function_exists('finfo_open')){
  42.     $finfo = finfo_open(FILEINFO_MIME_TYPE);
  43.     if (($mimeType = finfo_file($finfo, $filename)) != "text/plain") die('You have attempted to restore an invalid file type: ' . $mimeType . '<br />Required file type: text/plain');
  44. }
  45.  
  46. // Restore the backup
  47. $link = mysqli_connect($DBhost, $DBuser, $DBpass) or die(mysqli_connect_error());
  48. if ($link){
  49.     // Turn off transation auto commit for InnoDB tables
  50.     mysqli_autocommit($link, false);
  51.    
  52.     // Select the database for restoration to
  53.     mysqli_select_db($link, $DBName);
  54.    
  55.     // Get the SQL file's contents into a string variable and then restore the file's original permissions
  56.     if (($sqlFile = file_get_contents($filename)) === false) die('Cannot read the MySQL backup file\'s contents.');
  57.     if (substr(decoct(fileperms($filename)), 3) != $filePerms) chmod($filename, 0 . intval($filePerms, 8));
  58.    
  59.     // Run every query in the SQL file with one execution
  60.     mysqli_multi_query($link, $sqlFile);
  61.    
  62.     // Go through the results and break when there is a MySQL error
  63.     while (mysqli_next_result($link));
  64.    
  65.     $sqlErrorCode = mysqli_errno($link);
  66. }
  67.  
  68. // Echo handling (success or error)
  69. if ($link && ($sqlErrorCode = mysqli_errno($link)) == 0){
  70.     mysqli_commit($link); // Only for InnoDB tables
  71.    
  72.     echo "Database restored successfully!<br>\n";
  73.     echo "Backup used: " . $filename;
  74. } elseif (!$link){
  75.     echo "Could not establish a connection to the database.";
  76. } else{
  77.     echo "An error occurred while restoring the backup!<br /><br />\n";
  78.     echo "Error code: " . $sqlErrorCode . "<br />\n";
  79.     echo "Error text: " . mysqli_error($link) . "<br />\n";
  80.    
  81.     mysqli_rollback($link); // Only for InnoDB tables
  82. }
  83.  
  84. // Close the connection
  85. mysqli_close($link);
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement