Backup.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace addons\database\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($backUpdir = 'download/')
  55. {
  56. $sql = $this->_init();
  57. $zip = new ZipArchive();
  58. $date = date('YmdHis');
  59. if (!is_dir($backUpdir)) {
  60. @mkdir($backUpdir, 0755);
  61. }
  62. $name = "backup-{$this->name}-{$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. $zip->addFromString($name . ".sql", $sql);
  68. $zip->close();
  69. }
  70. private function _init()
  71. {
  72. # COUNT
  73. $ct = 0;
  74. # CONTENT
  75. $sqldump = '';
  76. # COPYRIGHT & OPTIONS
  77. $sqldump .= "-- SQL Dump by Erik Edgren\n";
  78. $sqldump .= "-- version 1.0\n";
  79. $sqldump .= "--\n";
  80. $sqldump .= "-- SQL Dump created: " . date('F jS, Y \@ g:i a') . "\n\n";
  81. $sqldump .= "SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\";";
  82. $sqldump .= "\n\n\n\n-- --------------------------------------------------------\n\n\n\n";
  83. $tables = $this->db->query("SHOW FULL TABLES WHERE Table_Type != 'VIEW'");
  84. # LOOP: Get the tables
  85. foreach ($tables as $table) {
  86. // 忽略表
  87. if (in_array($table[0], $this->ignoreTables)) {
  88. continue;
  89. }
  90. # COUNT
  91. $ct++;
  92. /** ** ** ** ** **/
  93. # DATABASE: Count the rows in each tables
  94. $count_rows = $this->db->prepare("SELECT * FROM `" . $table[0] . "`");
  95. $count_rows->execute();
  96. $c_rows = $count_rows->columnCount();
  97. # DATABASE: Count the columns in each tables
  98. $count_columns = $this->db->prepare("SELECT COUNT(*) FROM `" . $table[0] . "`");
  99. $count_columns->execute();
  100. $c_columns = $count_columns->fetchColumn();
  101. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  102. # MYSQL DUMP: Remove tables if they exists
  103. $sqldump .= "--\n";
  104. $sqldump .= "-- Remove the table if it exists\n";
  105. $sqldump .= "--\n\n";
  106. $sqldump .= "DROP TABLE IF EXISTS `" . $table[0] . "`;\n\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[0] . "`") as $field) {
  114. $sqldump .= str_replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS', $field['Create Table']);
  115. }
  116. # MYSQL DUMP: New rows
  117. $sqldump .= ";\n\n\n";
  118. /** ** ** ** ** **/
  119. # CHECK: There are one or more columns
  120. if ($c_columns != 0) {
  121. # MYSQL DUMP: List the data for each table
  122. $sqldump .= "--\n";
  123. $sqldump .= "-- List the data for the table\n";
  124. $sqldump .= "--\n\n";
  125. # MYSQL DUMP: Insert into each table
  126. $sqldump .= "INSERT INTO `" . $table[0] . "` (";
  127. # ARRAY
  128. $rows = [];
  129. $numeric = [];
  130. # LOOP: Get the tables
  131. foreach ($this->db->query("DESCRIBE `" . $table[0] . "`") as $row) {
  132. $rows[] = "`" . $row[0] . "`";
  133. $numeric[] = (bool)preg_match('#^[^(]*(BYTE|COUNTER|SERIAL|INT|LONG$|CURRENCY|REAL|MONEY|FLOAT|DOUBLE|DECIMAL|NUMERIC|NUMBER)#i', $row[1]);
  134. }
  135. $sqldump .= implode(', ', $rows);
  136. $sqldump .= ") VALUES\n";
  137. # COUNT
  138. $c = 0;
  139. # LOOP: Get the tables
  140. foreach ($this->db->query("SELECT * FROM `" . $table[0] . "`") as $data) {
  141. # COUNT
  142. $c++;
  143. /** ** ** ** ** **/
  144. $sqldump .= "(";
  145. # ARRAY
  146. $cdata = [];
  147. # LOOP
  148. for ($i = 0; $i < $c_rows; $i++) {
  149. $value = $data[$i];
  150. if (is_null($value)) {
  151. $cdata[] = "NULL";
  152. } elseif ($numeric[$i]) {
  153. $cdata[] = $value;
  154. } else {
  155. $cdata[] = $this->db->quote($value);
  156. }
  157. }
  158. $sqldump .= implode(', ', $cdata);
  159. $sqldump .= ")";
  160. $sqldump .= ($c % 600 != 0 ? ($c_columns != $c ? ',' : ';') : '');
  161. # CHECK
  162. if ($c % 600 == 0) {
  163. $sqldump .= ";\n\n";
  164. } else {
  165. $sqldump .= "\n";
  166. }
  167. # CHECK
  168. if ($c % 600 == 0) {
  169. $sqldump .= "INSERT INTO `" . $table[0] . "`(";
  170. # ARRAY
  171. $rows = [];
  172. # LOOP: Get the tables
  173. foreach ($this->db->query("DESCRIBE `" . $table[0] . "`") as $row) {
  174. $rows[] = "`" . $row[0] . "`";
  175. }
  176. $sqldump .= implode(', ', $rows);
  177. $sqldump .= ") VALUES\n";
  178. }
  179. }
  180. }
  181. }
  182. $sqldump .= "\n\n\n";
  183. // Backup views
  184. $tables = $this->db->query("SHOW FULL TABLES WHERE Table_Type = 'VIEW'");
  185. # LOOP: Get the tables
  186. foreach ($tables as $table) {
  187. // 忽略表
  188. if (in_array($table[0], $this->ignoreTables)) {
  189. continue;
  190. }
  191. foreach ($this->db->query("SHOW CREATE VIEW `" . $table[0] . "`") as $field) {
  192. $sqldump .= "--\n";
  193. $sqldump .= "-- Remove the view if it exists\n";
  194. $sqldump .= "--\n\n";
  195. $sqldump .= "DROP VIEW IF EXISTS `{$field[0]}`;\n\n";
  196. $sqldump .= "--\n";
  197. $sqldump .= "-- Create the view if it not exists\n";
  198. $sqldump .= "--\n\n";
  199. $sqldump .= "{$field[1]};\n\n";
  200. }
  201. }
  202. return $sqldump;
  203. }
  204. }