Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] kopia bazy
Forum PHP.pl > Forum > Przedszkole
Adyk
czesc,

jaka komenda tworzy kopie bazy mysql. chce tak aby od razu jak uruhcomi sie skrypt moc ja sciagnac. z tego co wiem troche jest ustawien w phpmyamdin aby zapisal z odpowienim formacie i strona kodowa.
mike
mysqldump — A Database Backup Program
Więcej? google.pl :: search :: mysql + dump
Zresztą na forum też było, nie pamiętam ale na pewno da się znaleść smile.gif
Lonas
kopia_mysql.php

  1. <?php
  2. $nazwa_pliku = date ("Y-m-d");
  3.  
  4. include ("mysqldumper.class.php");
  5.  
  6. $dumper = new Mysqldumper("localhost", "root", "haslo", "baza");
  7. $dumper->setDroptables(true);
  8. $dumpstring = $dumper->createDump();
  9.  
  10. $plik = "D:backupDB $nazwa_pliku.sql";
  11. $fp = fopen("$plik", "w");
  12. fwrite($fp, $dumpstring);
  13. fclose($fp);
  14. ?>


mysqldumper.class.php


  1. <?php
  2. /**
  3. * MySQLdumper is a small php class that lets you generate a dump of a MySQL data
    base
  4. * with just 2 lines of code. The dump can be used as a database backup. The dump
  5. * is a valid MySQL-query in plain text. It doesn't depend on the 'mysqldump' command
  6. * line utility, so you won't encounter a problem if this isn't available on the server.
  7. *
  8. * Example 1: Create a database drump.
  9. * <code>
  10. * $dumper = new MysqlDumper("localhost", "user", "password", "databasename");
  11. * $dumpstring = $dumper->createDump();
  12. * </code>
  13. *
  14. * Example 2: Create a database drump with a 'DROP TABLE IF EXISTS'-statement for each table.
  15. * <code>
  16. * $dumper = new MysqlDumper("localhost", "user", "password", "databasename");
  17. * $dumper->setDroptables(true);
  18. * $dumpstring = $dumper->createDump();
  19. * </code>
  20. *
  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 <a href="http://www.mosix.nl/mysqldumper" target="_blank">http://www.mosix.nl/mysqldumper</a>
  33. * @since php 4.0
  34. * @copyright Dennis Mozes
  35. * @license GNU/LGPL License: <a href="http://www.gnu.org/copyleft/lgpl.html" target="_blank">http://www.gnu.org/copyleft/lgpl.html</a>
  36. **/
  37. class Mysqldumper {
  38. var $_host;
  39. var $_dbuser;
  40. var $_dbpassword;
  41. var $_dbname;
  42. var $_isDroptables;
  43.  
  44. function Mysqldumper($host = "localhost", $dbuser = "root", $dbpassword = "haslo", $dbname = "baza") {
  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 = "rn";
  97.  
  98. $resource = mysql_connect($this->getHost(), $this->getDBuser(), $this->getDBpassword());
  99. mysql_select_db($this->getDbname(), $resource);
  100. $result = mysql_query("SHOW TABLES");
  101. $tables = $this->result2Array(0, $result);
  102. foreach ($tables as $tblval) {
  103. $result = mysql_query("SHOW CREATE TABLE `$tblval`");
  104. $createtable[$tblval] = $this->result2Array(1, $result);
  105. }
  106. // Set header
  107. $output = "#". $lf;
  108. $output .= "# mysqldumper SQL Dump" . $lf;
  109. $output .= "# Version 1.0" . $lf;
  110. $output .= "# ". $lf;
  111. $output .= "# Host: " . $this->getHost() . $lf;
  112. $output .= "# Czas wygenerowania: " . date("M j, Y at H:i") . $lf;
  113. $output .= "# Wersja serwera: ". mysql_get_server_info() . $lf;
  114. $output .= "# Wersja php: " . phpversion() . $lf;
  115. $output .= "# Baza danych : `" . $this->getDBname() . "`" . $lf;
  116. $output .= "#";
  117.  
  118. // Generate dumptext for the tables.
  119. foreach ($tables as $tblval)
  120.  {
  121. If (($tblval!="bip_logowanie") and ($tblval!="bip_users"))
  122.  {
  123. $output .= $lf . $lf . "# --$tabela------------------------------------------------------" . $lf . $lf;
  124. $output .= "#". $lf . "# Struktura tabeli dla `$tblval`" . $lf;
  125. $output .= "#" . $lf . $lf;
  126. // Generate DROP TABLE statement when client wants it to.
  127. if($this->isDroptables())
  128. {
  129. $output .= "DROP TABLE IF EXISTS `$tblval`;" . $lf;
  130. }
  131. $output .= $createtable[$tblval][0].";" . $lf;
  132. $output .= $lf;
  133. $output .= "#". $lf . "# Zrzut danych tabeli `$tblval`". $lf . "#" . $lf;
  134. $result = mysql_query("SELECT * FROM `$tblval`");
  135. $rows = $this->loadObjectList("", $result);
  136. foreach($rows as $row)
  137.  {
  138. $insertdump = $lf;
  139. $insertdump .= "INSERT INTO `$tblval` VALUES (";
  140. $arr = $this->object2Array($row);
  141. foreach($arr as $key => $value)
  142. {
  143. $value = addslashes($value);
  144. $value = str_replace("n", 'rn', $value);
  145. $value = str_replace("r", '', $value);
  146. $insertdump .= "'$value',";
  147. }
  148. $output .= rtrim($insertdump,',') . ");";
  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. ?>
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.