Skip to content

Commit

Permalink
Merge pull request #238 from biblioverse/remove-symandy
Browse files Browse the repository at this point in the history
Remove Symandy, create own db backup
  • Loading branch information
SergioMendolia authored Dec 19, 2024
2 parents 5a7ce39 + a41ccd4 commit 066ed1f
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 136 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"phpstan/phpdoc-parser": "^1.18",
"rtheunissen/guzzle-log-middleware": "^2.0",
"scienta/doctrine-json-functions": "^5.3",
"symandy/database-backup-bundle": "^0.4.0",
"symfony/apache-pack": "^1.0",
"symfony/asset": "^7.1",
"symfony/console": "^6.2",
Expand Down
124 changes: 2 additions & 122 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion config/bundles.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
Symfony\UX\Autocomplete\AutocompleteBundle::class => ['all' => true],
Symfony\UX\StimulusBundle\StimulusBundle::class => ['all' => true],
Andante\PageFilterFormBundle\AndantePageFilterFormBundle::class => ['all' => true],
Symandy\DatabaseBackupBundle\SymandyDatabaseBackupBundle::class => ['all' => true],
ACSEO\TypesenseBundle\ACSEOTypesenseBundle::class => ['all' => true],
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
];
8 changes: 0 additions & 8 deletions config/packages/symandy_database_backup.yaml

This file was deleted.

1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ parameters:
env(KOBO_READINGSERVICES_URL): 'https://readingservices.kobo.com'
ALLOW_BOOK_RELOCATION: '%env(bool:ALLOW_BOOK_RELOCATION)%'
env(ALLOW_BOOK_RELOCATION): true
DATABASE_URL: '%env(DATABASE_URL)%'

services:
# default configuration for services in *this* file
Expand Down
2 changes: 1 addition & 1 deletion doc/src/content/docs/guides/Administrator/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Clears the cache
## `doctrine:migrations:migrate`
Executes all missing database migrations.

## `symandy:databases:backup`
## `app:backup-db`
Creates a sql backup of the database in the `backups folder`.

## `typesense:create`
Expand Down
131 changes: 131 additions & 0 deletions src/Command/BackupDbCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace App\Command;

use Doctrine\DBAL\Tools\DsnParser;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;

#[AsCommand(
name: 'app:backup-db',
description: 'Add a short description for your command',
)]
class BackupDbCommand extends Command
{
public function __construct(
private readonly Filesystem $filesystem,
private readonly EntityManagerInterface $entityManager,
#[Autowire(param: 'kernel.project_dir')]
private readonly string $projectDir,
#[Autowire(param: 'DATABASE_URL')]
private readonly string $dsn,
) {
parent::__construct();
}

protected function configure(): void
{
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$mysqldump = (new ExecutableFinder())->find('mysqldump');

if (null === $mysqldump) {
$io->error('Cannot find "mysqldump" executable');

return Command::INVALID;
}

$connection = $this->entityManager->getConnection();
$backupName = 'biblioteca-backup';

$backupDirectory = $this->projectDir.'/backups';

$io->info(sprintf('The backup %s is in progress', $backupName));

$database = $connection->getDatabase();
if ($output->isVerbose()) {
$io->comment("Backup for $database database has started");
}

$filePath = "$backupDirectory/$backupName-$database-".date('Y-m-d').'.sql';

$process = Process::fromShellCommandline(
'"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME}" > "${:FILEPATH}"'
);

$parser = new DsnParser();
$params = $parser->parse($this->dsn);

$process->setPty(Process::isPtySupported());

$process->run(null, [
'MYSQL_DUMP' => $mysqldump,
'DB_USER' => $params['user'], // @phpstan-ignore-line
'DB_HOST' => $params['host'], // @phpstan-ignore-line
'DB_PORT' => $params['port'], // @phpstan-ignore-line
'DB_NAME' => $database,
'MYSQL_PWD' => $params['password'], // @phpstan-ignore-line
'FILEPATH' => $filePath,
]);

if (!$process->isSuccessful()) {
$message = '' !== $process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput();

$io->error($message);

return Command::FAILURE;
}

$finder = (new Finder())
->in($backupDirectory)
->name(["$backupName-$database-*.sql"])
->sortByModifiedTime()
->depth(['== 0'])
->files();

$filesCount = $finder->count();

/** @var array<int, \SplFileInfo> $files */
$files = iterator_to_array($finder);

$maxFiles = 5;
if ($filesCount > $maxFiles) {
$filesToDeleteCount = $filesCount - $maxFiles;
array_splice($files, $filesToDeleteCount);

if (1 === $filesToDeleteCount) {
$io->warning('Reached the max backup files limit, removing the oldest one');
} else {
$io->warning(sprintf(
'Reached the max backup files limit, removing the %d oldest ones',
$filesToDeleteCount
));
}

foreach ($files as $file) {
if ($output->isVerbose()) {
$io->comment(sprintf('Deleting "%s"', $file->getRealPath()));
}

$this->filesystem->remove($file->getRealPath());
}
}

$io->success(sprintf('Backup %s has been successfully completed', $backupName));

return Command::SUCCESS;
}
}
3 changes: 0 additions & 3 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@
"ref": "4ea4a4b6730f83239608d7d4c849533645c70169"
}
},
"symandy/database-backup-bundle": {
"version": "v0.4.0"
},
"symfony/apache-pack": {
"version": "1.0",
"recipe": {
Expand Down

0 comments on commit 066ed1f

Please sign in to comment.