Skip to content

Commit

Permalink
no issue - much better configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
pounard committed Nov 15, 2024
1 parent f31402f commit a252b57
Show file tree
Hide file tree
Showing 39 changed files with 1,082 additions and 664 deletions.
150 changes: 64 additions & 86 deletions config/db_tools.standalone.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,104 +9,82 @@

# Working directory is the path to which all relative file references will
# be relative to. If none set, the path will be this file directory instead.
workdir: /var/www/my_project/
# In most cases, you will not need it.
#workdir: /var/www/my_project/

# Database connections.
# One line per connection, a single database URL, all options as query
# parameters. Connections will be made using makincorpus/query-builder
# which will raise exceptions when invalid options are found.
# There is less configuration amplitude than using doctrine/dbal in
# Symfony, yet it should be enough in most case.
# In case any options or specific behaviour is missing, please file
# an issue at https://github.com/makinacorpus/php-query-builder/issues
connections:
connection_one: "pgsql://username:password@hostname:port?version=16.0&other_option=..."
connection_two: "mysql://username:password@hostname:port?version=8.1&other_option=..."
# Where to put generated backups.
# Root directory of the backup storage manager. Default filename
# strategy will always use this folder as root path.
storage_directory: ./var/db_tools

# If you have a single connection, you can use this syntax. In this case
# the connection name will be "default".
# connections: "pgsql://username:password@hostname:port?version=16.0&other_option=..."

# You can explicitely set which will be default connection in use when
# none providen in the command line options. If you omit this configuration
# value, then the first one in the list will be used.
#default_connection: connection_one

# Using the DbToolsBundle standalone, you must provide at least
# a root directory for backups.
storage:
# Path can be relative or absolute, Relative paths are relative to the
# workdir option if specified, or from this configuration file directory
# otherwise.
# If none provided, the default will be the following one.
root_dir: ./var/db_tools
# Filename strategies. You may specify one strategy for each doctrine
# connection. Keys are doctrine connection names. Values are strategy
# names, "default" (or null) or omitting the connection will use the
# default implementation.
# If you created and registered a custom one into the container as a
# service, you may simply set the service identifier. If no service
# exists, and your implementation does not require parameters, simply
# set the class name.
#filename_strategy:
# Backup filename strategy.
# "default" is an alias of "datetime"
#default: default
# "datetime" implementation is ROOT_DIR/YYYY/MM/<connection-name>-<datestamp>.<ext>"
#other_connection_strategy: datetime
# Example of using a service name:
#yet_another_connection: app.db_tools.filename.custom_strategy
# Or a classe name:
#another_one: App\DbTools\Storage\MyCustomStrategy
# Filename strategies. You may specify one strategy for each doctrine
# connection. Keys are doctrine connection names. Values are strategy
# names, "default" (or null) or omitting the connection will use the
# default implementation.
# If you created and registered a custom one into the container as a
# service, you may simply set the service identifier. If no service
# exists, and your implementation does not require parameters, simply
# set the class name.
# Allowed values are:
# - "default": alias of "datetime".
# - "datetime": implementation is "%db_tools.storage.root_dir%/YYYY/MM/<connection-name>-<datestamp>.<ext>".
# - CLASS_NAME: a class name to use that implements a strategy.
# - SERVICE_ID: in Symfony context, a service registered in container that implements a strategy.
#storage_filename_strategy: default

# When old backups are considered obsolete
# When old backups are considered obsolete.
# (Use relative date/time formats : https://www.php.net/manual/en/datetime.formats.relative.php)
#backup_expiration_age: '6 months ago' # default '3 months ago'

# Timeout for backups.
# backup_timeout: 1200 # default 600
#backup_timeout: 1200 # default 600

# Timeout for restores.
# restore_timeout: 2400 # default 1800
#restore_timeout: 2400 # default 1800

# List here tables (per connection) you don't want in your backups
#excluded_tables:
#default: ['table1', 'table2']
# List here tables (you don't want in your backups.
#backup_excluded_tables: ['table1', 'table2']

# Specify here paths to binaries, only if the system can't find them by himself
# platform are 'mysql', 'postgresql', 'sqlite'
#backupper_binaries:
#mariadb: '/usr/bin/mariadb-dump' # default 'mariadb-dump'
#mysql: '/usr/bin/mysqldump' # default 'mysqldump'
#postgresql: '/usr/bin/pg_dump' # default 'pg_dump'
#sqlite: '/usr/bin/sqlite3' # default 'sqlite3'
#restorer_binaries:
#mariadb: '/usr/bin/mariadb' # default 'mariadb'
#mysql: '/usr/bin/mysql' # default 'mysql'
#postgresql: '/usr/bin/pg_restore' # default 'pg_restore'
#sqlite: '/usr/bin/sqlite3' # default 'sqlite3'
# Specify here paths to binaries and options, only if the system can't find
# them by himself.
# Default value depends upon vendor and is documented at
# https://dbtoolsbundle.readthedocs.io/en/stable/configuration.html
#backup_binary: '/usr/bin/pg_dump'
#backup_options: '-Z 5 --lock-wait-timeout=120'
#restore_binary: '/usr/bin/pg_restore'
#restore_options: '-j 2 --clean --if-exists --disable-triggers'

