Backing up MySQL's PHP class

Source: Internet
Author: User
Tags php class
  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 < $this->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. ?>
Copy Code
MySQL, PHP
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.