Guest User

mysqldump

a guest
Oct 16th, 2013
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. <?php
  2.  
  3. // Init
  4. error_reporting(E_ALL);
  5. header('Content-type: text/plain');
  6.  
  7. // Config
  8. $dbname = 'wordpress';
  9. mysql_connect('localhost', 'root', '');
  10. mysql_select_db($dbname);
  11.  
  12. // Open dump file
  13. $dumpfile = $dbname.'_'.date('Y-m-d_H-i').'.sql';
  14. $fp = fopen($dumpfile, 'w');
  15. if (!is_resource($fp)) {
  16.     exit('Backup failed: unable to open dump file');
  17. }
  18.  
  19. // Header
  20. $out = '-- SQL Dump
  21. --
  22. -- Generation: '.date('r').'
  23. -- MySQL version: '.mysql_get_server_info().'
  24. -- PHP version: '.phpversion().'
  25.  
  26. SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
  27.  
  28. --
  29. -- Database: `'.$dbname.'`
  30. --';
  31.  
  32. // Write
  33. fwrite($fp, $out);
  34. $out = '';
  35.  
  36. // Fetch tables
  37. $tables = mysql_query("SHOW TABLE STATUS");
  38. $c = 0;
  39. while ($table = mysql_fetch_assoc($tables)) {
  40.  
  41.     $tableName = $table['Name'];
  42.  
  43.     $tmp = mysql_query("SHOW CREATE TABLE `$tableName`");
  44.  
  45.     // Create table
  46.     $create = mysql_fetch_assoc($tmp);
  47.     $out .= "\n\n--\n-- Table structure: `$tableName`\n--\n\n".$create['Create Table'].' ;';
  48.  
  49.     // Clean
  50.     mysql_free_result($tmp);
  51.     unset($tmp);
  52.  
  53.     // Write
  54.     fwrite($fp, $out);
  55.     $out = '';
  56.  
  57.     // Rows
  58.     $tmp = mysql_query("SHOW COLUMNS FROM `$tableName`");
  59.     $rows = array();
  60.     while ($row = mysql_fetch_assoc($tmp)) {
  61.         $rows[] = $row['Field'];
  62.     }
  63.  
  64.     // Clean
  65.     mysql_free_result($tmp);
  66.     unset($tmp, $row);
  67.  
  68.     // Get data
  69.     $tmp = mysql_query("SELECT * FROM `$tableName`");
  70.     $count = mysql_num_rows($tmp);
  71.  
  72.     if ($count > 0) {
  73.  
  74.         $out .= "\n\n--\n-- Table data: `$tableName`\n--";
  75.         $out .= "\nINSERT INTO `$tableName` (`".implode('`, `', $rows)."`) VALUES ";
  76.  
  77.         $i = 1;
  78.         // Fetch data
  79.         while ($entry = mysql_fetch_assoc($tmp)) {
  80.  
  81.             // Create values
  82.             $out .= "\n(";
  83.             $tmp2 = array();
  84.  
  85.             foreach ($rows as $row) {
  86.                 $tmp2[] = "'" . mysql_real_escape_string($entry[$row]) . "'";
  87.             }
  88.  
  89.             $out .= implode(', ', $tmp2);
  90.             $out .= $i++ === $count ? ');' : '),';
  91.  
  92.             // Save
  93.             fwrite($fp, $out);
  94.             $out = '';
  95.         }
  96.  
  97.         // Clean
  98.         mysql_free_result($tmp);
  99.         unset($tmp, $tmp2, $i, $count, $entry);
  100.  
  101.     }
  102.  
  103.     // Operations counter
  104.     $c++;
  105. }
  106.  
  107. // Close dump file
  108. fclose($fp);
  109.  
  110. echo "Finished! Backup $c tables to `$dumpfile` (".filesize($dumpfile)." o).";
  111.  
  112. ?>
Add Comment
Please, Sign In to add comment