# Default options to pass to the binary when backing up or restoring
# a database. Those options must be defined per connection.
# If you do not define some default options, here or by using the
# "--extra-options" option when invoking the command, the following
# ones will be used according to the database vendor:
# - When backing up:
# - MariaDB: --no-tablespaces
# - MySQL: --no-tablespaces
# - PostgreSQL: -Z 5 --lock-wait-timeout=120
# - SQLite: -bail
# - When restoring:
# - MariaDB: None
# - MySQL: None
# - PostgreSQL: -j 2 --clean --if-exists --disable-triggers
# - SQLite: None
#backupper_options:
#default: ''
#another_connection: ''
#restorer_options:
#default: ''
#another_connection: ''
# If you have a single connection, you can use this syntax. In this case
# the connection name will be "default".
connections: "pgsql://username:password@hostname:port?version=16.0&other_option=..."

# Alternatively, you are allowed to have more than one connections, case
# in which you might synthesize the configuration as this:
#connections:
# connection_one: "pgsql://username:password@hostname:port?version=16.0&other_option=..."
# connection_two: "mysql://username:password@hostname:port?version=8.1&other_option=..."

# For advanced usage, you may also override any parameter at the connection
# Each key is a connection name, all parameters above are allowed for each
# unique connection.
#connections:
# connection_one:
# # Connection URL for connecting.
# # Please refer to makinacorpus/db-query-builder documentation for more information.
# # Any URL built for doctrine/dbal usage should work.
# # URL is the only mandatory parameter.
# url: "pgsql://username:password@hostname:port?version=16.0&other_option=..."
# #backup_expiration_age: '3 month ago'
# # ...
# connection_two:
# url: "mysql://username:password@hostname:port?version=8.1&other_option=..."
# backup_options: '-Z 5 --lock-wait-timeout=60'
# restore_options: '-j 3 --clean'

# You can explicitely set which will be default connection in use when
# none providen in the command line options. If you omit this configuration
# value, then the first one in the list will be used.
#default_connection: connection_one

# Update this configuration if you want to look for anonymizers in a custom folder.
# These are default paths that will always be registered even if you override
Expand Down
107 changes: 46 additions & 61 deletions config/packages/db_tools.yaml
Original file line number Diff line number Diff line change
@@ -1,77 +1,62 @@
db_tools:
# Where to put generated backups.
#storage:
# Root directory of the backup storage manager. Default filename
# strategy will always use this folder as root path.
#root_dir: '%kernel.project_dir%/var/db_tools'
# Root directory of the backup storage manager. Default filename
# strategy will always use this folder as root path.
storage_directory: '%kernel.project_dir%/var/db_tools'

# Filename strategies. You may specify one strategy for each doctrine
# connection. Keys are doctrine connection names. Values are strategy
# names, "default" (or null) or omitting the connection will use the
# default implementation.
# If you created and registered a custom one into the container as a
# service, you may simply set the service identifier. If no service
# exists, and your implementation does not require parameters, simply
# set the class name.
#filename_strategy:
# Backup filename strategy.
# "default" is an alias of "datetime"
#default: default
# "datetime" implementation is "%db_tools.storage.root_dir%/YYYY/MM/<connection-name>-<datestamp>.<ext>"
#other_connection_strategy: datetime
# Example of using a service name:
#yet_another_connection: app.db_tools.filename.custom_strategy
# Or a classe name:
#another_one: App\DbTools\Storage\MyCustomStrategy
# Filename strategies. You may specify one strategy for each doctrine
# connection. Keys are doctrine connection names. Values are strategy
# names, "default" (or null) or omitting the connection will use the
# default implementation.
# If you created and registered a custom one into the container as a
# service, you may simply set the service identifier. If no service
# exists, and your implementation does not require parameters, simply
# set the class name.
# Allowed values are:
# - "default": alias of "datetime".
# - "datetime": implementation is "%db_tools.storage.root_dir%/YYYY/MM/<connection-name>-<datestamp>.<ext>".
# - CLASS_NAME: a class name to use that implements a strategy.
# - SERVICE_ID: in Symfony context, a service registered in container that implements a strategy.
#storage_filename_strategy: default

# When old backups are considered obsolete
# When old backups are considered obsolete.
# (Use relative date/time formats : https://www.php.net/manual/en/datetime.formats.relative.php)
#backup_expiration_age: '6 months ago' # default '3 months ago'

# Timeout for backups.
# backup_timeout: 1200 # default 600
#backup_timeout: 1200 # default 600

# Timeout for restores.
# restore_timeout: 2400 # default 1800
#restore_timeout: 2400 # default 1800

