Add restore command
This commit is contained in:
@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user