<?php
//error_reporting(0);
function db_dump($host, $user, $pwd, $db) {
$con = mysqli_connect($host, $user, $pwd, $db);
if (!$con) exit(sprintf('No MySQL connection: %s', mysqli_connect_error()));
$tabelstobackup = Array();
$result = mysqli_query($con, "SHOW TABLES FROM `$db`");
if (!$result) exit(sprintf('Database error %1$s for query %2$s', mysqli_error(), "SHOW TABLE STATUS FROM `$db`;"));
while ($data = mysqli_fetch_row($result)) {
$tabelstobackup[] = $data[0];
}
if (count($tabelstobackup) > 0) {
$result = mysqli_query($con, "SHOW TABLE STATUS FROM `$db`");
if (!$result) exit(sprintf('Database error %1$s for query %2$s', mysqli_error(), "SHOW TABLE STATUS FROM `$db`;"));
$status= Array();
while ($data = mysqli_fetch_assoc($result)) {
$status[$data['Name']] = $data;
}
if ($file = fopen("$db.sql", 'wb')) {
fwrite($file, "-- ---------------------------------------------------------\n");
fwrite($file, "-- Database Name: $db\n");
fwrite($file, "-- ---------------------------------------------------------\n\n");
fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n");
fwrite($file, "/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n");
fwrite($file, "/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n");
fwrite($file, "/*!40101 SET NAMES '".mysqli_character_set_name($con)."' */;\n");
fwrite($file, "/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;\n");
fwrite($file, "/*!40103 SET TIME_ZONE='".mysqli_fetch_array(mysqli_query($con, "SELECT @@time_zone"))[0]."' */;\n");
fwrite($file, "/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;\n");
fwrite($file, "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;\n");
fwrite($file, "/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;\n");
fwrite($file, "/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;\n\n");
foreach($tabelstobackup as $table) {
echo sprintf('Dump database table "%s"',$table) . "<br />";
need_free_memory(($status[$table]['Data_length']+$status[$table]['Index_length'])*3);
_db_dump_table($con, $table, $status[$table], $file);
}
}
}
}
function _db_dump_table($con, $table, $status, $file) {
fwrite($file, "\n");
fwrite($file, "--\n");
fwrite($file, "-- Table structure for table $table\n");
fwrite($file, "--\n\n");
fwrite($file, "DROP TABLE IF EXISTS `" . $table . "`;\n");
fwrite($file, "/*!40101 SET @saved_cs_client = @@character_set_client */;\n");
fwrite($file, "/*!40101 SET character_set_client = '".$status["Collation"]."' */;\n");
$result = mysqli_query($con, "SHOW CREATE TABLE `".$table."`");
if (!$result) {
echo sprintf('Database error %1$s for query %2$s', mysqli_error(), "SHOW CREATE TABLE `".$table."`")."\n";
return false;
}
$tablestruc = mysqli_fetch_assoc($result);
fwrite($file, $tablestruc['Create Table'].";\n");
fwrite($file, "/*!40101 SET character_set_client = @saved_cs_client */;\n");
$result = mysqli_query($con, "SELECT * FROM `".$table."`");
if (!$result) {
echo sprintf('Database error %1$s for query %2$s', mysqli_error(), "SELECT * FROM `".$table."`")."<br />";
return false;
}
fwrite($file, "--\n");
fwrite($file, "-- Dumping data for table $table\n");
fwrite($file, "--\n\n");
if ($status['Engine']=='MyISAM') fwrite($file, "/*!40000 ALTER TABLE `".$table."` DISABLE KEYS */;\n");
while ($data = mysqli_fetch_assoc($result)) {
$keys = array();
$values = array();
foreach($data as $key => $value) {
if ($value === NULL) $value = "NULL";
elseif ($value === "" or $value === false) $value = "''";
elseif (!is_numeric($value)) $value = "'".mysqli_real_escape_string($con, $value)."'";
$values[] = $value;
}
fwrite($file, "INSERT INTO `".$table."` VALUES ( ".implode(", ",$values)." );\n");
}
if ($status['Engine']=='MyISAM') fwrite($file, "/*!40000 ALTER TABLE ".$table." ENABLE KEYS */;\n");
}
function need_free_memory($memneed) {
if (!function_exists('memory_get_usage')) return;
$needmemory = @memory_get_usage(true)+inbytes($memneed);
if ($needmemory>inbytes(ini_get('memory_limit'))) {
$newmemory=round($needmemory/1024/1024)+1 .'M';
if ($needmemory>=1073741824) $newmemory = round($needmemory/1024/1024/1024) .'G';
if ($oldmem=@ini_set('memory_limit', $newmemory)) echo sprintf(__('Memory increased from %1$s to %2$s','backwpup'),$oldmem,@ini_get('memory_limit'))."<br />";
else echo sprintf(__('Can not increase memory limit is %1$s','backwpup'),@ini_get('memory_limit'))."<br />";
}
}
function inbytes($value) {
$multi = strtoupper(substr(trim($value),-1));
$bytes = abs(intval(trim($value)));
if ($multi=='G') $bytes = $bytes*1024*1024*1024;
if ($multi=='M') $bytes=$bytes*1024*1024;
if ($multi=='K') $bytes=$bytes*1024;
return $bytes;
}
db_dump('localhost', 'root', '1234', 'youyoubao');
?>