# List here tables (per connection) you don't want in your backups
#excluded_tables:
#default: ['table1', 'table2']
# List here tables (you don't want in your backups.
#backup_excluded_tables: ['table1', 'table2']

# Specify here paths to binaries, only if the system can't find them by himself
# platform are 'mysql', 'postgresql', 'sqlite'
#backupper_binaries:
#mariadb: '/usr/bin/mariadb-dump' # default 'mariadb-dump'
#mysql: '/usr/bin/mysqldump' # default 'mysqldump'
#postgresql: '/usr/bin/pg_dump' # default 'pg_dump'
#sqlite: '/usr/bin/sqlite3' # default 'sqlite3'
#restorer_binaries:
#mariadb: '/usr/bin/mariadb' # default 'mariadb'
#mysql: '/usr/bin/mysql' # default 'mysql'
#postgresql: '/usr/bin/pg_restore' # default 'pg_restore'
#sqlite: '/usr/bin/sqlite3' # default 'sqlite3'
# Specify here paths to binaries and options, only if the system can't find
# them by himself.
# Default value depends upon vendor and is documented at
# https://dbtoolsbundle.readthedocs.io/en/stable/configuration.html
#backup_binary: '/usr/bin/pg_dump'
#backup_options: '-Z 5 --lock-wait-timeout=120'
#restore_binary: '/usr/bin/pg_restore'
#restore_options: '-j 2 --clean --if-exists --disable-triggers'

# Default options to pass to the binary when backing up or restoring
# a database. Those options must be defined per connection.
# If you do not define some default options, here or by using the
# "--extra-options" option when invoking the command, the following
# ones will be used according to the database vendor:
# - When backing up:
# - MariaDB: --no-tablespaces
# - MySQL: --no-tablespaces
# - PostgreSQL: -Z 5 --lock-wait-timeout=120
# - SQLite: -bail
# - When restoring:
# - MariaDB: None
# - MySQL: None
# - PostgreSQL: -j 2 --clean --if-exists --disable-triggers
# - SQLite: None
#backupper_options:
#default: ''
#another_connection: ''
#restorer_options:
#default: ''
#another_connection: ''
# You may specify a list of connections with varying configuration on
# a per-connection basis.
# Each key is a doctrine connection name, all values upper are allowed
# for each unique connection.
#connections:
# connection_one:
# backup_expiration_age: '3 month ago'
# # ...
# connection_two:
# backup_options: '-Z 5 --lock-wait-timeout=60'
# restore_options: '-j 3 --clean'

# You can explicitely set which will be default connection in use when
# none providen in the command line options. If you omit this configuration
# value, then the first one in the list will be used.
#default_connection: connection_one

# Update this configuration if you want to look for anonymizers in a custom folder.
# These are default paths that will always be registered even if you override
Expand Down
21 changes: 15 additions & 6 deletions src/Backupper/AbstractBackupper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MakinaCorpus\DbToolsBundle\Backupper;

use MakinaCorpus\DbToolsBundle\Configuration\Configuration;
use MakinaCorpus\DbToolsBundle\Helper\Output\NullOutput;
use MakinaCorpus\DbToolsBundle\Helper\Process\CommandLine;
use MakinaCorpus\DbToolsBundle\Helper\Process\ProcessTrait;
Expand All @@ -22,21 +23,24 @@ abstract class AbstractBackupper implements LoggerAwareInterface
{
use ProcessTrait;

protected string $binary;
protected ?string $destination = null;
protected string $defaultOptions = '';
protected ?string $extraOptions = null;
protected bool $ignoreDefaultOptions = false;
protected array $excludedTables = [];
protected bool $verbose = false;
protected ?int $timeout = 600;
protected ?int $timeout = null;

public function __construct(
protected string $binary,
protected DatabaseSession $databaseSession,
protected Dsn $databaseDsn,
?string $defaultOptions = null,
protected readonly DatabaseSession $databaseSession,
protected readonly Dsn $databaseDsn,
protected readonly Configuration $config,
) {
$this->defaultOptions = $defaultOptions ?? $this->getBuiltinDefaultOptions();
$this->excludedTables = $config->getBackupExcludedTables();
$this->binary = $config->getBackupBinary() ?? $this->getDefaultBinary();
$this->defaultOptions = $config->getBackupOptions() ?? $this->getBuiltinDefaultOptions();
$this->timeout = $config->getBackupTimeout();

$this->destination = \sprintf(
'%s/db-tools-backup-%s.dump',
Expand Down Expand Up @@ -161,6 +165,11 @@ protected function beforeProcess(): void
$this->process->setTimeout(null === $this->timeout ? null : (float) $this->timeout);
}

/**
* Get default binary path and name (e.g. "/usr/bin/foosql-backup").
*/
abstract protected function getDefaultBinary(): string;

/**
* Provide the built-in default options that will be used if none is given
* through the dedicated constructor argument.
Expand Down
Loading

0 comments on commit a252b57

Please sign in to comment.