diff --git a/README.md b/README.md index 9b986e3..f2d7b06 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,28 @@ symandy_database_backup: backup_directory: "%kernel.project_dir%/backups" ``` +#### Basic configuration with Additional options +If You don't need all tables in backup you can list tables that you need and +if you don't need 'CREATE TABLE' Statements(ex: you create database structure with migrations) , +you can set 'only_data' config option to have only 'INSERT' Statements in Backup Files + +```yaml +symandy_database_backup: + backups: + app: + connection: + url: "%env(DATABASE_URL)%" + strategy: + max_files: 5 + backup_directory: "%kernel.project_dir%/var/backups" + + only_data: true + tables: + - 'Table_1' + - 'Table_2' + - 'Table_3' +``` + #### Advanced usages ```yaml symandy_database_backup: diff --git a/composer.json b/composer.json index 6d5a071..182abba 100644 --- a/composer.json +++ b/composer.json @@ -21,28 +21,28 @@ ], "require": { "php": ">=8.1", - "symfony/http-kernel": "^6.0", - "symfony/dependency-injection": "^6.0", - "symfony/config": "^6.0", - "symfony/process": "^6.0", - "symfony/serializer": "^6.0", - "symfony/property-access": "^6.0", + "symfony/http-kernel": "^6.4 || ^7.1", + "symfony/dependency-injection": "^6.4 || ^7.1", + "symfony/config": "^6.4 || ^7.1", + "symfony/process": "^6.4 || ^7.1", + "symfony/serializer": "^6.4 || ^7.1", + "symfony/property-access": "^6.4 || ^7.1", "nyholm/dsn": "^2.0" }, "require-dev": { "phpstan/phpstan": "^1.5", "phpunit/phpunit": "^9.5", - "symfony/phpunit-bridge": "^6.0", - "symfony/framework-bundle": "^6.0", - "symfony/yaml": "^6.0", - "symfony/console": "^6.0", + "symfony/phpunit-bridge": "^6.4 || ^7.1", + "symfony/framework-bundle": "^6.4 || ^7.1", + "symfony/yaml": "^6.4 || ^7.1", + "symfony/console": "^6.4 || ^7.1", "doctrine/orm": "^2.11", - "symfony/dotenv": "^6.0", + "symfony/dotenv": "^6.4 || ^7.1", "webmozart/assert": "^1.11", "friendsofphp/php-cs-fixer": "^3.16" }, "scripts": { - "cs-fixer": "vendor/bin/php-cs-fixer fix --dry-run", + "cs-fixer": "vendor/bin/php-cs-fixer fix --dry-run --diff", "analyse": "vendor/bin/phpstan analyse", "test": "vendor/bin/phpunit" } diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index 0553c34..b0013a9 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -23,6 +23,7 @@ use function array_splice; use function getcwd; +use function implode; use function iterator_to_array; use function sprintf; use function Symfony\Component\String\u; @@ -113,6 +114,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int throw new RuntimeException('Unable to get the current directory, check the user permissions') ; + $dumpOnlyData = $backup->getStrategy()->getOnlyData(); + $backupTables = $backup->getStrategy()->getTables(); + $io->info(sprintf('The backup %s is in progress', $backupName)); foreach ($connection->getDatabases() as $database) { @@ -124,7 +128,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $filePath = "$backupDirectory/$backupName-$database-$date.sql"; $process = Process::fromShellCommandline( - '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME}" > "${:FILEPATH}"' + sprintf( + '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" %s "${:DB_NAME}" %s > "${:FILEPATH}"', + $dumpOnlyData ? '--no-create-info' : '', + implode(' ', $backupTables) + ) ); $process->setPty(Process::isPtySupported()); diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 92b1188..03e4ce2 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -55,6 +55,8 @@ public function getConfigTreeBuilder(): TreeBuilder ->integerNode('max_files')->isRequired()->defaultNull()->end() ->scalarNode('backup_directory')->isRequired()->defaultNull()->end() ->scalarNode('date_format')->defaultValue('Y-m-d')->end() + ->booleanNode('only_data')->defaultFalse()->end() + ->arrayNode('tables')->scalarPrototype()->end()->defaultValue([])->end() ->end() ->end() ->end() diff --git a/src/Factory/Backup/BackupFactory.php b/src/Factory/Backup/BackupFactory.php index ba3f43e..70c07de 100644 --- a/src/Factory/Backup/BackupFactory.php +++ b/src/Factory/Backup/BackupFactory.php @@ -20,7 +20,7 @@ final class BackupFactory implements NamedFactoryInterface */ public function __construct( private readonly ConnectionFactory $connectionFactory, - private readonly FactoryInterface $strategyFactory + private readonly FactoryInterface $strategyFactory, ) { } diff --git a/src/Model/Backup/Backup.php b/src/Model/Backup/Backup.php index d53c64c..09e2c4c 100644 --- a/src/Model/Backup/Backup.php +++ b/src/Model/Backup/Backup.php @@ -11,7 +11,7 @@ class Backup public function __construct( private readonly string $name, private readonly Connection $connection, - private readonly Strategy $strategy + private readonly Strategy $strategy, ) { } diff --git a/src/Model/Backup/Strategy.php b/src/Model/Backup/Strategy.php index 75a98bb..41fdc0c 100644 --- a/src/Model/Backup/Strategy.php +++ b/src/Model/Backup/Strategy.php @@ -6,10 +6,15 @@ class Strategy { + /** + * @param list|null $tables + */ public function __construct( private readonly ?int $maxFiles = null, private readonly ?string $backupDirectory = null, - private readonly ?string $dateFormat = 'Y-m-d' + private readonly ?string $dateFormat = 'Y-m-d', + private readonly ?bool $onlyData = false, + private readonly ?array $tables = null, ) { } @@ -27,4 +32,15 @@ public function getDateFormat(): ?string { return $this->dateFormat; } + + public function getOnlyData(): ?bool + { + return $this->onlyData; + } + + /** @return list|null */ + public function getTables(): ?array + { + return $this->tables; + } } diff --git a/src/Model/Connection/ConnectionDriver.php b/src/Model/Connection/ConnectionDriver.php index 738c624..db0647c 100644 --- a/src/Model/Connection/ConnectionDriver.php +++ b/src/Model/Connection/ConnectionDriver.php @@ -14,7 +14,7 @@ enum ConnectionDriver: string public function getConnectionClass(): string { return match ($this) { - self::MySQL => MySQLConnection::class + self::MySQL => MySQLConnection::class, }; } } diff --git a/src/Model/Connection/MySQLConnection.php b/src/Model/Connection/MySQLConnection.php index 5c76658..5d07c60 100644 --- a/src/Model/Connection/MySQLConnection.php +++ b/src/Model/Connection/MySQLConnection.php @@ -14,7 +14,7 @@ public function __construct( private readonly ?string $password = null, private readonly ?string $host = '127.0.0.1', private readonly ?int $port = 3306, - private readonly array $databases = [] + private readonly array $databases = [], ) { }