Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $dumperObject = new DbDumper();
- $dumperObject->magic();
- $dumperObject->init([
- 'host' => 'localhost',
- 'port' => '3306',
- 'dbname' => 'mg_bazarsports',
- 'user' => '#######',
- 'password' => '######',
- ]);
- $dumperObject->debug();
- /*
- * Author vkwasinski at gmail or vinicius.carvalho at agenciafrog.com.br
- *
- * This class is intent to dump databases
- * which not have foreign host access enable
- *
- * USAGE:
- *
- * $dumperObject = new DbDumper([
- * 'host' => 'localhost',
- * 'port' => '3306',
- * 'dbname' => 'ventura_wp',
- * 'user' => 'root',
- * 'password' => '',
- * ]);
- *
- * Will dump the whole database to a file and download it for you as [dbname]_dump.sql :')
- *
- */
- class DbDumper {
- private $context = [];
- private $connectionString = "mysql:host=#host;port=#port;dbname=#dbname";
- private $connectionContext = [
- 'host' => NULL,
- 'port' => NULL,
- 'dbname' => NULL,
- 'user' => NULL,
- 'password' => NULL,
- ];
- var $error = [];
- // Search in the context and then in the connectionContext
- public function __get($name) {
- if (isset($this->context[$name]) && !is_null($this->context[$name])) {
- return $this->context[$name];
- } else if (isset($this->connectionContext[$name]) && !is_null($this->connectionContext[$name])) {
- return $this->connectionContext[$name];
- }
- }
- public function __set($name, $value) { isset($value) && !is_null($value)? $this->context[$name] = $value: NULL; }
- public function __call($method, $arguments) { $this->__behavioral(); return $this;}
- public function __wakeup() { $connection->reconnect(); }
- public function __toString() { return ''.$this; }
- public function __behavioral() {
- // print '<pre>';
- return $this;
- } // This method is defined to change the object behavior after each method called
- public function __construct($arrCredentials = false) {
- $this->init($arrCredentials);
- return $this;
- }
- public function init($arrCredentials = false) {
- if (!$arrCredentials) return false;
- foreach (array_keys($this->connectionContext) as $expectedKey) {
- if (!isset($arrCredentials[$expectedKey])) {
- $this->error[] = "$expectedKey missing";
- continue;
- }
- $this->connectionContext[$expectedKey] = $arrCredentials[$expectedKey];
- }
- if (empty($this->error)) try {
- $this->setCredentials(); // set connection credentials to the actual database;
- $this->setConnection(); // Instance of a new PDO
- $this->showTables(); // SHOW TABLES;
- $this->appendFileHeader();
- $this->dumpData(); // Come along every table and create insert statements
- // $this->createDumpFile();
- } catch (PDOException $e) {
- $this->error[] = 'Error!: '.$e->getMessage();
- die(implode(', ',$this->error));
- }
- return $this;
- }
- private function setCredentials() {
- if (!empty($this->error)) return $this;
- foreach (array_keys($this->connectionContext) as $connectionParam)
- $this->connectionString = preg_replace("/\#$connectionParam/", $this->connectionContext[$connectionParam], $this->connectionString);
- return $this;
- }
- public function setConnection() {
- if (!empty($this->error)) return $this;
- $this->connection = new PDO($this->connectionString, $this->user, $this->password);
- return $this;
- }
- public function showTables() {
- if (!empty($this->error)) return $this;
- $this->tablesToparse = $this->query('SHOW TABLES;');
- return $this;
- }
- private function appendFileHeader() {
- if (!empty($this->error)) return $this;
- $this->file .= "CREATE DATABASE IF NOT EXISTS `{$this->dbname}`;\n\n";
- $this->file .= "USE `{$this->dbname}`;\n\n";
- $this->file .= "SET character_set_client = utf8;\n\n";
- return $this;
- }
- public function dumpData() {
- if (!empty($this->error)) return $this;
- $this->tablesStrutures = [];
- foreach ( $this->tablesToparse as $tableName) {
- $tableName = $tableName{'Tables_in_'.$this->dbname};
- $this->appendCreateTable($tableName);
- $this->appendInserts($tableName);
- }
- // var_dump($this->file);
- }
- private function appendCreateTable($tableName) {
- if (!$tableName) return;
- $createTableStatement = $this->query("SHOW CREATE TABLE $tableName;")[0];
- $createTableStatement['Create Table'];
- $this->file .= "DROP TABLE IF EXISTS `$tableName`;\n";
- $this->file .= $createTableStatement['Create Table'].";\n\n";
- }
- private function appendInserts($tableName) {
- if (!($tableName && empty($this->error))) return;
- $this->file .= "LOCK TABLES `$tableName` WRITE;\n";
- // We must read the table columns metadata
- // to know about the type of the column and generate the right INSERT statement
- // We can instead of just passing the table's name to this function
- // pass a associative array like [column_name => column_type],
- // perhaps we just need to differ string from integer and treat NULL and Empty strings.
- $wholeTable = $this->query("SELECT * FROM wp_posts;");
- print '<pre>';
- var_dump($wholeTable);
- exit;
- $this->file .= "UNLOCK TABLES;\n\n";
- }
- private function query($statement) {
- if (!($statement && empty($this->error))) return;
- try {
- $query = $this->connection->prepare($statement);
- $query->execute();
- $query = $query->fetchAll(PDO::FETCH_ASSOC);
- } catch (PDOException $e) {
- $this->error[] = 'Error!: '.$e->getMessage();
- }
- return $query;
- }
- // This method is intent to create a SQLDump File from the flagged Database
- public function createDumpFile() {
- if (!empty($this->error)) return $this;
- $this->filename = $this->dbname.'_dump.sql';
- file_put_contents($this->filename, $this->file);
- return;
- header('Content-Description: File Transfer');
- header('Content-type: application/octet-stream');
- header('Content-Disposition: attachment; filename='.$this->filename);
- header('Content-Transfer-Encoding: binary');
- header('Content-Length: ' . strlen($this->file));
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Expires: 0');
- header('Pragma: public');
- print $this->file;
- return $this;
- }
- public function debug() {
- print '<pre>';
- var_dump($this);
- print '</pre>';
- }
- }
Add Comment
Please, Sign In to add comment