Implement mysqldump/backup service/command
This commit is contained in:
@ -2,19 +2,23 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Service\DatabaseManager;
|
||||
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\Question;
|
||||
|
||||
class BackupDatabase extends Command
|
||||
{
|
||||
protected static $defaultName = 'app:db:create';
|
||||
use SelectDatabaseQuestion;
|
||||
|
||||
protected static $defaultName = 'app:db:backup';
|
||||
|
||||
public function __construct(
|
||||
private DatabaseManager $db,
|
||||
private DatabaseService $db,
|
||||
private BackupService $bs,
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
@ -27,15 +31,10 @@ class BackupDatabase extends Command
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$question = $this->getHelper('question');
|
||||
$db = $this->getDatabaseName($input, $output);
|
||||
|
||||
if (!$name = $input->getArgument('name')) {
|
||||
$name = $question->ask($input, $output, new Question('Database name: '));
|
||||
}
|
||||
|
||||
$this->db->createDatabase($name);
|
||||
|
||||
$output->writeln(sprintf('Database "%s" successfully created', $name));
|
||||
$output = $this->bs->backup($db);
|
||||
dump($output);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Service\DatabaseManager;
|
||||
use App\Service\DatabaseService;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@ -14,7 +14,7 @@ class CreateDatabaseCommand extends Command
|
||||
protected static $defaultName = 'app:db:create';
|
||||
|
||||
public function __construct(
|
||||
private DatabaseManager $db,
|
||||
private DatabaseService $db,
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Service\DatabaseManager;
|
||||
use App\Service\DatabaseService;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
@ -12,7 +12,7 @@ class ListDatabasesCommand extends Command
|
||||
protected static $defaultName = 'app:db:list';
|
||||
|
||||
public function __construct(
|
||||
private DatabaseManager $db,
|
||||
private DatabaseService $db,
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Service\DatabaseManager;
|
||||
use App\Service\DatabaseService;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
@ -12,7 +12,7 @@ class ListUsersCommand extends Command
|
||||
protected static $defaultName = 'app:users:list';
|
||||
|
||||
public function __construct(
|
||||
private DatabaseManager $db,
|
||||
private DatabaseService $db,
|
||||
)
|
||||
{
|
||||
parent::__construct();
|
||||
|
30
src/Service/BackupService.php
Normal file
30
src/Service/BackupService.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class BackupService
|
||||
{
|
||||
public function __construct(private DatabaseCredentials $credentials)
|
||||
{
|
||||
}
|
||||
|
||||
public function backup(string $db): string
|
||||
{
|
||||
$filesystem = new Filesystem();
|
||||
$process = new Process([
|
||||
'mysqldump',
|
||||
'-B', $db,
|
||||
'-u', $this->credentials->getUser(),
|
||||
]);
|
||||
$process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]);
|
||||
$process->run();
|
||||
|
||||
$fileName = sprintf('%s_backup.sql', $db);
|
||||
$filesystem->dumpFile($fileName, $process->getOutput());
|
||||
|
||||
return $fileName;
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@ namespace App\Service;
|
||||
|
||||
use mysqli;
|
||||
|
||||
class DatabaseManager
|
||||
class DatabaseService
|
||||
{
|
||||
private mysqli $conn;
|
||||
|
Reference in New Issue
Block a user