Shikiryu_Backup/app/code/backup/Mysql.php

103 lines
2.8 KiB
PHP
Raw Normal View History

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;
2015-08-17 20:17:46 +02:00
protected $tables;
protected $host;
protected $db;
protected $login;
protected $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);
$this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->db, $this->login, $this->pwd);
2015-08-17 20:17:46 +02:00
empty($this->tables) || $this->tables == '*' ? $this->everything() : $this->fromTables($this->tables);
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
}
2015-08-17 20:17:46 +02:00
$this->_streamsToBackup[sprintf('db-%s.sql', $this->db)] = $this->getFromTables();
2015-07-03 00:00:30 +02:00
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
}
2015-08-17 20:17:46 +02:00
$this->_streamsToBackup[sprintf('db-%s.sql', $this->db)] = $this->getFromTables();
2015-07-03 00:00:30 +02:00
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]);
2015-08-17 20:17:46 +02:00
// $row[$j] = preg_replace("\n", "\\n", $row[$j]);
2015-07-03 00:00:30 +02:00
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()
{
2015-08-17 20:17:46 +02:00
return !empty($this->_streamsToBackup);
2015-07-12 20:53:15 +02:00
}
2015-07-03 00:00:30 +02:00
}
?>