Backup.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace addons\famysql\library;
  3. use Exception;
  4. use fast\Random;
  5. use PDO;
  6. use ZipArchive;
  7. class Backup
  8. {
  9. private $host = '';
  10. private $user = '';
  11. private $name = '';
  12. private $pass = '';
  13. private $port = '';
  14. private $tables = ['*'];
  15. private $ignoreTables = [];
  16. private $db;
  17. private $ds = "\n";
  18. public function __construct($host = null, $user = null, $name = null, $pass = null, $port = 3306)
  19. {
  20. if ($host !== null) {
  21. $this->host = $host;
  22. $this->name = $name;
  23. $this->port = $port;
  24. $this->pass = $pass;
  25. $this->user = $user;
  26. }
  27. $this->db = new PDO('mysql:host=' . $this->host . ';dbname=' . $this->name . '; port=' . $port, $this->user, $this->pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
  28. $this->db->exec('SET NAMES "utf8"');
  29. }
  30. /**
  31. * 设置备份表
  32. * @param $table
  33. * @return $this
  34. */
  35. public function setTable($table)
  36. {
  37. if ($table) {
  38. $this->tables = is_array($table) ? $table : explode(',', $table);
  39. }
  40. return $this;
  41. }
  42. /**
  43. * 设置忽略备份的表
  44. * @param $table
  45. * @return $this
  46. */
  47. public function setIgnoreTable($table)
  48. {
  49. if ($table) {
  50. $this->ignoreTables = is_array($table) ? $table : explode(',', preg_replace('/\s+/', '', $table));
  51. }
  52. return $this;
  53. }
  54. public function backup($addon = 'all', $type = 0, $backUpdir = 'download/')
  55. {
  56. $sql = $this->_init($type);
  57. $zip = new ZipArchive();
  58. $date = date('YmdHis');
  59. if (!is_dir($backUpdir)) {
  60. @mkdir($backUpdir, 0755);
  61. }
  62. $name = "backup-{$this->name}-{$addon}-{$type}-{$date}-" . Random::alnum(6);
  63. $filename = $backUpdir . $name . ".zip";
  64. if ($zip->open($filename, ZIPARCHIVE::CREATE) !== true) {
  65. throw new Exception("Could not open <$filename>\n");
  66. }
  67. if ($type == 0) {
  68. $sql = preg_replace('/AUTO_INCREMENT=\d+ /', '', $sql);
  69. }
  70. $zip->addFromString($name . ".sql", $sql);
  71. $zip->close();
  72. }
  73. private function _init($type)
  74. {
  75. # COUNT
  76. $ct = 0;
  77. # CONTENT
  78. $sqldump = '';
  79. # COPYRIGHT & OPTIONS
  80. $sqldump .= "-- MySQL dump\n";
  81. $sqldump .= "-- version 1.0\n";
  82. $sqldump .= "--\n";
  83. $sqldump .= "-- SQL Dump created: " . date('F jS, Y \@ g:i a') . "\n\n";
  84. $sqldump .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";";
  85. $sqldump .= "\n\n-- --------------------------------------------------------\n\n";
  86. $tables = array_diff($this->tables, $this->ignoreTables);
  87. # LOOP: Get the tables
  88. foreach ($tables as $table) {
  89. # COUNT
  90. $ct++;
  91. /** ** ** ** ** **/
  92. # DATABASE: Count the rows in each tables
  93. $count_rows = $this->db->prepare("SELECT * FROM `" . $table . "`");
  94. $count_rows->execute();
  95. $c_rows = $count_rows->columnCount();
  96. # DATABASE: Count the columns in each tables
  97. $count_columns = $this->db->prepare("SELECT COUNT(*) FROM `" . $table . "`");
  98. $count_columns->execute();
  99. $c_columns = $count_columns->fetchColumn();
  100. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  101. if ($type != 1) {
  102. # MYSQL DUMP: Remove tables if they exists
  103. $sqldump .= "--\n";
  104. $sqldump .= "-- Table structure for table `" . $table . "`\n";
  105. $sqldump .= "--\n\n";
  106. $sqldump .= "DROP TABLE IF EXISTS `" . $table . "`;\n\n";
  107. /** ** ** ** ** **/
  108. # MYSQL DUMP: Create table if they do not exists
  109. $sqldump .= "--\n";
  110. $sqldump .= "-- Create the table if it not exists\n";
  111. $sqldump .= "--\n\n";
  112. # LOOP: Get the fields for the table
  113. foreach ($this->db->query("SHOW CREATE TABLE `" . $table . "`") as $field) {
  114. $sqldump .= str_replace('CREATE TABLE', 'CREATE TABLE', $field['Create Table']);
  115. }
  116. # MYSQL DUMP: New rows
  117. $sqldump .= ";\n\n";
  118. /** ** ** ** ** **/
  119. }
  120. # CHECK: There are one or more columns
  121. if ($c_columns != 0 && $type != 0) {
  122. # MYSQL DUMP: List the data for each table
  123. $sqldump .= "--\n";
  124. $sqldump .= "-- Dumping data for table `" . $table . "`\n";
  125. $sqldump .= "--\n\n";
  126. $sqldump .= "LOCK TABLES `" . $table . "` WRITE;\n";
  127. # MYSQL DUMP: Insert into each table
  128. $sqldump .= "INSERT INTO `" . $table . "` (";
  129. # ARRAY
  130. $rows = [];
  131. $numeric = [];
  132. # LOOP: Get the tables
  133. foreach ($this->db->query("DESCRIBE `" . $table . "`") as $row) {
  134. $rows[] = "`" . $row[0] . "`";
  135. $numeric[] = (bool) preg_match('#^[^(]*(BYTE|COUNTER|SERIAL|INT|LONG$|CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER)#i', $row[1]);
  136. }
  137. $sqldump .= implode(', ', $rows);
  138. $sqldump .= ") VALUES\n";
  139. # COUNT
  140. $c = 0;
  141. # LOOP: Get the tables
  142. foreach ($this->db->query("SELECT * FROM `" . $table . "`") as $data) {
  143. # COUNT
  144. $c++;
  145. /** ** ** ** ** **/
  146. $sqldump .= "(";
  147. # ARRAY
  148. $cdata = [];
  149. # LOOP
  150. for ($i = 0; $i < $c_rows; $i++) {
  151. $value = $data[$i];
  152. if (is_null($value)) {
  153. $cdata[] = "NULL";
  154. } elseif ($numeric[$i]) {
  155. $cdata[] = $value;
  156. } else {
  157. $cdata[] = $this->db->quote($value);
  158. }
  159. }
  160. $sqldump .= implode(', ', $cdata);
  161. $sqldump .= ")";
  162. $sqldump .= ($c % 600 != 0 ? ($c_columns != $c ? ',' : ';') : '');
  163. # CHECK
  164. if ($c % 600 == 0) {
  165. $sqldump .= ";\n\n";
  166. } else {
  167. $sqldump .= "\n";
  168. }
  169. # CHECK
  170. if ($c % 600 == 0) {
  171. $sqldump .= "INSERT INTO `" . $table . "`(";
  172. # ARRAY
  173. $rows = [];
  174. # LOOP: Get the tables
  175. foreach ($this->db->query("DESCRIBE `" . $table . "`") as $row) {
  176. $rows[] = "`" . $row[0] . "`";
  177. }
  178. $sqldump .= implode(', ', $rows);
  179. $sqldump .= ") VALUES\n";
  180. }
  181. }
  182. $sqldump .= "UNLOCK TABLES;\n\n";
  183. }
  184. }
  185. $sqldump .= "\n\n\n";
  186. return $sqldump;
  187. }
  188. }