PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/attic/p20/libs/backup/mysql_backup/mysql_backup.class.php

https://github.com/holsinger/openfloor
PHP | 348 lines | 228 code | 71 blank | 49 comment | 24 complexity | c3df46ba5b4144779c0414446fa3b007 MD5 | raw file
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. //
  4. // Class name: mysql_backup
  5. // PHP version: 4.0.2
  6. // Function: Back up/Restore a MySql db.
  7. // Written by: Peyman Hooshmandi Raad
  8. // Date: Jan 9, 2003
  9. // Contact: captain5ive@yahoo.com
  10. //=============================================================================
  11. //
  12. // Methods:
  13. //=============================================================================
  14. // 1) Backup this method will back up the whole database.
  15. // $output: text file name and path(the one that holds backup)
  16. // $structure_only: (true / false)
  17. // true : backup method will only make backup from tables NOT the data.
  18. // false : backup method will make backup from tables and the data(WHOLE DB).
  19. //
  20. // 2) Restor This method will create table with data from a txt file.
  21. //
  22. // Copyright: this class is free for non commercial uses.
  23. //
  24. ////////////////////////////////////////////////////////////////////////////////
  25. class mysql_backup
  26. {
  27. //---- Class Variables.
  28. //---------------------
  29. var $host; //---- host name e.g. localhost
  30. var $db; //---- db name
  31. var $user; //---- db username
  32. var $pass; //---- db password
  33. var $output; //---- file name(sqldata.txt)
  34. var $structure_only; //---- Output method : true/false
  35. var $fptr; //---- Do Not change this.
  36. //---------------------
  37. //---- Constructor function: This will Inisialize variables.
  38. //----------------------------------------------------------
  39. function mysql_backup($host,$db,$user,$pass,$output,$structure_only)
  40. {
  41. set_time_limit (120);
  42. $this->host = $host;
  43. $this->db = $db;
  44. $this->user = $user;
  45. $this->pass = $pass;
  46. $this->output = $output;
  47. $this->structure_only = $structure_only;
  48. }
  49. //----------------------------------------------------------
  50. //---- This will create the sqldata.txt file.
  51. //-------------------------------------------
  52. function _Mysqlbackup($host,$dbname, $uid, $pwd, $output, $structure_only)
  53. {
  54. if (strval($this->output)!="") $this->fptr=fopen($this->output,"w"); else $this->fptr=false;
  55. //connect to MySQL database
  56. $con=mysql_connect($this->host,$this->user, $this->pass);
  57. $db=mysql_select_db($dbname,$con);
  58. //open back-up file ( or no file for browser output)
  59. //set up database
  60. //out($this->fptr, "create database $dbname;\n\n");
  61. //enumerate tables
  62. $res=mysql_list_tables($dbname);
  63. $nt=mysql_num_rows($res);
  64. for ($a=0;$a<$nt;$a++)
  65. {
  66. $row=mysql_fetch_row($res);
  67. $tablename=$row[0];
  68. //start building the table creation query
  69. $sql="create table $tablename\n(\n";
  70. $res2=mysql_query("select * from $tablename",$con);
  71. $nf=mysql_num_fields($res2);
  72. $nr=mysql_num_rows($res2);
  73. $fl="";
  74. //parse the field info first
  75. for ($b=0;$b<$nf;$b++)
  76. {
  77. $fn=mysql_field_name($res2,$b);
  78. $ft=mysql_fieldtype($res2,$b);
  79. $fs=mysql_field_len($res2,$b);
  80. $ff=mysql_field_flags($res2,$b);
  81. $sql.=" $fn ";
  82. $is_numeric=false;
  83. switch(strtolower($ft))
  84. {
  85. case "int":
  86. $sql.="int";
  87. $is_numeric=true;
  88. break;
  89. case "blob":
  90. $sql.="text";
  91. $is_numeric=false;
  92. break;
  93. case "real":
  94. $sql.="real";
  95. $is_numeric=true;
  96. break;
  97. case "string":
  98. $sql.="char($fs)";
  99. $is_numeric=false;
  100. break;
  101. case "unknown":
  102. switch(intval($fs))
  103. {
  104. case 4: //little weakness here...there is no way (thru the PHP/MySQL interface) to tell the difference between a tinyint and a year field type
  105. $sql.="tinyint";
  106. $is_numeric=true;
  107. break;
  108. default: //we could get a little more optimzation here! (i.e. check for medium ints, etc.)
  109. $sql.="int";
  110. $is_numeric=true;
  111. break;
  112. }
  113. break;
  114. case "timestamp":
  115. $sql.="timestamp";
  116. $is_numeric=true;
  117. break;
  118. case "date":
  119. $sql.="date";
  120. $is_numeric=false;
  121. break;
  122. case "datetime":
  123. $sql.="datetime";
  124. $is_numeric=false;
  125. break;
  126. case "time":
  127. $sql.="time";
  128. $is_numeric=false;
  129. break;
  130. default: //future support for field types that are not recognized (hopefully this will work without need for future modification)
  131. $sql.=$ft;
  132. $is_numeric=true; //I'm assuming new field types will follow SQL numeric syntax..this is where this support will breakdown
  133. break;
  134. }
  135. //VERY, VERY IMPORTANT!!! Don't forget to append the flags onto the end of the field creator
  136. if (strpos($ff,"unsigned")!=false)
  137. {
  138. //timestamps are a little screwy so we test for them
  139. if ($ft!="timestamp") $sql.=" unsigned";
  140. }
  141. if (strpos($ff,"zerofill")!=false)
  142. {
  143. //timestamps are a little screwy so we test for them
  144. if ($ft!="timestamp") $sql.=" zerofill";
  145. }
  146. if (strpos($ff,"auto_increment")!=false) $sql.=" auto_increment";
  147. if (strpos($ff,"not_null")!=false) $sql.=" not null";
  148. if (strpos($ff,"primary_key")!=false) $sql.=" primary key";
  149. //End of field flags
  150. if ($b<$nf-1)
  151. {
  152. $sql.=",\n";
  153. $fl.=$fn.", ";
  154. }
  155. else
  156. {
  157. $sql.="\n);\n\n";
  158. $fl.=$fn;
  159. }
  160. //we need some of the info generated in this loop later in the algorythm...save what we need to arrays
  161. $fna[$b]=$fn;
  162. $ina[$b]=$is_numeric;
  163. }
  164. //$this->_Out($sql);
  165. if ($this->structure_only!=true)
  166. {
  167. //parse out the table's data and generate the SQL INSERT statements in order to replicate the data itself...
  168. for ($c=0;$c<$nr;$c++)
  169. {
  170. $sql="insert into $tablename ($fl) values (";
  171. $row=mysql_fetch_row($res2);
  172. for ($d=0;$d<$nf;$d++)
  173. {
  174. $data=strval($row[$d]);
  175. if ($ina[$d]==true)
  176. $sql.= intval($data);
  177. else
  178. $sql.="\"".mysql_escape_string($data)."\"";
  179. if ($d<($nf-1)) $sql.=", ";
  180. }
  181. $sql.=");\n";
  182. //echo $sql;
  183. $this->_Out($sql);
  184. }
  185. $this->_Out("\n\n");
  186. }
  187. mysql_free_result($res2);
  188. }
  189. if ($this->fptr!=false) fclose($this->fptr);
  190. if ($this->structure_only==true)
  191. {
  192. $fname = $output;
  193. if (!mysql_connect($host, $uid, $pwd)) {
  194. echo 'Could not connect to mysql';
  195. exit;
  196. }
  197. $sql = "SHOW TABLES FROM $dbname";
  198. $result = mysql_query($sql);
  199. if (!$result) {
  200. echo "DB Error, could not list tables\n";
  201. echo 'MySQL Error: ' . mysql_error();
  202. exit;
  203. }
  204. $fp = fopen($fname, "w");
  205. while ($row = mysql_fetch_row($result)) {
  206. $result2 = mysql_query("SHOW CREATE TABLE `".$row[0]."`");
  207. $row2 = mysql_fetch_row($result2);
  208. fwrite($fp, "DROP TABLE IF EXISTS `".$row[0]."`;\n\n");
  209. fwrite($fp, $row2[1] . ";\n\n");
  210. }
  211. fclose($fp);
  212. mysql_free_result($result2);
  213. mysql_free_result($result);
  214. }
  215. return 0;
  216. }
  217. //-------------------------------------------
  218. //---- This will Open sqldata.txt.
  219. //--------------------------------
  220. function _Open()
  221. {
  222. $filename = $this->output;
  223. $fp = fopen( $filename, "r" ) or die("Couldn't open $filename");
  224. while ( ! feof( $fp ) )
  225. {
  226. $line = fgets( $fp, 1024 );
  227. $SQL .= "$line";
  228. }
  229. return $SQL;
  230. }
  231. //--------------------------------
  232. //---- This will Restore data in sqldata.txt
  233. //------------------------------------------
  234. function Restore($sep=";")
  235. {
  236. //---- Calls _Open function to make an array of sql peices.
  237. //---------------------------------------------------------
  238. $SQL = explode($sep, $this->_Open($this->output));
  239. //---------------------------------------------------------
  240. //---- Make connection to MySql and the specified db.
  241. //---------------------------------------------------
  242. $link = mysql_connect($this->host, $this->user, $this->pass) or (die (mysql_error()));
  243. mysql_select_db($this->db, $link) or (die (mysql_error()));
  244. //---------------------------------------------------
  245. //---- This loop will execute every peice of SQL.
  246. //-----------------------------------------------
  247. for ($i=0;$i<count($SQL)-1;$i++)
  248. {
  249. //echo $SQL[$i] . $sep . '<BR>';
  250. mysql_unbuffered_query($SQL[$i] . $sep) or (die (mysql_error()));
  251. }
  252. //-----------------------------------------------
  253. }
  254. //---------------------------------------------------------
  255. //---- This will put data in sqldata.txt
  256. //------------------------------------------
  257. function _out($s)
  258. {
  259. if ($this->fptr==false) echo("$s"); else fputs($this->fptr,$s);
  260. }
  261. //------------------------------------------
  262. //---- This is the function to be called for backup.
  263. //--------------------------------------------------
  264. function Backup()
  265. {
  266. $this->_Mysqlbackup($this->host,$this->db,$this->user,$this->pass,$this->output,$this->structure_only);
  267. return 1;
  268. }
  269. //--------------------------------------------------
  270. }
  271. ?>