Skip to content

Dump Only Specific Tables in Database and Dump Only Data Option #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
f3a3b9b
Add a Tables Bundle Config Option.
iatanasov77 Jan 10, 2025
3977ab9
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
b054e66
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
e67f949
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
f703372
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
d4cda14
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
7e05689
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
d4d9e56
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
b79ddfd
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
b293dbd
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
d8fc070
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
fe3c177
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
ebf6983
Use Tables Bundle Config Option in Backup Command.
iatanasov77 Jan 10, 2025
2b6e24f
Add dumpOnlyData Strategy Config and Use It in Backup Command.
iatanasov77 Jan 10, 2025
9e988db
Add dumpOnlyData Strategy Config and Use It in Backup Command.
iatanasov77 Jan 10, 2025
2382930
Add Howto Use New Options in Readme.
iatanasov77 Jan 10, 2025
fd4a120
Try to fix PhpStan Error.
iatanasov77 Feb 22, 2025
c68d06a
Try to fix PhpStan Error.
iatanasov77 Feb 22, 2025
a21a95a
Try to fix PhpStan Error.
iatanasov77 Feb 25, 2025
80ef3c3
chore: fix code style and static analysis
SVillette Mar 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
10 changes: 9 additions & 1 deletion src/Command/BackupDatabasesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/Backup/BackupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class BackupFactory implements NamedFactoryInterface
*/
public function __construct(
private readonly ConnectionFactory $connectionFactory,
private readonly FactoryInterface $strategyFactory
private readonly FactoryInterface $strategyFactory,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Model/Backup/Backup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand Down
18 changes: 17 additions & 1 deletion src/Model/Backup/Strategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

class Strategy
{
/**
* @param list<string>|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,
) {
}

Expand All @@ -27,4 +32,15 @@ public function getDateFormat(): ?string
{
return $this->dateFormat;
}

public function getOnlyData(): ?bool
{
return $this->onlyData;
}

/** @return list<string>|null */
public function getTables(): ?array
{
return $this->tables;
}
}
2 changes: 1 addition & 1 deletion src/Model/Connection/ConnectionDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ enum ConnectionDriver: string
public function getConnectionClass(): string
{
return match ($this) {
self::MySQL => MySQLConnection::class
self::MySQL => MySQLConnection::class,
};
}
}
2 changes: 1 addition & 1 deletion src/Model/Connection/MySQLConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [],
) {
}

Expand Down
Loading