Move database backups to their own location in var/backups
This commit is contained in:
parent
ef9af933bd
commit
77a1729296
4
.gitignore
vendored
4
.gitignore
vendored
@ -7,6 +7,4 @@
|
||||
/public/bundles/
|
||||
/var/
|
||||
/vendor/
|
||||
###< symfony/framework-bundle ###
|
||||
|
||||
*_backup.sql
|
||||
###< symfony/framework-bundle ###
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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,
|
||||
|
32
src/Service/Folders.php
Normal file
32
src/Service/Folders.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
|
||||
|
||||
readonly class Folders
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire('%kernel.project_dir%')] private string $projectRoot
|
||||
) {}
|
||||
|
||||
private function getVarFolder(): string
|
||||
{
|
||||
return $this->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;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user