Move database backups to their own location in var/backups
This commit is contained in:
parent
ef9af933bd
commit
77a1729296
2
.gitignore
vendored
2
.gitignore
vendored
@ -8,5 +8,3 @@
|
|||||||
/var/
|
/var/
|
||||||
/vendor/
|
/vendor/
|
||||||
###< symfony/framework-bundle ###
|
###< symfony/framework-bundle ###
|
||||||
|
|
||||||
*_backup.sql
|
|
||||||
|
@ -4,6 +4,7 @@ namespace App\Console;
|
|||||||
|
|
||||||
use App\Service\BackupService;
|
use App\Service\BackupService;
|
||||||
use App\Service\DatabaseService;
|
use App\Service\DatabaseService;
|
||||||
|
use App\Service\Folders;
|
||||||
use App\Service\Traits\SelectDatabaseQuestion;
|
use App\Service\Traits\SelectDatabaseQuestion;
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
@ -19,6 +20,7 @@ class BackupDatabaseCommand extends Command
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly BackupService $bs,
|
private readonly BackupService $bs,
|
||||||
private readonly DatabaseService $db,
|
private readonly DatabaseService $db,
|
||||||
|
private readonly Folders $folders,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@ -35,7 +37,13 @@ class BackupDatabaseCommand extends Command
|
|||||||
$db = $this->getDatabaseName($input, $output);
|
$db = $this->getDatabaseName($input, $output);
|
||||||
$fileName = $input->getArgument('file');
|
$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));
|
$output->writeln(sprintf('Backup written to: %s', $backupName));
|
||||||
|
|
||||||
return Command::SUCCESS;
|
return Command::SUCCESS;
|
||||||
|
@ -4,6 +4,7 @@ namespace App\Console;
|
|||||||
|
|
||||||
use App\Service\BackupService;
|
use App\Service\BackupService;
|
||||||
use App\Service\DatabaseService;
|
use App\Service\DatabaseService;
|
||||||
|
use App\Service\Folders;
|
||||||
use App\Service\Traits\SelectDatabaseQuestion;
|
use App\Service\Traits\SelectDatabaseQuestion;
|
||||||
use Symfony\Component\Console\Attribute\AsCommand;
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
@ -21,6 +22,7 @@ class RestoreDatabaseCommand extends Command
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly DatabaseService $db,
|
private readonly DatabaseService $db,
|
||||||
private readonly BackupService $bs,
|
private readonly BackupService $bs,
|
||||||
|
private readonly Folders $folders,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@ -37,24 +39,26 @@ class RestoreDatabaseCommand extends Command
|
|||||||
$db = $this->getDatabaseName($input, $output);
|
$db = $this->getDatabaseName($input, $output);
|
||||||
$fileName = $input->getArgument('file');
|
$fileName = $input->getArgument('file');
|
||||||
if (!$fileName) {
|
if (!$fileName) {
|
||||||
$fileName = $this->getFileName($input, $output);
|
$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;
|
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 = new Finder();
|
||||||
$finder->in(__DIR__ . '/../..');
|
$finder->in($this->folders->getBackupFolder());
|
||||||
$files = [];
|
$files = [];
|
||||||
foreach ($finder->files()->name('*_backup.sql') as $file) {
|
foreach ($finder->files()->name(sprintf('%s_*_backup.sql', $db)) as $file) {
|
||||||
$files[] = $file->getFilename();
|
$files[] = $file->getFilename();
|
||||||
}
|
}
|
||||||
sort($files);
|
rsort($files);
|
||||||
$selectQuestion = new ChoiceQuestion('Backup name (*_backup.sql): ', $files);
|
$selectQuestion = new ChoiceQuestion(sprintf('Backup name (%s_*_backup.sql)', $db), $files, 0);
|
||||||
$question = $this->getHelper('question');
|
$question = $this->getHelper('question');
|
||||||
return $question->ask($input, $output, $selectQuestion);
|
return $question->ask($input, $output, $selectQuestion);
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,11 @@ readonly class BackupService
|
|||||||
private const COMMAND_DB = 'mariadb';
|
private const COMMAND_DB = 'mariadb';
|
||||||
|
|
||||||
public function __construct(
|
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([
|
$process = new Process([
|
||||||
self::COMMAND_DUMP,
|
self::COMMAND_DUMP,
|
||||||
'-u', $this->credentials->user,
|
'-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