Maison > développement back-end > tutoriel php > 备份MySQL的php类

备份MySQL的php类

WBOY
Libérer: 2016-07-25 08:45:08
original
863 Les gens l'ont consulté
  1. define('MSB_VERSION', '1.0.0');
  2. define('MSB_NL', "\r\n");
  3. define('MSB_STRING', 0);
  4. define('MSB_DOWNLOAD', 1);
  5. define('MSB_SAVE', 2);
  6. define('__SEP__', "/*sep*/" );
  7. set_time_limit( 600 );
  8. class MySQL_Backup {
  9. var $server = 'localhost';
  10. var $port = 3306;
  11. var $username = 'root';
  12. var $password = '';
  13. var $database = '';
  14. var $link_id = -1;
  15. var $connected = false;
  16. var $tables = array();
  17. var $drop_tables = true;
  18. var $struct_only = false;
  19. var $comments = true;
  20. var $backup_dir = '';
  21. var $fname_format = 'd_m_y__H_i_s';
  22. var $error = '';
  23. var $complete_inserts = false;
  24. var $inserts_block = 200;
  25. function Execute($task = MSB_STRING, $fname = '', $compress = false) {
  26. if ( !( $sql = $this->_Retrieve() ) ) {
  27. return false;
  28. }
  29. if ( $task == MSB_SAVE ) {
  30. if (empty($fname)) {
  31. $fname = $this->backup_dir;
  32. $fname .= date($this->fname_format);
  33. $fname .= ($compress ? '.sql.gz' : '.sql');
  34. }
  35. return $this->_SaveToFile($fname, $sql, $compress);
  36. } elseif ($task == MSB_DOWNLOAD) {
  37. if ( empty( $fname ) ) {
  38. $fname = date($this->fname_format);
  39. $fname .= ($compress ? '.sql.gz' : '.sql');
  40. }
  41. return $this->_DownloadFile($fname, $sql, $compress);
  42. } else {
  43. return $sql;
  44. }
  45. }
  46. function _Connect() {
  47. $value = false;
  48. if (!$this->connected) {
  49. $host = $this->server . ':' . $this->port;
  50. $this->link_id = mysql_connect($host, $this->username, $this->password);
  51. }
  52. if ($this->link_id) {
  53. if (empty($this->database)) {
  54. $value = true;
  55. } elseif ($this->link_id !== -1) {
  56. $value = mysql_select_db($this->database, $this->link_id);
  57. } else {
  58. $value = mysql_select_db($this->database);
  59. }
  60. }
  61. if (!$value) {
  62. $this->error = mysql_error();
  63. }
  64. return $value;
  65. }
  66. function _Query($sql) {
  67. if ($this->link_id !== -1) {
  68. $result = mysql_query($sql, $this->link_id);
  69. } else {
  70. $result = mysql_query($sql);
  71. }
  72. if (!$result) {
  73. $this->error = mysql_error();
  74. }
  75. return $result;
  76. }
  77. function _GetTables() {
  78. $value = array();
  79. if ( !( $result = $this->_Query('SHOW TABLES') ) ) {
  80. return false;
  81. }
  82. while ( $row = mysql_fetch_row( $result ) ) {
  83. if ( empty( $this->tables) || in_array( $row[0], $this->tables ) ) {
  84. $value[] = $row[0];
  85. }
  86. }
  87. if (!sizeof($value)) {
  88. $this->error = 'No tables found in database.';
  89. return false;
  90. }
  91. return $value;
  92. }
  93. function _DumpTable( $table ) {
  94. $value = '';
  95. $this->_Query( 'LOCK TABLES ' . $table . ' WRITE' );
  96. if ( $this->comments ) {
  97. $value .= '#' . MSB_NL;
  98. $value .= '# Table structure for table `' . $table . '`' . MSB_NL;
  99. $value .= '#' . MSB_NL . MSB_NL;
  100. }
  101. if ( $this->drop_tables ) {
  102. $value .= 'DROP TABLE IF EXISTS `' . $table . '`;' . __SEP__ . MSB_NL;
  103. }
  104. if ( !( $result = $this->_Query('SHOW CREATE TABLE ' . $table) ) ) {
  105. return false;
  106. }
  107. $row = mysql_fetch_assoc($result);
  108. $value .= str_replace("\n", MSB_NL, $row['Create Table']) . ';' . __SEP__;
  109. $value .= MSB_NL . MSB_NL;
  110. if (!$this->struct_only) {
  111. if ($this->comments) {
  112. $value .= '#' . MSB_NL;
  113. $value .= '# Dumping data for table `' . $table . '`' . MSB_NL;
  114. $value .= '#' . MSB_NL . MSB_NL;
  115. }
  116. $value .= $this->_GetInserts($table);
  117. }
  118. $value .= MSB_NL . MSB_NL;
  119. $this->_Query('UNLOCK TABLES');
  120. return $value;
  121. }
  122. function _GetInserts($table) {
  123. $value = '';
  124. if (!($result = $this->_Query('SELECT * FROM ' . $table))) {
  125. return false;
  126. }
  127. if ( $this->complete_inserts ) {
  128. while ($row = mysql_fetch_row($result)) {
  129. $values = '';
  130. foreach ($row as $data) {
  131. $values .= '\'' . addslashes($data) . '\', ';
  132. }
  133. $values = substr($values, 0, -2);
  134. $value .= 'INSERT INTO ' . $table . ' VALUES (' . $values . ');' . __SEP__ . MSB_NL;
  135. }
  136. } else {
  137. $blocks_counter = 0;
  138. $blocks = array();
  139. while ($row = mysql_fetch_row($result)) {
  140. $values = array();
  141. foreach ($row as $data) {
  142. $values[] = '\'' . addslashes($data) . '\'';
  143. }
  144. $blocks[] = '(' . implode( ',', $values ) . ')';
  145. if ( $blocks_counter inserts_block ) {
  146. $blocks_counter++;
  147. } else {
  148. $value .= 'INSERT INTO ' . $table . ' VALUES ' . implode( ',', $blocks ) . ";" . __SEP__ . MSB_NL;
  149. $blocks = array();
  150. $blocks_counter = 0;
  151. }
  152. }
  153. if ( count( $blocks ) ) {
  154. $value .= 'INSERT INTO ' . $table . ' VALUES ' . implode( ',', $blocks ) . ";" . __SEP__ . MSB_NL;
  155. }
  156. }
  157. return $value;
  158. }
  159. function _Retrieve() {
  160. $value = '';
  161. if (!$this->_Connect()) {
  162. return false;
  163. }
  164. if ($this->comments) {
  165. $value .= '#' . MSB_NL;
  166. $value .= '# MySQL database dump' . MSB_NL;
  167. $value .= '# Created by MySQL_Backup class, ver. ' . MSB_VERSION . MSB_NL;
  168. $value .= '#' . MSB_NL;
  169. $value .= '# Host: ' . $this->server . MSB_NL;
  170. $value .= '# Generated: ' . date('M j, Y') . ' at ' . date('H:i') . MSB_NL;
  171. $value .= '# MySQL version: ' . mysql_get_server_info() . MSB_NL;
  172. $value .= '# PHP version: ' . phpversion() . MSB_NL;
  173. if (!empty($this->database)) {
  174. $value .= '#' . MSB_NL;
  175. $value .= '# Database: `' . $this->database . '`' . MSB_NL;
  176. }
  177. $value .= '#' . MSB_NL . MSB_NL . MSB_NL;
  178. }
  179. if (!($tables = $this->_GetTables())) {
  180. return false;
  181. }
  182. foreach ($tables as $table) {
  183. if (!($table_dump = $this->_DumpTable($table))) {
  184. $this->error = mysql_error();
  185. return false;
  186. }
  187. $value .= $table_dump;
  188. }
  189. return $value;
  190. }
  191. function _SaveToFile($fname, $sql, $compress) {
  192. if ($compress) {
  193. if (!($zf = gzopen($fname, 'w9'))) {
  194. $this->error = 'Can\'t create the output file.';
  195. return false;
  196. }
  197. gzwrite($zf, $sql);
  198. gzclose($zf);
  199. } else {
  200. if (!($f = fopen($fname, 'w'))) {
  201. $this->error = 'Can\'t create the output file.';
  202. return false;
  203. }
  204. fwrite($f, $sql);
  205. fclose($f);
  206. }
  207. return true;
  208. }
  209. function _DownloadFile($fname, $sql, $compress) {
  210. header('Content-disposition: filename=' . $fname);
  211. header('Content-type: application/octetstream');
  212. header('Pragma: no-cache');
  213. header('Expires: 0');
  214. echo ($compress ? gzencode($sql) : $sql);
  215. return true;
  216. }
  217. }
  218. ?>
复制代码

MySQL, php


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal