From f3a3b9b917689aa889ac4c2382d82be5c1734d9b Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 11:04:38 +0000 Subject: [PATCH 01/21] Add a Tables Bundle Config Option. --- src/DependencyInjection/Configuration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 92b1188..a543fa6 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -55,6 +55,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->integerNode('max_files')->isRequired()->defaultNull()->end() ->scalarNode('backup_directory')->isRequired()->defaultNull()->end() ->scalarNode('date_format')->defaultValue('Y-m-d')->end() + ->arrayNode('tables')->scalarPrototype()->end()->end() ->end() ->end() ->end() From 3977ab9580b951f8cd328a31953a2be39e8555bc Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 11:20:00 +0000 Subject: [PATCH 02/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 5 ++++- src/Model/Backup/Strategy.php | 8 +++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index 0553c34..dd89b42 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -114,7 +114,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int ; $io->info(sprintf('The backup %s is in progress', $backupName)); - + + var_dump( $backup->getStrategy()->getTables() ); + return Command::SUCCESS; + foreach ($connection->getDatabases() as $database) { if ($output->isVerbose()) { $io->comment("Backup for $database database has started"); diff --git a/src/Model/Backup/Strategy.php b/src/Model/Backup/Strategy.php index 75a98bb..a4065d7 100644 --- a/src/Model/Backup/Strategy.php +++ b/src/Model/Backup/Strategy.php @@ -9,7 +9,8 @@ class Strategy 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 ?array $tables = [] ) { } @@ -27,4 +28,9 @@ public function getDateFormat(): ?string { return $this->dateFormat; } + + public function getTables(): ?array + { + return $this->tables; + } } From b054e662487717842c006dd67e35e47ae250088f Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 11:37:33 +0000 Subject: [PATCH 03/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index dd89b42..d3353c5 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -112,11 +112,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int getcwd() : throw new RuntimeException('Unable to get the current directory, check the user permissions') ; - - $io->info(sprintf('The backup %s is in progress', $backupName)); - var_dump( $backup->getStrategy()->getTables() ); - return Command::SUCCESS; + $backupTables = $backup->getStrategy()->getTables(); + + $io->info(sprintf('The backup %s is in progress', $backupName)); foreach ($connection->getDatabases() as $database) { if ($output->isVerbose()) { @@ -127,7 +126,7 @@ 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}"' + '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME} "${:DB_NAME}" "${:DB_TABLES}" > "${:FILEPATH}"' ); $process->setPty(Process::isPtySupported()); @@ -137,6 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'DB_HOST' => $connection->getHost(), 'DB_PORT' => $connection->getPort(), 'DB_NAME' => $database, + 'DB_TABLES' => \implode( ' ', $backupTables ), 'MYSQL_PWD' => $connection->getPassword(), 'FILEPATH' => $filePath, ]); From e67f9495eb36c53fc4bb8610e62c4448b8f50e33 Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 11:47:25 +0000 Subject: [PATCH 04/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index d3353c5..8f277f0 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -128,6 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $process = Process::fromShellCommandline( '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME} "${:DB_NAME}" "${:DB_TABLES}" > "${:FILEPATH}"' ); + var_dump( $process->getCommandLine() ); return Command::SUCCESS; $process->setPty(Process::isPtySupported()); $process->run(null, [ From f7033728cfd13a2a077552929c9e8ad394dd606e Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 12:04:52 +0000 Subject: [PATCH 05/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index 8f277f0..283c275 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -114,7 +114,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int ; $backupTables = $backup->getStrategy()->getTables(); - + var_dump( \implode( ' ', $backupTables ) ); return Command::SUCCESS; + $io->info(sprintf('The backup %s is in progress', $backupName)); foreach ($connection->getDatabases() as $database) { From d4cda14a42f0de3ce140a376a2cd9ad7c01f0779 Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 12:19:33 +0000 Subject: [PATCH 06/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index 283c275..15b8742 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -114,7 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int ; $backupTables = $backup->getStrategy()->getTables(); - var_dump( \implode( ' ', $backupTables ) ); return Command::SUCCESS; + //var_dump( \implode( ' ', $backupTables ) ); return Command::SUCCESS; $io->info(sprintf('The backup %s is in progress', $backupName)); @@ -129,7 +129,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $process = Process::fromShellCommandline( '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME} "${:DB_NAME}" "${:DB_TABLES}" > "${:FILEPATH}"' ); - var_dump( $process->getCommandLine() ); return Command::SUCCESS; + //var_dump( $process->getCommandLine() ); return Command::SUCCESS; $process->setPty(Process::isPtySupported()); $process->run(null, [ From 7e05689805936f2947ece7db44ecdef394bb907f Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 12:23:11 +0000 Subject: [PATCH 07/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index 15b8742..b104b7c 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -127,7 +127,7 @@ 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} "${:DB_NAME}" "${:DB_TABLES}" > "${:FILEPATH}"' + '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME} "${:DB_NAME}" ${:DB_TABLES} > "${:FILEPATH}"' ); //var_dump( $process->getCommandLine() ); return Command::SUCCESS; From d4d9e561d81aedb8b6d90b7fafc868cd3293d102 Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 12:43:25 +0000 Subject: [PATCH 08/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 32 +++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index b104b7c..a5e3768 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -114,7 +114,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int ; $backupTables = $backup->getStrategy()->getTables(); - //var_dump( \implode( ' ', $backupTables ) ); return Command::SUCCESS; + /* + $shellCommand = '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME} "${:DB_TABLES}" > "${:FILEPATH}"'; + $shellCommandPlaceholders = [ + 'MYSQL_DUMP' => $mysqldump, + 'DB_USER' => $connection->getUser(), + 'DB_HOST' => $connection->getHost(), + 'DB_PORT' => $connection->getPort(), + 'DB_NAME' => $database, + 'MYSQL_PWD' => $connection->getPassword(), + ]; + */ + foreach ( $backupTables as $dbTable ) { + + } $io->info(sprintf('The backup %s is in progress', $backupName)); @@ -127,10 +140,9 @@ 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} "${:DB_NAME}" ${:DB_TABLES} > "${:FILEPATH}"' - ); - //var_dump( $process->getCommandLine() ); return Command::SUCCESS; - + '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME}" "${:DB_TABLES}" > "${:FILEPATH}"' + ); + $process->setPty(Process::isPtySupported()); $process->run(null, [ 'MYSQL_DUMP' => $mysqldump, @@ -138,11 +150,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'DB_HOST' => $connection->getHost(), 'DB_PORT' => $connection->getPort(), 'DB_NAME' => $database, - 'DB_TABLES' => \implode( ' ', $backupTables ), + 'DB_TABLES' => \implode('', $backupTables), 'MYSQL_PWD' => $connection->getPassword(), 'FILEPATH' => $filePath, ]); + + /* + $process = Process::fromShellCommandline($shellCommand . ' > "${:FILEPATH}"'); + $shellCommandPlaceholders['FILEPATH'] = $filePath; + $process->setPty(Process::isPtySupported()); + $process->run(null, $shellCommandPlaceholders); + */ + if (!$process->isSuccessful()) { $message = '' !== $process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput(); From b79ddfdf5329c9882539959414a963161d04038c Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 12:46:58 +0000 Subject: [PATCH 09/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index a5e3768..f82e1c2 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -150,7 +150,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'DB_HOST' => $connection->getHost(), 'DB_PORT' => $connection->getPort(), 'DB_NAME' => $database, - 'DB_TABLES' => \implode('', $backupTables), + 'DB_TABLES' => \implode(' ', $backupTables), 'MYSQL_PWD' => $connection->getPassword(), 'FILEPATH' => $filePath, ]); From b293dbd844d726056387f9d4618334d51a4fa3da Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 12:49:06 +0000 Subject: [PATCH 10/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index f82e1c2..f01899c 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -140,7 +140,7 @@ 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}" "${:DB_TABLES}" > "${:FILEPATH}"' + '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME}" ${:DB_TABLES} > "${:FILEPATH}"' ); $process->setPty(Process::isPtySupported()); From d8fc0704f2a22f794d160cd7d8a98ee1033712de Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 12:52:41 +0000 Subject: [PATCH 11/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index f01899c..5914858 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -140,7 +140,7 @@ 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}" ${:DB_TABLES} > "${:FILEPATH}"' + '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME}" "${:DB_TABLES}" > "${:FILEPATH}"' ); $process->setPty(Process::isPtySupported()); @@ -150,7 +150,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'DB_HOST' => $connection->getHost(), 'DB_PORT' => $connection->getPort(), 'DB_NAME' => $database, - 'DB_TABLES' => \implode(' ', $backupTables), + 'DB_TABLES' => \implode('" "', $backupTables), 'MYSQL_PWD' => $connection->getPassword(), 'FILEPATH' => $filePath, ]); From fe3c177e8581b9b2d21f092aeb3476caf81c39c4 Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 13:00:17 +0000 Subject: [PATCH 12/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index 5914858..f82e1c2 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -150,7 +150,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'DB_HOST' => $connection->getHost(), 'DB_PORT' => $connection->getPort(), 'DB_NAME' => $database, - 'DB_TABLES' => \implode('" "', $backupTables), + 'DB_TABLES' => \implode(' ', $backupTables), 'MYSQL_PWD' => $connection->getPassword(), 'FILEPATH' => $filePath, ]); From ebf698310c4ab91049738e73af38f21b88ce8f32 Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 13:10:20 +0000 Subject: [PATCH 13/21] Use Tables Bundle Config Option in Backup Command. --- src/Command/BackupDatabasesCommand.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index f82e1c2..22bc018 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -140,8 +140,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}" "${:DB_TABLES}" > "${:FILEPATH}"' - ); + \sprintf( + '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME}" %s > "${:FILEPATH}"', + \implode(' ', $backupTables) + ) + ); $process->setPty(Process::isPtySupported()); $process->run(null, [ @@ -150,19 +153,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'DB_HOST' => $connection->getHost(), 'DB_PORT' => $connection->getPort(), 'DB_NAME' => $database, - 'DB_TABLES' => \implode(' ', $backupTables), 'MYSQL_PWD' => $connection->getPassword(), 'FILEPATH' => $filePath, ]); - /* - $process = Process::fromShellCommandline($shellCommand . ' > "${:FILEPATH}"'); - $shellCommandPlaceholders['FILEPATH'] = $filePath; - - $process->setPty(Process::isPtySupported()); - $process->run(null, $shellCommandPlaceholders); - */ - if (!$process->isSuccessful()) { $message = '' !== $process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput(); From 2b6e24f0529d46d8d0e8ec2a64980d576564d7c2 Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 13:45:42 +0000 Subject: [PATCH 14/21] Add dumpOnlyData Strategy Config and Use It in Backup Command. --- src/Command/BackupDatabasesCommand.php | 18 +++--------------- src/DependencyInjection/Configuration.php | 1 + src/Model/Backup/Strategy.php | 6 ++++++ 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/Command/BackupDatabasesCommand.php b/src/Command/BackupDatabasesCommand.php index 22bc018..4463ecc 100644 --- a/src/Command/BackupDatabasesCommand.php +++ b/src/Command/BackupDatabasesCommand.php @@ -113,21 +113,8 @@ 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(); - /* - $shellCommand = '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME} "${:DB_TABLES}" > "${:FILEPATH}"'; - $shellCommandPlaceholders = [ - 'MYSQL_DUMP' => $mysqldump, - 'DB_USER' => $connection->getUser(), - 'DB_HOST' => $connection->getHost(), - 'DB_PORT' => $connection->getPort(), - 'DB_NAME' => $database, - 'MYSQL_PWD' => $connection->getPassword(), - ]; - */ - foreach ( $backupTables as $dbTable ) { - - } $io->info(sprintf('The backup %s is in progress', $backupName)); @@ -141,7 +128,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int $process = Process::fromShellCommandline( \sprintf( - '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" "${:DB_NAME}" %s > "${:FILEPATH}"', + '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" %s "${:DB_NAME}" %s > "${:FILEPATH}"', + $dumpOnlyData ? '--no-create-info' : '', \implode(' ', $backupTables) ) ); diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index a543fa6..012e723 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -55,6 +55,7 @@ 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()->end() ->end() ->end() diff --git a/src/Model/Backup/Strategy.php b/src/Model/Backup/Strategy.php index a4065d7..1789782 100644 --- a/src/Model/Backup/Strategy.php +++ b/src/Model/Backup/Strategy.php @@ -10,6 +10,7 @@ public function __construct( private readonly ?int $maxFiles = null, private readonly ?string $backupDirectory = null, private readonly ?string $dateFormat = 'Y-m-d', + private readonly ?array $onlyData = false, private readonly ?array $tables = [] ) { } @@ -29,6 +30,11 @@ public function getDateFormat(): ?string return $this->dateFormat; } + public function getOnlyData(): ?bool + { + return $this->onlyData; + } + public function getTables(): ?array { return $this->tables; From 9e988dbccaf203024d8217004ae919b21b8e8478 Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 13:51:11 +0000 Subject: [PATCH 15/21] Add dumpOnlyData Strategy Config and Use It in Backup Command. --- src/Model/Backup/Strategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Backup/Strategy.php b/src/Model/Backup/Strategy.php index 1789782..5717637 100644 --- a/src/Model/Backup/Strategy.php +++ b/src/Model/Backup/Strategy.php @@ -10,7 +10,7 @@ public function __construct( private readonly ?int $maxFiles = null, private readonly ?string $backupDirectory = null, private readonly ?string $dateFormat = 'Y-m-d', - private readonly ?array $onlyData = false, + private readonly ?bool $onlyData = false, private readonly ?array $tables = [] ) { } From 238293028a85ec0cc2b954472b7443823e4e4d6d Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Fri, 10 Jan 2025 14:07:12 +0000 Subject: [PATCH 16/21] Add Howto Use New Options in Readme. --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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: From fd4a120fa7382870b90f99b007e67e77dd74e48b Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Sat, 22 Feb 2025 03:15:13 +0000 Subject: [PATCH 17/21] Try to fix PhpStan Error. --- src/Model/Backup/Strategy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Backup/Strategy.php b/src/Model/Backup/Strategy.php index 5717637..8815f26 100644 --- a/src/Model/Backup/Strategy.php +++ b/src/Model/Backup/Strategy.php @@ -11,7 +11,7 @@ public function __construct( private readonly ?string $backupDirectory = null, private readonly ?string $dateFormat = 'Y-m-d', private readonly ?bool $onlyData = false, - private readonly ?array $tables = [] + private readonly ?array $tables = null ) { } From c68d06a62f046aee38cce04482368d05a11b298a Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Sat, 22 Feb 2025 03:35:41 +0000 Subject: [PATCH 18/21] Try to fix PhpStan Error. --- src/DependencyInjection/Configuration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 012e723..03e4ce2 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -56,7 +56,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->scalarNode('backup_directory')->isRequired()->defaultNull()->end() ->scalarNode('date_format')->defaultValue('Y-m-d')->end() ->booleanNode('only_data')->defaultFalse()->end() - ->arrayNode('tables')->scalarPrototype()->end()->end() + ->arrayNode('tables')->scalarPrototype()->end()->defaultValue([])->end() ->end() ->end() ->end() From a21a95a08c308dd1e396b565d7c65301e5ddd8a1 Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Tue, 25 Feb 2025 10:36:47 +0000 Subject: [PATCH 19/21] Try to fix PhpStan Error. --- src/Model/Backup/Strategy.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Model/Backup/Strategy.php b/src/Model/Backup/Strategy.php index 8815f26..749fc64 100644 --- a/src/Model/Backup/Strategy.php +++ b/src/Model/Backup/Strategy.php @@ -6,6 +6,15 @@ class Strategy { + /** + * Default Constructor + * + * @param int $maxFiles + * @param string $backupDirectory + * @param string $dateFormat + * @param bool $onlyData + * @param array $tables + */ public function __construct( private readonly ?int $maxFiles = null, private readonly ?string $backupDirectory = null, @@ -35,6 +44,7 @@ public function getOnlyData(): ?bool return $this->onlyData; } + /** @return array */ public function getTables(): ?array { return $this->tables; From 80ef3c3cc974d86fe7f123f5554e0cec337e683d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sama=C3=ABl=20Villette?= Date: Sun, 9 Mar 2025 17:29:35 +0100 Subject: [PATCH 20/21] chore: fix code style and static analysis --- composer.json | 2 +- src/Command/BackupDatabasesCommand.php | 15 ++++++++------- src/Factory/Backup/BackupFactory.php | 2 +- src/Model/Backup/Backup.php | 2 +- src/Model/Backup/Strategy.php | 16 +++++----------- src/Model/Connection/ConnectionDriver.php | 2 +- src/Model/Connection/MySQLConnection.php | 2 +- 7 files changed, 18 insertions(+), 23 deletions(-) diff --git a/composer.json b/composer.json index 6d5a071..cb8dc41 100644 --- a/composer.json +++ b/composer.json @@ -42,7 +42,7 @@ "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 4463ecc..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; @@ -112,12 +113,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int getcwd() : 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) { if ($output->isVerbose()) { $io->comment("Backup for $database database has started"); @@ -127,13 +128,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int $filePath = "$backupDirectory/$backupName-$database-$date.sql"; $process = Process::fromShellCommandline( - \sprintf( + sprintf( '"${:MYSQL_DUMP}" -u "${:DB_USER}" -h "${:DB_HOST}" -P "${:DB_PORT}" %s "${:DB_NAME}" %s > "${:FILEPATH}"', $dumpOnlyData ? '--no-create-info' : '', - \implode(' ', $backupTables) + implode(' ', $backupTables) ) ); - + $process->setPty(Process::isPtySupported()); $process->run(null, [ 'MYSQL_DUMP' => $mysqldump, @@ -144,7 +145,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int 'MYSQL_PWD' => $connection->getPassword(), 'FILEPATH' => $filePath, ]); - + if (!$process->isSuccessful()) { $message = '' !== $process->getErrorOutput() ? $process->getErrorOutput() : $process->getOutput(); 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 749fc64..41fdc0c 100644 --- a/src/Model/Backup/Strategy.php +++ b/src/Model/Backup/Strategy.php @@ -7,20 +7,14 @@ class Strategy { /** - * Default Constructor - * - * @param int $maxFiles - * @param string $backupDirectory - * @param string $dateFormat - * @param bool $onlyData - * @param array $tables + * @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 ?bool $onlyData = false, - private readonly ?array $tables = null + private readonly ?array $tables = null, ) { } @@ -38,13 +32,13 @@ public function getDateFormat(): ?string { return $this->dateFormat; } - + public function getOnlyData(): ?bool { return $this->onlyData; } - - /** @return array */ + + /** @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 = [], ) { } From 538b8add47409f44212ad9ad66ae43cb53b760e7 Mon Sep 17 00:00:00 2001 From: "Ivan I. Atanasov" Date: Sat, 6 Dec 2025 06:14:42 +0000 Subject: [PATCH 21/21] Add Support for Symfony 7. --- composer.json | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 6d5a071..4464c42 100644 --- a/composer.json +++ b/composer.json @@ -21,23 +21,23 @@ ], "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" },