├── LICENSE ├── README.md └── SQLDumper.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Yehuda Eisenberg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP-Sql-Dumper 2 | SQL dumper in pure php (no shell command!) 3 | 4 | ## Use: 5 | ````php 6 | dump(); //dump all databases 16 | $success = $dumper->dump(['db1', 'db2', 'otherDB']); //dump specific databases 17 | ```` 18 | 19 | ## Example 20 | * [Daily backup to telegram (github gist)](https://gist.github.com/YehudaEi/ead01d7993f72575535309486efe822d) 21 | 22 | dump all databases: 23 | ````php 24 | $dumper = new SQLDumper("localhost", "root", "pa$$w0rd", "SqlDump.sql"); 25 | $success = $dumper->dump(); 26 | if($success) echo "The dump has been created"; 27 | ```` 28 | 29 | dump specific databases: 30 | ````php 31 | $dumper = new SQLDumper("localhost", "root", "pa$$w0rd", "SqlDump.sql"); 32 | $success = $dumper->dump(['db1', 'db2', 'otherDB']); 33 | if($success) echo "The dump has been created"; 34 | ```` 35 | 36 | ## Contact 37 | [sqldumper@yehudae.net](mailto:sqldumper@yehudae.net) 38 | 39 | ## License 40 | MIT 41 | -------------------------------------------------------------------------------- /SQLDumper.php: -------------------------------------------------------------------------------- 1 | DBConn = new mysqli($host, $user, $pass); 55 | if($this->DBConn == false || empty($this->DBConn) || $this->DBConn->connect_error){ 56 | http_response_code(500); 57 | die('Error connecting to the DB'); 58 | } 59 | $this->DBConn->set_charset("utf8mb4"); 60 | 61 | $this->outFile = $outFile; 62 | $this->write("", NULL); 63 | } 64 | 65 | /** 66 | * Dumping function 67 | * 68 | * @param array $onlyDump (optional) dump only this Databases 69 | * 70 | * @return bool or void (true if success or void if faile) 71 | */ 72 | public function dump($onlyDump = array()){ 73 | $this->onlyDump = $onlyDump; 74 | $DBs = $this->DBConn->query("SHOW DATABASES;"); 75 | 76 | $databases = array(); 77 | 78 | while ($row = $DBs->fetch_assoc()){ 79 | if(!in_array($row['Database'], $this->nonDump) && empty($this->onlyDump)){ 80 | $tables = $this->DBConn->query("SHOW TABLES FROM `{$row['Database']}`;")->fetch_all(); 81 | $databases[$row['Database']] = $tables; 82 | } 83 | else if(!empty($this->onlyDump) && in_array($row['Database'], $this->onlyDump)){ 84 | $tables = $this->DBConn->query("SHOW TABLES FROM `{$row['Database']}`;")->fetch_all(); 85 | $databases[$row['Database']] = $tables; 86 | } 87 | } 88 | 89 | $this->write("-- -------------------------------------------------- --"); 90 | $this->write("-- --"); 91 | $this->write("-- Created by PHP-Sql-Dumper by Yehuda Eisenberg. --"); 92 | $this->write("-- Repo: https://github.com/YehudaEi/PHP-Sql-Dumper --"); 93 | $this->write("-- --"); 94 | $this->write("-- -------------------------------------------------- --"); 95 | $this->write(""); 96 | $this->write("-- Generation time: " . date('r')); 97 | $this->write("-- Host: " . $this->DBConn->host_info); 98 | $this->write("/*!40030 SET NAMES UTF8 */;"); 99 | $this->write("/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;"); 100 | $this->write("/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;"); 101 | $this->write("/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;"); 102 | $this->write("/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;"); 103 | $this->write("/*!40103 SET TIME_ZONE='" . date('P') . "' */;"); 104 | $this->write("/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;"); 105 | $this->write("/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;"); 106 | $this->write("/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;"); 107 | $this->write("/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;"); 108 | $this->write(""); 109 | 110 | 111 | foreach ($databases as $name => $tables){ 112 | $this->DBConn->select_db($name); 113 | $this->write("-- "); 114 | $this->write("-- Database: " . $name); 115 | $this->write("-- "); 116 | $this->write(""); 117 | $this->write($this->DBConn->query("SHOW CREATE DATABASE `{$name}`")->fetch_assoc()['Create Database'] . ";"); 118 | $this->write(""); 119 | $this->write("USE `{$name}`;"); 120 | $this->write(""); 121 | 122 | foreach ($tables as $table){ 123 | $table = $table[0]; 124 | $this->write("-- "); 125 | $this->write("-- Database: " . $name . ", Structure Table: " . $table); 126 | $this->write("-- "); 127 | $this->write(""); 128 | 129 | $this->write("DROP TABLE IF EXISTS `{$table}`;"); 130 | $res = $this->DBConn->query("SHOW CREATE TABLE `{$table}`")->fetch_assoc(); 131 | $this->write($res['Create Table'] . ";"); 132 | $this->write(""); 133 | $this->write(""); 134 | 135 | $this->write("-- "); 136 | $this->write("-- Database: " . $name . ", Data Table: " . $table); 137 | $this->write("-- "); 138 | $this->write(""); 139 | 140 | $this->write("LOCK TABLES `{$table}` WRITE;"); 141 | $data = $this->DBConn->query("SELECT * FROM `{$table}`;"); 142 | 143 | $i = 0; 144 | $tmpRows = array(); 145 | while ($row = $data->fetch_assoc()){ 146 | $rowData = array(); 147 | foreach ($row as $dat){ 148 | if($dat == null) $dat = "NULL"; 149 | $rowData[] = "'" . $this->DBConn->real_escape_string($dat) . "'"; 150 | } 151 | $tmpRows[] = '(' . implode(",", $rowData) . ')'; 152 | $i++; 153 | 154 | if($i == 20){ 155 | $this->write("INSERT INTO `{$table}` VALUES \r\n" . implode(",\n", $tmpRows) . ';'); 156 | $this->write(""); 157 | $i = 0; 158 | $tmpRows = array(); 159 | } 160 | } 161 | if(count($tmpRows) > 0){ 162 | $this->write("INSERT INTO `{$table}` VALUES \r\n" . implode(",\n", $tmpRows) . ';'); 163 | } 164 | $this->write("UNLOCK TABLES;"); 165 | 166 | $this->write(""); 167 | $this->write(""); 168 | } 169 | } 170 | 171 | 172 | $this->write("/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;"); 173 | $this->write("/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;"); 174 | $this->write("/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;"); 175 | $this->write("/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;"); 176 | $this->write("/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;"); 177 | $this->write("/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;"); 178 | $this->write("/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;"); 179 | 180 | return true; 181 | } 182 | 183 | /** 184 | * Write to file 185 | * 186 | * @param string $str string to write 187 | * @param string $flag file_put_contents flag 188 | * 189 | * @return void 190 | */ 191 | private function write($str, $flag = FILE_APPEND){ 192 | file_put_contents($this->outFile, $str . "\r\n", $flag); 193 | } 194 | } 195 | 196 | ?> 197 | --------------------------------------------------------------------------------