Advertisement
Guest User

Untitled

a guest
Nov 20th, 2016
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. <?php
  2. class DatabaseDump {
  3. private $db;
  4. private $host, $user, $pass, $dbname;
  5. private $sql, $removeAI;
  6.  
  7. public function __construct($host, $user, $pass, $dbname) {
  8. $this->host = $host;
  9. $this->user = $user;
  10. $this->pass = $pass;
  11. $this->dbname = $dbname;
  12. $this->removeAI = true;
  13.  
  14. try {
  15. $this->db = new PDO('mysql:dbname='.$dbname.';host='.$host, $user, $pass);
  16. $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  17. } catch(PDOException $e) {
  18. echo 'Connection failed: ' . $e->getMessage();
  19. die;
  20. }
  21. }
  22.  
  23. private function ln($text = '') {
  24. $this->sql = $this->sql . $text . "\n";
  25. }
  26.  
  27.  
  28. public function getInsert($table) {
  29. $output = "";
  30. $fields = "";
  31. $sep2 = "";
  32. $stmt = $this->db->query("SELECT * FROM $table");
  33. while($row = $stmt->fetch(PDO::FETCH_OBJ)){
  34. // runs once per table - create the INSERT INTO clause
  35. if($fields == ""){
  36. $fields = "INSERT INTO `$table` (";
  37. $sep = "";
  38. // grab each field name
  39. foreach($row as $col => $val){
  40. $fields .= $sep . "`$col`";
  41. $sep = ", ";
  42. }
  43. $fields .= ") VALUES";
  44. $output .= $fields . "\n";
  45. }
  46. // grab table data
  47. $sep = "";
  48. $output .= $sep2 . "(";
  49. foreach($row as $col => $val){
  50. // add slashes to field content
  51. $val = addslashes($val);
  52. // replace stuff that needs replacing
  53. //$val = str_replace($search, $replace, $val);
  54. $output .= $sep . "'$val'";
  55. $sep = ", ";
  56. }
  57. // terminate row data
  58. $output .= ")";
  59. $sep2 = ",\n";
  60. }
  61. // terminate insert data
  62. $output .= ";\n";
  63.  
  64. return $output;
  65. }
  66.  
  67. public function dump($file) {
  68. $this->ln("SET FOREIGN_KEY_CHECKS=0;\n");
  69.  
  70. $tables = $this->db->query('SHOW TABLES')->fetchAll(PDO::FETCH_BOTH);
  71.  
  72. foreach ($tables as $table) {
  73. $table = $table[0];
  74. $this->ln('DROP TABLE IF EXISTS `'.$table.'`;');
  75.  
  76. $schemas = $this->db->query("SHOW CREATE TABLE `{$table}`")->fetchAll(PDO::FETCH_ASSOC);
  77.  
  78. foreach ($schemas as $schema) {
  79. $schema = $schema['Create Table'];
  80. if($this->removeAI) $schema = preg_replace('/AUTO_INCREMENT=([0-9]+)(\s{0,1})/', '', $schema);
  81. $this->ln($schema.";\n\n");
  82.  
  83. $insertSql = $this->getInsert($table);
  84. $this->ln($insertSql);
  85. }
  86. }
  87.  
  88. file_put_contents($file, $this->sql);
  89. }
  90. }
  91.  
  92. $host = 'localhost';
  93. $user = 'root';
  94. $pass = '';
  95. $dbname = 'database';
  96.  
  97. $date = date('Y-m-d-H-i');
  98. $filename = "dump.{$date}.sql";
  99. $dbDump = new DatabaseDump($host, $user, $pass, $dbname);
  100. $dbDump->dump($filename);
  101.  
  102. exit(sprintf('<a href="%s" target="_blank">Download file</a>', $filename));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement