2015-07-03 00:00:30 +02:00
|
|
|
<?php
|
|
|
|
|
2015-07-10 19:07:14 +02:00
|
|
|
namespace Shikiryu\Backup\Backup;
|
2015-07-03 00:00:30 +02:00
|
|
|
|
2015-07-10 19:07:14 +02:00
|
|
|
class Mysql extends BackupAbstract
|
2015-07-09 00:46:17 +02:00
|
|
|
{
|
2015-07-03 00:00:30 +02:00
|
|
|
|
|
|
|
/**
|
2015-07-12 20:34:54 +02:00
|
|
|
* @var $pdo \PDO
|
2015-07-03 00:00:30 +02:00
|
|
|
*/
|
2015-07-12 20:34:54 +02:00
|
|
|
private $pdo;
|
|
|
|
private $tables;
|
|
|
|
private $host;
|
|
|
|
private $db;
|
|
|
|
private $login;
|
|
|
|
private $pwd;
|
2015-07-03 00:00:30 +02:00
|
|
|
|
|
|
|
/**
|
2015-07-12 20:34:54 +02:00
|
|
|
* @param array $config
|
2015-07-03 00:00:30 +02:00
|
|
|
*/
|
2015-07-12 20:34:54 +02:00
|
|
|
public function __construct(array $config = array()) {
|
|
|
|
parent::__construct($config);
|
|
|
|
empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables);
|
|
|
|
$this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->db, $this->login, $this->pwd);
|
2015-07-03 00:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $tables
|
|
|
|
* @return $this|string
|
|
|
|
*/
|
|
|
|
public function fromTables($tables = array())
|
|
|
|
{
|
|
|
|
if(!empty($tables)) {
|
2015-07-12 20:34:54 +02:00
|
|
|
$this->tables = $tables;
|
2015-07-03 00:00:30 +02:00
|
|
|
}
|
|
|
|
$this->_streamsToBackup[] = $this->getFromTables();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the list of table to backup to all tables
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function everything() {
|
2015-07-12 20:34:54 +02:00
|
|
|
$this->tables = array();
|
|
|
|
foreach($this->pdo->query('SHOW TABLES') as $table) {
|
|
|
|
$this->tables[] = $table;
|
2015-07-03 00:00:30 +02:00
|
|
|
}
|
|
|
|
$this->_streamsToBackup[] = $this->getFromTables();
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getFromTables()
|
|
|
|
{
|
|
|
|
$return = "";
|
2015-07-12 20:34:54 +02:00
|
|
|
foreach ($this->tables as $table) {
|
2015-07-03 00:00:30 +02:00
|
|
|
if(is_array($table)) {
|
|
|
|
$table = $table[0];
|
|
|
|
}
|
2015-07-12 20:34:54 +02:00
|
|
|
$result = $this->pdo->prepare('SELECT * FROM ' . $table);
|
2015-07-03 00:00:30 +02:00
|
|
|
$result->execute();
|
|
|
|
$num_fields = $result->columnCount();
|
|
|
|
$return .= 'DROP TABLE IF EXISTS ' . $table . ';';
|
2015-07-12 20:34:54 +02:00
|
|
|
$result2 = $this->pdo->prepare('SHOW CREATE TABLE ' . $table);
|
2015-07-03 00:00:30 +02:00
|
|
|
$result2->execute();
|
|
|
|
$row2 = $result2->fetch();
|
|
|
|
$return.= "\n\n" . $row2[1] . ";\n\n";
|
|
|
|
foreach($result as $i=>$row){
|
|
|
|
$return.= 'INSERT INTO ' . $table . ' VALUES(';
|
|
|
|
for ($j = 0; $j < $num_fields; $j++) {
|
|
|
|
$row[$j] = addslashes($row[$j]);
|
|
|
|
$row[$j] = preg_replace("\n", "\\n", $row[$j]);
|
|
|
|
if (isset($row[$j])) {
|
|
|
|
$return.= '"' . $row[$j] . '"';
|
|
|
|
} else {
|
|
|
|
$return.= '""';
|
|
|
|
}
|
|
|
|
if ($j < ($num_fields - 1)) {
|
|
|
|
$return.= ',';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$return.= ");\n";
|
|
|
|
}
|
|
|
|
$return.="\n\n\n";
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2015-07-12 20:53:15 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isValid()
|
|
|
|
{
|
|
|
|
// TODO: Implement isValid() method.
|
|
|
|
}
|
2015-07-03 00:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|