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

2
.gitignore vendored
View File

@ -8,3 +8,5 @@
/var/
/vendor/
###< symfony/framework-bundle ###
*_backup.sql

View File

@ -11,6 +11,7 @@
"symfony/console": "5.3.*",
"symfony/dotenv": "5.3.*",
"symfony/filesystem": "5.3.*",
"symfony/finder": "5.3.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "5.3.*",
"symfony/process": "5.3.*",

2
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "8c7050f7bee5a93ee3509d8014488736",
"content-hash": "9ee32d11a76246681509a53554f766a3",
"packages": [
{
"name": "psr/cache",

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

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();
}
}