Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Kopia zapasowa w MySQLu
Forum PHP.pl > Forum > Bazy danych > MySQL
rsobczuk
Witam wszystkich...
potrzebuję zrobić backup wszystkich tabel w MySQLu, ale...
... musiało by to być zrobione poprzez zapytanie MySQL, a nie program mysqldump...

Chcę, żeby cała struktura tabel (dokładnie to co robi mysqldump) zapisała się do pliku. Czy jest to wogóle możliwe?questionmark.gif

Chcę w ten sposób rozwiązać problem, ponieważ mam bazę danych na serwerze komercyjnym, a ten nie oferuje dostępu do programu mysqldump.

Z góry dziękuję za jakie kolwiek pomysły.
Pozdrawiam
Rafał
mario
Użyj poniższego kodu:
kopia_mysql.php

  1. <?php
  2. $nazwa_pliku = date (&#092;"Y-m-d\");
  3.  
  4. include (&#092;"mysqldumper.class.php\");
  5.  
  6. $dumper = new Mysqldumper($hostname, $username, $password, $database); 
  7. $dumper->setDroptables(true);
  8. $dumpstring = $dumper->createDump();
  9.  
  10. $plik = &#092;"$nazwa_pliku.sql\";
  11. $fp = fopen(&#092;"$plik\", \"w\");
  12. fwrite($fp, $dumpstring);
  13. fclose($fp);
  14. ?>

a do tego klasa include'owana:
mysqldumper.class.php

  1. <?php
  2. /**
  3. * MySQLdumper is a small php class that lets you generate a dump of a MySQL data
  4. ase 
  5. * with just 2 lines of code. The dump can be used as a database backup. The dump
  6. * is a valid MySQL-query in plain text. It doesn't depend on the 'mysqldump' command
  7. * line utility, so you won't encounter a problem if this isn't available on the server.
  8. *
  9. * Example 1: Create a database drump.
  10. * <code>
  11. * $dumper = new MysqlDumper(\"localhost\", \"user\", \"password\", \"databasename\");
  12. * $dumpstring = $dumper->createDump();
  13. * </code>
  14. *
  15. * Example 2: Create a database drump with a 'DROP TABLE IF EXISTS'-statement for each table.
  16. * <code>
  17. * $dumper = new MysqlDumper(\"localhost\", \"user\", \"password\", \"databasename\");
  18. * $dumper->setDroptables(true);
  19. * $dumpstring = $dumper->createDump();
  20. * </code>
  21. * Example 3: Create dumps of different databases.
  22. * <code>
  23. * $dumper = new MysqlDumper(\"localhost\", \"user\", \"password\", \"database1\");
  24. * $dumpstring1 = $dumper->createDump();
  25. * $dumper->setDBname\"(\"database2\");
  26. * $dumpstring2 = $dumper->createDump();
  27. * </code>
  28. *
  29. * @package MySQLdumper
  30. * @version 1.0
  31. * @author  Dennis Mozes <opensource@mosix.nl>
  32. * @url http://www.mosix.nl/mysqldumper
  33. * @since php 4.0
  34. * @copyright Dennis Mozes
  35. * @license GNU/LGPL License: http://www.gnu.org/copyleft/lgpl.html
  36. **/
  37. class Mysqldumper {
  38. var $_host;
  39. var $_dbuser;
  40. var $_dbpassword;
  41. var $_dbname;
  42. var $_isDroptables;
  43.  
  44. function Mysqldumper($host = &#092;"localhost\", $dbuser = \"\", $dbpassword = \"\", $dbname = \"\") {
  45. $this->setHost($host);
  46. $this->setDBuser($dbuser);
  47. $this->setDBpassword($dbpassword);
  48. $this->setDBname($dbname);
  49. // Don't drop tables by default.
  50. $this->setDroptables(false);
  51. }
  52.  
  53. function setHost($host) {
  54. $this->_host = $host;
  55. }
  56.  
  57. function getHost() {
  58. return $this->_host;
  59. }
  60.  
  61. function setDBname($dbname) {
  62. $this->_dbname = $dbname;
  63. }
  64.  
  65. function getDBname() {
  66. return $this->_dbname;
  67. }
  68.  
  69. function getDBuser() {
  70. return $this->_dbuser;
  71. }
  72.  
  73. function setDBpassword($dbpassword) {
  74. $this->_dbpassword = $dbpassword;
  75. }
  76.  
  77. function getDBpassword() {
  78. return $this->_dbpassword;
  79. }
  80.  
  81. function setDBuser($dbuser) {
  82. $this->_dbuser = $dbuser;
  83. }
  84.  
  85. // If set to true, it will generate 'DROP TABLE IF EXISTS'-statements for each table.
  86. function setDroptables($state) {
  87. $this->_isDroptables = $state;
  88. }
  89.  
  90. function isDroptables() {
  91. return $this->_isDroptables;
  92. }
  93.  
  94. function createDump() {
  95. // Set line feed
  96. $lf = &#092;"rn\";
  97.  
  98. $resource = mysql_connect($this->getHost(), $this->getDBuser(), $this->getDBpassword());
  99. mysql_select_db($this->getDbname(), $resource);
  100. $result = mysql_query(&#092;"SHOW TABLES\");
  101. $tables = $this->result2Array(0, $result);
  102. foreach ($tables as $tblval) {
  103. $result = mysql_query(&#092;"SHOW CREATE TABLE `$tblval`\");
  104. $createtable[$tblval] = $this->result2Array(1, $result);
  105. }
  106. // Set header
  107. $output = &#092;"#\". $lf;
  108. $output .= &#092;"# mysqldumper SQL Dump\" . $lf;
  109. $output .= &#092;"# Version 1.0\" . $lf;
  110. $output .= &#092;"# \". $lf;
  111. $output .= &#092;"# Host: \" . $this->getHost() . $lf;
  112. $output .= &#092;"# Czas wygenerowania: \" . date(\"M j, Y at H:i\") . $lf;
  113. $output .= &#092;"# Wersja serwera: \". mysql_get_server_info() . $lf;
  114. $output .= &#092;"# Wersja php: \" . phpversion() . $lf;
  115. $output .= &#092;"# Baza danych : `\" . $this->getDBname() . \"`\" . $lf;
  116. $output .= &#092;"#\";
  117.  
  118. // Generate dumptext for the tables.
  119. foreach ($tables as $tblval)
  120.  {
  121. If (($tblval!=&#092;"bip_logowanie\") and ($tblval!=\"bip_users\"))
  122.  {
  123. $output .= $lf . $lf . &#092;"# --$tabela------------------------------------------------------\" . $lf . $lf;
  124. $output .= &#092;"#\". $lf . \"# Struktura tabeli dla `$tblval`\" . $lf;
  125. $output .= &#092;"#\" . $lf . $lf;
  126. // Generate DROP TABLE statement when client wants it to.
  127. if($this->isDroptables())
  128. {
  129. $output .= &#092;"DROP TABLE IF EXISTS `$tblval`;\" . $lf;
  130. }
  131. $output .= $createtable[$tblval][0].&#092;";\" . $lf;
  132. $output .= $lf;
  133. $output .= &#092;"#\". $lf . \"# Zrzut danych tabeli `$tblval`\". $lf . \"#\" . $lf;
  134. $result = mysql_query(&#092;"SELECT * FROM `$tblval`\");
  135. $rows = $this->loadObjectList(&#092;"\", $result);
  136. foreach($rows as $row)
  137.  {
  138. $insertdump = $lf;
  139. $insertdump .= &#092;"INSERT INTO `$tblval` VALUES (\";
  140. $arr = $this->object2Array($row);
  141. foreach($arr as $key => $value)
  142. {
  143. $value = addslashes($value);
  144. $value = str_replace(&#092;"n\", 'rn', $value);
  145. $value = str_replace(&#092;"r\", '', $value);
  146. $insertdump .= &#092;"'$value',\";
  147. }
  148. $output .= rtrim($insertdump,',') . &#092;");\";
  149. }
  150. }
  151. }
  152. mysql_close($resource);
  153. return $output;
  154. }
  155.  
  156. // Private function object2Array.
  157. function object2Array($obj) {
  158. $array = null;
  159. if(is_object($obj)) {
  160. $array = array();
  161. foreach (get_object_vars($obj) as $key => $value) {
  162. if(is_object($value))
  163. $array[$key] = $this->object2Array($value);
  164. else
  165. $array[$key] = $value;
  166. }
  167. }
  168. return $array;
  169. }
  170.  
  171. // Private function loadObjectList.
  172. function loadObjectList($key='', $resource) {
  173. $array = array();
  174. while ($row = mysql_fetch_object($resource)) {
  175. if ($key)
  176. $array[$row->$key] = $row;
  177. else
  178. $array[] = $row;
  179. }
  180. mysql_free_result($resource);
  181. return $array;
  182. }
  183.  
  184. // Private function result2Array.
  185. function result2Array($numinarray = 0, $resource) {
  186. $array = array();
  187. while ($row = mysql_fetch_row($resource)) {
  188. $array[] = $row[$numinarray];
  189. }
  190. mysql_free_result($resource);
  191. return $array;
  192. }
  193. }
  194. ?>
rsobczuk
Dziękuję...
Działa.
Pozdrawiam.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.