· 8 years ago · Feb 11, 2018, 08:42 PM
1<?php
2$host = "XXXXXXXX";
3$user = "XXXXXXXX";
4$pass = "XXXXXXXX";
5$name ="XXXXXXXX";
6function EXPORT_TABLES($host,$user,$pass,$name, $tables=false, $backup_name=false){
7 set_time_limit(3000); $mysqli = new mysqli($host,$user,$pass,$name);
8 $mysqli->select_db($name); $mysqli->query("SET NAMES 'utf8'");
9 $queryTables = $mysqli->query('SHOW TABLES');
10 while($row = $queryTables->fetch_row()) {
11 $target_tables[] = $row[0];
12 }
13 if($tables !== false) {
14 $target_tables = array_intersect( $target_tables, $tables);
15 }
16 $content = "SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";rnSET time_zone = "+00:00";rnrnrn/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;rn/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;rn/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;rn/*!40101 SET NAMES utf8 */;rn--rn-- Database: `".$name."`rn--rnrnrn";
17 foreach($target_tables as $table){
18 if (empty($table)){ continue; }
19 $result = $mysqli->query('SELECT * FROM `'.$table.'`');
20 $fields_amount=$result->field_count;
21 $rows_num=$mysqli->affected_rows;
22 $res = $mysqli->query('SHOW CREATE TABLE '.$table);
23 $TableMLine=$res->fetch_row();
24 $content .= "nn".$TableMLine[1].";nn";
25 $TableMLine[1]=str_ireplace('CREATE TABLE `','CREATE TABLE IF NOT EXISTS `',$TableMLine[1]);
26 for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0) {
27 while($row = $result->fetch_row()) { //when started (and every after 100 command cycle):
28 if ($st_counter%100 == 0 || $st_counter == 0 ) {$content .= "nINSERT INTO ".$table." VALUES";}
29 $content .= "n(";
30 for($j=0; $j<$fields_amount; $j++){
31 $row[$j] = str_replace("n","\n", addslashes($row[$j]) );
32 if (isset($row[$j])){$content .= '"'.$row[$j].'"' ;}
33 else{$content .= '""';}
34 if ($j<($fields_amount-1)){$content.= ',';}
35 }
36 $content .=")";
37 //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
38 if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) {$content .= ";";}
39 else {$content .= ",";} $st_counter=$st_counter+1;
40 }
41 }
42 $content .="nnn";
43 }
44 $content .= "rnrn/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;rn/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;rn/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;";
45 $backup_name = $backup_name ? $backup_name : $name.'___('.date('H-i-s').'_'.date('d-m-Y').').sql';
46 ob_get_clean();
47 header('Content-Type: application/octet-stream');
48 header("Content-Transfer-Encoding: Binary");
49 header('Content-Length: '. (function_exists('mb_strlen') ? mb_strlen($content, '8bit'): strlen($content)) );
50 header("Content-disposition: attachment; filename="".$backup_name.""");
51 echo $content; exit;
52} //see import.php too
53EXPORT_TABLES($host,$user,$pass,$name)
54?>