diff --git a/.gitignore b/.gitignore index 0216fdd..a8c1143 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,4 @@ /public/bundles/ /var/ /vendor/ -###< symfony/framework-bundle ### - -*_backup.sql +###< symfony/framework-bundle ### \ No newline at end of file diff --git a/src/Console/BackupDatabaseCommand.php b/src/Console/BackupDatabaseCommand.php index 3471b22..cfcff4f 100644 --- a/src/Console/BackupDatabaseCommand.php +++ b/src/Console/BackupDatabaseCommand.php @@ -4,6 +4,7 @@ namespace App\Console; use App\Service\BackupService; use App\Service\DatabaseService; +use App\Service\Folders; use App\Service\Traits\SelectDatabaseQuestion; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; @@ -19,6 +20,7 @@ class BackupDatabaseCommand extends Command public function __construct( private readonly BackupService $bs, private readonly DatabaseService $db, + private readonly Folders $folders, ) { parent::__construct(); @@ -35,7 +37,13 @@ class BackupDatabaseCommand extends Command $db = $this->getDatabaseName($input, $output); $fileName = $input->getArgument('file'); - $backupName = $this->bs->backup($db, $fileName); + if (!$fileName) { + $date = date('y_m_d_H_i'); + $fileName = sprintf('%s_%s_backup.sql', $db, $date); + } + + $output->writeln(sprintf('Start backup of %s to %s', $db, $fileName));; + $backupName = $this->bs->backup($db, $this->folders->getBackupFolder() . DIRECTORY_SEPARATOR . $fileName); $output->writeln(sprintf('Backup written to: %s', $backupName)); return Command::SUCCESS; diff --git a/src/Console/RestoreDatabaseCommand.php b/src/Console/RestoreDatabaseCommand.php index a9ceac3..227f885 100644 --- a/src/Console/RestoreDatabaseCommand.php +++ b/src/Console/RestoreDatabaseCommand.php @@ -4,6 +4,7 @@ namespace App\Console; use App\Service\BackupService; use App\Service\DatabaseService; +use App\Service\Folders; use App\Service\Traits\SelectDatabaseQuestion; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; @@ -21,6 +22,7 @@ class RestoreDatabaseCommand extends Command public function __construct( private readonly DatabaseService $db, private readonly BackupService $bs, + private readonly Folders $folders, ) { parent::__construct(); @@ -36,25 +38,27 @@ class RestoreDatabaseCommand extends Command { $db = $this->getDatabaseName($input, $output); $fileName = $input->getArgument('file'); - if (!$fileName){ - $fileName = $this->getFileName($input, $output); + if (!$fileName) { + $fileName = $this->getBackupFileName($input, $output, $db); } - $this->bs->restore($db, $fileName); + $output->writeln(sprintf('Start restore of %s from %s', $db, $fileName));; + $this->bs->restore($db, $this->folders->getBackupFolder() . DIRECTORY_SEPARATOR . $fileName); + $output->writeln(sprintf('Restored from: %s', $fileName)); return Command::SUCCESS; } - private function getFileName(InputInterface $input, OutputInterface $output): string + private function getBackupFileName(InputInterface $input, OutputInterface $output, string $db): string { $finder = new Finder(); - $finder->in(__DIR__ . '/../..'); + $finder->in($this->folders->getBackupFolder()); $files = []; - foreach ($finder->files()->name('*_backup.sql') as $file) { + foreach ($finder->files()->name(sprintf('%s_*_backup.sql', $db)) as $file) { $files[] = $file->getFilename(); } - sort($files); - $selectQuestion = new ChoiceQuestion('Backup name (*_backup.sql): ', $files); + rsort($files); + $selectQuestion = new ChoiceQuestion(sprintf('Backup name (%s_*_backup.sql)', $db), $files, 0); $question = $this->getHelper('question'); return $question->ask($input, $output, $selectQuestion); } diff --git a/src/Service/BackupService.php b/src/Service/BackupService.php index 1e4de21..db681de 100644 --- a/src/Service/BackupService.php +++ b/src/Service/BackupService.php @@ -10,16 +10,11 @@ readonly class BackupService private const COMMAND_DB = 'mariadb'; public function __construct( - private DatabaseCredentials $credentials + private DatabaseCredentials $credentials, ) {} - public function backup(string $db, ?string $fileName = null): string + public function backup(string $db, string $fileName): string { - if (!$fileName) { - $date = date('y_m_d_H_i'); - $fileName = sprintf('%s_%s_backup.sql', $db, $date); - } - $process = new Process([ self::COMMAND_DUMP, '-u', $this->credentials->user, diff --git a/src/Service/Folders.php b/src/Service/Folders.php new file mode 100644 index 0000000..7502766 --- /dev/null +++ b/src/Service/Folders.php @@ -0,0 +1,32 @@ +projectRoot . DIRECTORY_SEPARATOR . 'var'; + } + + public function getBackupFolder(): string + { + return $this->get('backup'); + } + + private function get(string $name): string + { + $folder = $this->getVarFolder() . DIRECTORY_SEPARATOR . $name; + if (!is_dir($folder)) { + mkdir($folder, 0777, true); + } + return $folder; + } +} \ No newline at end of file