Add restore command
This commit is contained in:
@ -33,8 +33,8 @@ class BackupDatabase extends Command
|
||||
{
|
||||
$db = $this->getDatabaseName($input, $output);
|
||||
|
||||
$output = $this->bs->backup($db);
|
||||
dump($output);
|
||||
$backupName = $this->bs->backup($db);
|
||||
$output->writeln(sprintf('Backup written to: %s', $backupName));
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
52
src/Console/RestoreDatabase.php
Normal file
52
src/Console/RestoreDatabase.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Service\BackupService;
|
||||
use App\Service\DatabaseService;
|
||||
use App\Service\Traits\SelectDatabaseQuestion;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ChoiceQuestion;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
class RestoreDatabase extends Command
|
||||
{
|
||||
use SelectDatabaseQuestion;
|
||||
|
||||
protected static $defaultName = 'app:db:restore';
|
||||
|
||||
public function __construct(
|
||||
private DatabaseService $db,
|
||||
private BackupService $bs,
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->addArgument('name', InputArgument::OPTIONAL, 'Database name');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$question = $this->getHelper('question');
|
||||
$db = $this->getDatabaseName($input, $output);
|
||||
|
||||
$finder = new Finder();
|
||||
$finder->in(__DIR__ . '/../..');
|
||||
$files = [sprintf('%s_backup.sql', $db)];
|
||||
foreach ($finder->files()->name('*_backup.sql') as $file) {
|
||||
$files[] = $file->getFilename();
|
||||
}
|
||||
$selectQuestion = new ChoiceQuestion('Backup name (*_backup.sql): ', $files);
|
||||
$fileName = $question->ask($input, $output, $selectQuestion);
|
||||
|
||||
$this->bs->restore($db, $fileName);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
@ -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