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

@ -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;
}

View 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;
}
}