Add restore command

This commit is contained in:
Tim
2021-12-16 01:40:55 +01:00
parent 11089d82bc
commit 7daddaab88
6 changed files with 77 additions and 6 deletions

View File

@ -3,6 +3,7 @@
namespace App\Service;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\InputStream;
use Symfony\Component\Process\Process;
class BackupService
@ -11,8 +12,12 @@ class BackupService
{
}
public function backup(string $db): string
public function backup(string $db, ?string $fileName = null): string
{
if (!$fileName) {
$fileName = sprintf('%s_backup.sql', $db);
}
$filesystem = new Filesystem();
$process = new Process([
'mysqldump',
@ -21,10 +26,21 @@ class BackupService
]);
$process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]);
$process->run();
$fileName = sprintf('%s_backup.sql', $db);
$filesystem->dumpFile($fileName, $process->getOutput());
return $fileName;
}
public function restore(string $db, string $fileName): void
{
$process = new Process([
'mysql',
'-u', $this->credentials->getUser(),
$db,
]);
$process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]);
$process->setInput(file_get_contents($fileName));
$process->run();
}
}