PageRenderTime 28ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/backup_Restore/prueba.php

https://gitlab.com/gustCL/syscar
PHP | 268 lines | 172 code | 43 blank | 53 comment | 15 complexity | eb98a331931d6ecf9dd90891707fce2c MD5 | raw file
  1. <?php
  2. /**
  3. * This file contains the Backup_Database class wich performs
  4. * a partial or complete backup of any given MySQL database
  5. * @author Daniel López Azaña <http://www.daniloaz.com-->
  6. * @version 1.0
  7. */
  8. // Report all errors
  9. error_reporting(E_ALL);
  10. /**
  11. * Define database parameters here
  12. */
  13. define("DB_USER", 'admin');
  14. define("DB_PASSWORD", '');
  15. define("DB_NAME", 'db_farmacia');
  16. define("DB_HOST", 'localhost');
  17. define("OUTPUT_DIR", 'cache');
  18. define("TABLES", '*');
  19. /**
  20. * Instantiate Backup_Database and perform backup
  21. */
  22. $backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
  23. $status = $backupDatabase->backupTables(TABLES, OUTPUT_DIR) ? 'OK' : 'KO';
  24. echo "
  25. Backup result: ".$status;
  26. /**
  27. * The Backup_Database class
  28. */
  29. class Backup_Database {
  30. /**
  31. * Host where database is located
  32. */
  33. var $host = 'localhost';
  34. /**
  35. * Username used to connect to database
  36. */
  37. var $username = '';
  38. /**
  39. * Password used to connect to database
  40. */
  41. var $passwd = '';
  42. /**
  43. * Database to backup
  44. */
  45. var $dbName = 'db_farmacia';
  46. /**
  47. * Database charset
  48. */
  49. var $charset = 'utf8';
  50. /**
  51. * Constructor initializes database
  52. */
  53. function Backup_Database($host, $username, $passwd, $dbName, $charset = 'utf8')
  54. {
  55. $this->host = $host;
  56. $this->username = $username;
  57. $this->passwd = $passwd;
  58. $this->dbName = $dbName;
  59. $this->charset = $charset;
  60. $this->initializeDatabase();
  61. }
  62. protected function initializeDatabase()
  63. {
  64. $conn = mysql_connect($this->host, $this->username, $this->passwd);
  65. mysql_select_db($this->dbName, $conn);
  66. if (! mysql_set_charset ($this->charset, $conn))
  67. {
  68. mysql_query('SET NAMES'.$this->charset);
  69. }
  70. }
  71. public function backupTables($tables = '*', $outputDir = '.')
  72. {
  73. try
  74. {
  75. /**
  76. * Tables to export
  77. */
  78. if($tables == '*')
  79. {
  80. $tables = array();
  81. $result = mysql_query('SHOW TABLES');
  82. while($row = mysql_fetch_row($result))
  83. {
  84. $tables[] = $row[0];
  85. }
  86. }
  87. else
  88. {
  89. $tables = is_array($tables) ? $tables : explode(',',$tables);
  90. }
  91. $sql = 'CREATE DATABASE IF NOT EXISTS '.$this->dbName.";\n\n";
  92. $sql .= 'USE '.$this->dbName.";\n\n";
  93. /**
  94. * Iterate tables
  95. */
  96. foreach($tables as $table)
  97. {
  98. echo "Backing up ".$table." table...";
  99. $result = mysql_query('SELECT * FROM '.$table);
  100. $numFields = mysql_num_fields($result);
  101. $sql .= 'DROP TABLE IF EXISTS '.$table.';';
  102. $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
  103. $sql.= "\n\n".$row2[1].";\n\n";
  104. for ($i = 0; $i < $numFields; $i++)
  105. {
  106. while($row = mysql_fetch_row($result))
  107. {
  108. $sql .= 'INSERT INTO '.$table.' VALUES(';
  109. for($j=0; $j<$numFields; $j++)
  110. {
  111. $row[$j] = addslashes($row[$j]);
  112. $row[$j] = ereg_replace("\n","\\n",$row[$j]);
  113. if (isset($row[$j]))
  114. {
  115. $sql .= '"'.$row[$j].'"' ;
  116. }
  117. else
  118. {
  119. $sql.= '""';
  120. }
  121. if ($j < ($numFields-1))
  122. {
  123. $sql .= ',';
  124. }
  125. }
  126. $sql.= ");\n";
  127. }
  128. }
  129. $sql.="\n\n\n";
  130. echo " OK" . "
  131. ";
  132. }
  133. }
  134. catch (Exception $e)
  135. {
  136. var_dump($e->getMessage());
  137. return false;
  138. }
  139. return $this->saveFile($sql, $outputDir);
  140. }
  141. /**
  142. * Save SQL to file
  143. * @param string $sql
  144. */
  145. protected function saveFile(&$sql, $outputDir = '.')
  146. {
  147. if (!$sql) return false;
  148. try
  149. {
  150. $handle = fopen($outputDir.'/db-backup-'.$this->dbName.'-'.date("Ymd-His", time()).'.sql','w+');
  151. fwrite($handle, $sql);
  152. fclose($handle);
  153. }
  154. catch (Exception $e)
  155. {
  156. var_dump($e->getMessage());
  157. return false;
  158. }
  159. return true;
  160. }
  161. }
  162. ?>
  163. <?php
  164. require_once './conexion.php';
  165. $usuario='root';
  166. $password='';
  167. $db='db_farmacia';
  168. backup_tables('localhost',$usuario,$password,$db);
  169. $return="";
  170. /* backup the db OR just a table */
  171. //En la variable $talbes puedes agregar las tablas especificas separadas por comas:
  172. //profesor,estudiante,clase
  173. //O déjalo con el asterisco '*' para que se respalde toda la base de datos
  174. function backup_tables($host,$user,$pass,$name,$tables = '*')
  175. {
  176. //$link = mysql_connect($host,$user,$pass);
  177. //mysql_select_db($name,$link);
  178. //get all of the tables
  179. if($tables == '*')
  180. {
  181. $tables = array();
  182. $result = mysql_query('SHOW TABLES');
  183. while($row = mysql_fetch_row($result))
  184. {
  185. $tables[] = $row[0];
  186. }
  187. }
  188. else
  189. {
  190. $tables = is_array($tables) ? $tables : explode(',',$tables);
  191. }
  192. //cycle through
  193. foreach($tables as $table)
  194. {
  195. $result = mysql_query('SELECT * FROM '.$table);
  196. $num_fields = mysql_num_fields($result);
  197. $return.= 'DROP TABLE '.$table.';';
  198. $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
  199. $return.= "\n\n".$row2[1].";\n\n";
  200. for ($i = 0; $i < $num_fields; $i++)
  201. {
  202. while($row = mysql_fetch_row($result))
  203. {
  204. $return.= 'INSERT INTO '.$table.' VALUES(';
  205. for($j=0; $j<$num_fields; $j++)
  206. {
  207. $row[$j] = addslashes($row[$j]);
  208. $row[$j] = ereg_replace("\n","\\n",$row[$j]);
  209. if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
  210. if ($j<($num_fields-1)) { $return.= ','; }
  211. }
  212. $return.= ");\n";
  213. }
  214. }
  215. $return.="\n\n\n";
  216. }
  217. //save file
  218. $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
  219. fwrite($handle,$return);
  220. fclose($handle);
  221. }
  222. echo "<script language='JavaScript'> alert('La copia de seguridad se creo correctamente..!! ');</script>";
  223. sleep(1);
  224. header("location: backup_Restore.php");
  225. ?>