Update SQLDumper

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Update SQLDumper

      Aquí les dejo los ficheros para los que lo necesiten

      PHP Source Code: SQLDumper.class.php

      1. <?php
      2. class SQLDumper
      3. {
      4. private $host;
      5. private $user;
      6. private $pass;
      7. private $name;
      8. private $timeZone = '+00:00';
      9. private $mySqli;
      10. private $sqlString = "";
      11. private $arrayTables;
      12. private $tableFieldCount = 0;
      13. private $tableNumberOfRows = 0;
      14. private $queryResult;
      15. public function __construct(array $arrayConnConfig)
      16. {
      17. if (
      18. (!isset($arrayConnConfig['host'])) ||
      19. (!isset($arrayConnConfig['user'])) ||
      20. (!isset($arrayConnConfig['userpw'])) ||
      21. (!isset($arrayConnConfig['databasename']))
      22. ) {
      23. throw new Exception('Missing connection data.');
      24. }
      25. $this->setHost($arrayConnConfig['host']);
      26. $this->setUser($arrayConnConfig['user']);
      27. $this->setPass($arrayConnConfig['userpw']);
      28. $this->setName($arrayConnConfig['databasename']);
      29. }
      30. public function backUp()
      31. {
      32. $this->connectMySql();
      33. $this->getTables();
      34. $this->generateSqlHeader();
      35. $this->createTableStaments();
      36. $this->insertStaments();
      37. $this->generateSqlFooter();
      38. }
      39. private function setHost($host)
      40. {
      41. $this->host = $host;
      42. }
      43. private function setUser($user)
      44. {
      45. $this->user = $user;
      46. }
      47. private function setPass($password)
      48. {
      49. $this->pass = $password;
      50. }
      51. private function setName($name)
      52. {
      53. $this->name = $name;
      54. }
      55. public function setFileName($name)
      56. {
      57. $this->fileName = $name;
      58. }
      59. public function setFileDir($dir)
      60. {
      61. $this->fileDir = $dir;
      62. }
      63. private function connectMySql()
      64. {
      65. $this->mySqli = new mysqli($this->host, $this->user, $this->pass, $this->name);
      66. $this->mySqli->select_db($this->name);
      67. $this->mySqli->query("SET NAMES 'utf8'");
      68. }
      69. private function getTables()
      70. {
      71. $queryTables = $this->mySqli->query('SHOW TABLES');
      72. while($row = $queryTables->fetch_row()) {
      73. $this->arrayTables[] = $row[0];
      74. }
      75. }
      76. private function generateSqlHeader()
      77. {
      78. $this->sqlString = 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";' . "\r\n";
      79. $this->sqlString .= 'SET time_zone = "' . $this->timeZone . '";' . "\r\n\r\n\r\n";
      80. $this->sqlString .= '/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;' . "\r\n";
      81. $this->sqlString .= '/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;' . "\r\n";
      82. $this->sqlString .= '/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;' . "\r\n";
      83. $this->sqlString .= '/*!40101 SET NAMES utf8 */;' . "\r\n";
      84. $this->sqlString .= '--' . "\r\n";
      85. $this->sqlString .= '-- Database: `' . $this->name . '`' . "\r\n";
      86. $this->sqlString .= '--' . "\r\n\r\n\r\n";
      87. return;
      88. }
      89. private function createTableStaments()
      90. {
      91. foreach($this->arrayTables as $table){
      92. $this->sqlCreateTableStament($table);
      93. }
      94. }
      95. private function sqlCreateTableStament($table)
      96. {
      97. $res = $this->mySqli->query('SHOW CREATE TABLE '.$table);
      98. $temp = $res->fetch_row();
      99. $this->sqlString .= "\n\n" . str_ireplace('CREATE TABLE `','CREATE TABLE IF NOT EXISTS `', $temp[1]) . ";\n\n";
      100. }
      101. private function insertStaments()
      102. {
      103. foreach($this->arrayTables as $table){
      104. $this->sqlInsertStaments($table);
      105. }
      106. }
      107. private function sqlInsertStaments($table)
      108. {
      109. $this->getTableData($table);
      110. if($this->tableFieldCount == 0) {
      111. return;
      112. }
      113. $i = 0;
      114. while($row = $this->queryResult->fetch_row()) {
      115. $this->insertResultHeader($table, $i);
      116. $this->insertSingleResult($row);
      117. $i++;
      118. $this->sqlString .= (($i != 0) && ($i % 100 == 0) || ($i == $this->tableNumberOfRows)) ? ";" : ",";
      119. }
      120. $this->sqlString .= "\n\n\n";
      121. }
      122. private function getTableData($table)
      123. {
      124. $this->queryResult = $this->mySqli->query('SELECT * FROM `' . $table . '`');
      125. $this->tableFieldCount = $this->queryResult->field_count;
      126. $this->tableNumberOfRows = $this->mySqli->affected_rows;
      127. }
      128. private function insertResultHeader($table, $currentRowNumber)
      129. {
      130. if ($currentRowNumber % 100 == 0 || $currentRowNumber == 0 ) {
      131. $this->sqlString .= "\nINSERT INTO " . $table . " VALUES";
      132. }
      133. }
      134. private function insertSingleResult($row)
      135. {
      136. $this->sqlString .= "\n(";
      137. for($i = 0; $i < $this->tableFieldCount; $i++){
      138. $row[$i] = str_replace("\n","\\n", addslashes($row[$i]));
      139. $this->sqlString .= (isset($row[$i])) ? '"'.$row[$i].'"' : '""';
      140. if($i < ($this->tableFieldCount-1)){
      141. $this->sqlString.= ',';
      142. }
      143. }
      144. $this->sqlString .=")";
      145. }
      146. private function generateSqlFooter()
      147. {
      148. $this->sqlString .= "\r\n\r\n";
      149. $this->sqlString .= '/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;';
      150. $this->sqlString .= "\r\n";
      151. $this->sqlString .= '/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;';
      152. $this->sqlString .= "\r\n";
      153. $this->sqlString .= '/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;';
      154. }
      155. public function saveToFile()
      156. {
      157. if (!$backupFile = fopen($this->fileDir . $this->fileName, "w+")) {
      158. throw new Exception('Imposible to create file.');
      159. }
      160. fwrite($backupFile, $this->sqlString);
      161. fclose($backupFile);
      162. }
      163. }
      164. ?>
      Display All


      PHP Source Code: DumpCronjob.class.php

      1. <?php
      2. require_once 'includes/classes/cronjob/CronjobTask.interface.php';
      3. class DumpCronjob implements CronjobTask
      4. {
      5. function run()
      6. {
      7. require 'includes/classes/SQLDumper.class.php';
      8. require 'includes/config.php';
      9. try {
      10. $bck = new SQLDumper($database);
      11. $bck->backUp();
      12. $bck->setFileDir('./includes/backups/');
      13. $bck->setFileName('2MoonsBackup_'.date('d_m_Y_H_i_s', TIMESTAMP).'.sql');
      14. $bck->saveToFile();
      15. }
      16. catch(Exception $e) {
      17. echo $e;
      18. }
      19. }
      20. }
      Display All
      Files
      • SQLDumper.zip

        (2.24 kB, downloaded 218 times, last: )
    • Ala wrote:

      No es el original este es mucho mas simple
      Хорошо, я посмотрю, когда буду дома на сколько этот код лучше) надеюсь, что он очень классный и оптмизированный.
    • Aquí les dejo los archivos corregidos para poder hacer el respaldo de la base de datos desde el panel de Admin. Esto es solo para el caso del New Star
      Files

      The post was edited 1 time, last by Linkin ().

    • Все отлично, код удобен, все работает, но из-за вызова этой функции дается второе имя. Лично я просто закомментировал ее вызов и на это проблема исчезла (проверено на New-Star).
      Images
      • Без названия.png

        346.8 kB, 1,597×746, viewed 197 times
    • Но грамотное решение здесь другое.
      По коду понятно стало, что $filePath - отвечает за само нахождение папки, а вот $fileName за его название.
      Но работа все равно отличная и достойна внимания!
      Images
      • Без названия (1).png

        219.7 kB, 1,578×756, viewed 208 times