-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MariaDB - Add backupper and restorer (#77)
* noissue - MariaDB - Add backupper and restorer * noissue - MariaDB - Add backupper and restorer * remove unwanted modification * Check backup file can be read * fix configuration examples * fix configuration examples
- Loading branch information
1 parent
4876743
commit 4eb6a93
Showing
11 changed files
with
234 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MakinaCorpus\DbToolsBundle\Backupper\MariaDB; | ||
|
||
use MakinaCorpus\DbToolsBundle\Backupper\AbstractBackupper; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
use Symfony\Component\Process\Process; | ||
|
||
class Backupper extends AbstractBackupper | ||
{ | ||
private ?Process $process = null; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function startBackup(): self | ||
{ | ||
$dbParams = $this->connection->getParams(); | ||
|
||
$args = [ | ||
$this->binary, | ||
]; | ||
|
||
if (isset($dbParams['host'])) { | ||
$args[] = '-h'; | ||
$args[] = $dbParams['host']; | ||
} | ||
|
||
if (isset($dbParams['user'])) { | ||
$args[] = '-u'; | ||
$args[] = $dbParams['user']; | ||
} | ||
|
||
if (isset($dbParams['port'])) { | ||
$args[] = '-P'; | ||
$args[] = $dbParams['port']; | ||
} | ||
|
||
if (isset($dbParams['password'])) { | ||
$args[] = '-p' . $dbParams['password']; | ||
} | ||
|
||
$args[] = '-r'; | ||
$args[] = $this->destination; | ||
$args[] = $dbParams['dbname']; | ||
$args[] = '--no-tablespaces'; | ||
|
||
foreach ($this->excludedTables as $table) { | ||
$args[] = '--ignore-table'; | ||
$args[] = $dbParams['dbname'] . '.' . $table; | ||
} | ||
|
||
if ($this->verbose) { | ||
$args[] = '-v'; | ||
} | ||
|
||
$this->process = new Process( | ||
$args, | ||
null, | ||
null, | ||
null, | ||
600 | ||
); | ||
|
||
$this->process->start(); | ||
|
||
return $this; | ||
} | ||
|
||
public function checkSuccessful(): void | ||
{ | ||
if (!$this->process->isSuccessful()) { | ||
throw new ProcessFailedException($this->process); | ||
} | ||
} | ||
|
||
public function getExtension(): string | ||
{ | ||
return 'sql'; | ||
} | ||
|
||
public function getOutput(): string | ||
{ | ||
return $this->process->getOutput(); | ||
} | ||
|
||
public function getIterator(): \Traversable | ||
{ | ||
return $this->process; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MakinaCorpus\DbToolsBundle\Restorer\MariaDB; | ||
|
||
use MakinaCorpus\DbToolsBundle\Restorer\AbstractRestorer; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
use Symfony\Component\Process\Process; | ||
|
||
class Restorer extends AbstractRestorer | ||
{ | ||
private ?Process $process = null; | ||
private mixed $backupStream = null; | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function startRestore(): self | ||
{ | ||
$dbParams = $this->connection->getParams(); | ||
|
||
$args = [ | ||
$this->binary, | ||
]; | ||
|
||
if (isset($dbParams['host'])) { | ||
$args[] = '-h'; | ||
$args[] = $dbParams['host']; | ||
} | ||
|
||
if (isset($dbParams['user'])) { | ||
$args[] = '-u'; | ||
$args[] = $dbParams['user']; | ||
} | ||
|
||
if (isset($dbParams['port'])) { | ||
$args[] = '-P'; | ||
$args[] = $dbParams['port']; | ||
} | ||
|
||
if (isset($dbParams['password'])) { | ||
$args[] = '-p' . $dbParams['password']; | ||
} | ||
|
||
$args[] = $dbParams['dbname']; | ||
|
||
if ($this->verbose) { | ||
$args[] = '-v'; | ||
} | ||
|
||
$this->backupStream = \fopen($this->backupFilename, 'r'); | ||
|
||
if (false === $this->backupStream) { | ||
throw new \InvalidArgumentException(\sprintf( | ||
"Backup file '%s' can't be read", | ||
$this->backupFilename | ||
)); | ||
} | ||
|
||
$this->process = new Process( | ||
$args, | ||
null, | ||
null, | ||
$this->backupStream, | ||
1800 | ||
); | ||
|
||
$this->process->start(); | ||
|
||
return $this; | ||
} | ||
|
||
public function checkSuccessful(): void | ||
{ | ||
if (!$this->process->isSuccessful()) { | ||
throw new ProcessFailedException($this->process); | ||
} | ||
|
||
\fclose($this->backupStream); | ||
} | ||
|
||
public function getExtension(): string | ||
{ | ||
return 'sql'; | ||
} | ||
|
||
public function getOutput(): string | ||
{ | ||
return $this->process->getOutput(); | ||
} | ||
|
||
public function getIterator(): \Traversable | ||
{ | ||
return $this->process; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters