first commit
This commit is contained in:
209
libraries/MySQLDump.php
Normal file
209
libraries/MySQLDump.php
Normal file
@@ -0,0 +1,209 @@
|
||||
<?php
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* MySQL database dump.
|
||||
|
||||
*
|
||||
|
||||
* @author David Grudl (http://davidgrudl.com)
|
||||
|
||||
* @copyright Copyright (c) 2008 David Grudl
|
||||
|
||||
* @license New BSD License
|
||||
|
||||
* @version 1.0
|
||||
|
||||
*/
|
||||
|
||||
class MySQLDump
|
||||
|
||||
{
|
||||
|
||||
const MAX_SQL_SIZE = 1e6;
|
||||
|
||||
|
||||
|
||||
const NONE = 0;
|
||||
|
||||
const DROP = 1;
|
||||
|
||||
const CREATE = 2;
|
||||
|
||||
const DATA = 4;
|
||||
|
||||
const TRIGGERS = 8;
|
||||
|
||||
const ALL = 15; // DROP | CREATE | DATA | TRIGGERS
|
||||
|
||||
|
||||
|
||||
/** @var array */
|
||||
|
||||
public $tables = array(
|
||||
|
||||
'*' => self::ALL,
|
||||
|
||||
);
|
||||
|
||||
|
||||
|
||||
/** @var mysqli */
|
||||
|
||||
private $connection;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Connects to database.
|
||||
|
||||
* @param mysqli connection
|
||||
|
||||
*/
|
||||
|
||||
public function __construct(mysqli $connection, $charset = 'utf8')
|
||||
|
||||
{
|
||||
|
||||
$this->connection = $connection;
|
||||
|
||||
|
||||
|
||||
if ($connection->connect_errno) {
|
||||
|
||||
throw new Exception($connection->connect_error);
|
||||
|
||||
|
||||
|
||||
} elseif (!$connection->set_charset($charset)) { // was added in MySQL 5.0.7 and PHP 5.0.5, fixed in PHP 5.1.5)
|
||||
|
||||
throw new Exception($connection->error);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Saves dump to the file.
|
||||
|
||||
* @param string filename
|
||||
|
||||
* @return void
|
||||
|
||||
*/
|
||||
|
||||
public function save($file)
|
||||
|
||||
{
|
||||
|
||||
$handle = strcasecmp(substr($file, -3), '.gz') ? fopen($file, 'wb') : gzopen($file, 'wb');
|
||||
|
||||
if (!$handle) {
|
||||
|
||||
throw new Exception("ERROR: Cannot write file '$file'.");
|
||||
|
||||
}
|
||||
|
||||
$this->write($handle);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
||||
* Writes dump to logical file.
|
||||
|
||||
* @param resource
|
||||
|
||||
* @return void
|
||||
|
||||
*/
|
||||
|
||||
public function write($handle = null)
|
||||
|
||||
{
|
||||
|
||||
if ($handle === null) {
|
||||
|
||||
$handle = fopen('php://output', 'wb');
|
||||
|
||||
} elseif (!is_resource($handle) || get_resource_type($handle) !== 'stream') {
|
||||
|
||||
throw new Exception('Argument must be stream resource.');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
$tables = $views = array();
|
||||
|
||||
|
||||
|
||||
$res = $this->connection->query('SHOW FULL TABLES');
|
||||
|
||||
while ($row = $res->fetch_row()) {
|
||||
|
||||
if ($row[1] === 'VIEW') {
|
||||
|
||||
$views[] = $row[0];
|
||||
|
||||
} else {
|
||||
|
||||
$tables[] = $row[0];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$res->close();
|
||||
|
||||
|
||||
|
||||
$tables = array_merge($tables, $views); // views must be last
|
||||
|
||||
|
||||
|
||||
$this->connection->query('LOCK TABLES `' . implode('` READ, `', $tables) . '` READ');
|
||||
|
||||
|
||||
|
||||
$db = $this->connection->query('SELECT DATABASE()')->fetch_row();
|
||||
|
||||
fwrite($handle, '-- Created at ' . date('j.n.Y G:i') . " using David Grudl MySQL Dump Utility\n"
|
||||
|
||||
. (isset($_SERVER['HTTP_HOST']) ? "-- Host: $_SERVER[HTTP_HOST]\n" : '')
|
||||
|
||||
. '-- MySQL Server: ' . $this->connection->server_info . "\n"
|
||||
|
||||
. '-- Database: ' . $db[0] . "\n"
|
||||
|
||||
. "\n"
|
||||
|
||||
. "SET NAMES utf8;\n"
|
||||
|
||||
. "SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\n"
|
||||
|
||||
. "SET FOREIGN_KEY_CHECKS=0;\n"
|
||||
|
||||
);
|
||||
|
||||
|
||||
|
||||
foreach ($tables as $table) {
|
||||
|
||||
$this->dumpTable($handle, $table);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user