Shikiryu_Backup/app/code/backup/Mysql.php

130 lines
3.2 KiB
PHP
Raw Permalink 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
2021-06-30 16:00:24 +02:00
use PDO;
2015-07-10 19:07:14 +02:00
class Mysql extends BackupAbstract
2015-07-09 00:46:17 +02:00
{
use IsLocalTrait;
2015-07-03 00:00:30 +02:00
/**
2021-06-30 16:00:24 +02:00
* @var $pdo PDO
2016-07-03 21:54:35 +02:00
*/
2015-07-12 20:34:54 +02:00
private $pdo;
2015-08-18 17:02:37 +02:00
/*
2016-07-03 21:54:35 +02:00
* from config
*/
2015-08-17 20:17:46 +02:00
protected $tables;
protected $host;
protected $database;
2015-08-17 20:17:46 +02:00
protected $login;
protected $pwd;
2015-07-03 00:00:30 +02:00
/**
2016-07-03 21:54:35 +02:00
* @param array $config
*/
public function __construct(array $config = [])
2016-07-03 21:54:35 +02:00
{
2015-07-12 20:34:54 +02:00
parent::__construct($config);
2015-07-03 00:00:30 +02:00
}
/**
2016-07-03 21:54:35 +02:00
* @param array $tables
* @return $this|string
*/
private function fromTables($tables = [])
2015-07-03 00:00:30 +02:00
{
2016-07-03 21:54:35 +02:00
if (!empty($tables)) {
2015-07-12 20:34:54 +02:00
$this->tables = $tables;
2015-07-03 00:00:30 +02:00
}
2016-07-03 21:54:35 +02:00
$this->streams_to_backup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables();
2015-07-03 00:00:30 +02:00
return $this;
}
/**
2016-07-03 21:54:35 +02:00
* set the list of table to backup to all tables
*
* @return $this
*/
private function everything()
{
2015-07-12 20:34:54 +02:00
$this->tables = array();
2016-07-03 21:54:35 +02:00
foreach ($this->pdo->query('SHOW TABLES') as $table) {
2015-07-12 20:34:54 +02:00
$this->tables[] = $table;
2015-07-03 00:00:30 +02:00
}
2016-07-03 21:54:35 +02:00
$this->streams_to_backup[sprintf('db-%s.sql', $this->database)] = $this->getFromTables();
2015-07-03 00:00:30 +02:00
return $this;
}
/**
2016-07-03 21:54:35 +02:00
* @return string
*/
2015-07-03 00:00:30 +02:00
private function getFromTables()
{
2019-01-25 22:19:50 +01:00
$return = '';
2015-07-12 20:34:54 +02:00
foreach ($this->tables as $table) {
2016-07-03 21:54:35 +02:00
if (is_array($table)) {
2015-07-03 00:00:30 +02:00
$table = $table[0];
}
2019-01-25 22:19:50 +01:00
$result = $this->pdo->query('SELECT * FROM ' . $table);
2015-07-03 00:00:30 +02:00
$num_fields = $result->columnCount();
$return .= 'DROP TABLE IF EXISTS ' . $table . ';';
2019-01-25 22:19:50 +01:00
$result2 = $this->pdo->query('SHOW CREATE TABLE ' . $table);
2015-07-03 00:00:30 +02:00
$row2 = $result2->fetch();
$return.= "\n\n" . $row2[1] . ";\n\n";
2016-07-03 21:54:35 +02:00
foreach ($result as $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.= '""';
2015-07-03 00:00:30 +02:00
}
2016-07-03 21:54:35 +02:00
if ($j < ($num_fields - 1)) {
$return.= ',';
}
}
$return.= ");\n";
2015-07-03 00:00:30 +02:00
}
$return.="\n\n\n";
}
return $return;
}
2015-07-12 20:53:15 +02:00
/**
2016-07-03 21:54:35 +02:00
* @return bool
*/
2015-07-12 20:53:15 +02:00
public function isValid()
{
2016-07-03 21:54:35 +02:00
return !empty($this->streams_to_backup);
2015-07-12 20:53:15 +02:00
}
2015-08-18 17:02:37 +02:00
2016-07-03 21:54:35 +02:00
/**
* Function that can be used to initialize the backup
*/
2015-08-18 17:02:37 +02:00
protected function preBuild()
{
2021-06-30 16:00:24 +02:00
$this->pdo = new PDO('mysql:host='.$this->host.';dbname='.$this->database, $this->login, $this->pwd);
2015-08-18 17:02:37 +02:00
}
2016-07-03 21:54:35 +02:00
/**
* Function that can be used after the backup
*/
2015-08-18 17:02:37 +02:00
protected function postBuild()
{
// TODO: Implement postBuild() method.
}
2016-07-03 21:54:35 +02:00
/**
* Mandatory function doing the backup
*/
2015-08-18 17:02:37 +02:00
protected function build()
{
2019-01-25 22:19:50 +01:00
empty($this->tables) || $this->tables === '*' ? $this->everything() : $this->fromTables($this->tables);
2015-08-18 17:02:37 +02:00
}
2015-07-03 00:00:30 +02:00
}