DbToolsBundle

repository·main·Indexed 42 days ago

https://github.com/makinacorpus/dbtoolsbundle

A PHP toolset for backing up, restoring, and anonymizing databases, designed to create GDPR-compliant workflows when moving production data to development or staging environments. It features Symfony integration via Doctrine Entity attributes, YAML-based configuration, and support for MySQL, MariaDB, PostgreSQL, and SQL Server.

Tokens
44.8K
Snippets
161
Records
194
Agent score
53%

What's inside DbToolsBundle

  1. Overview of DbToolsBundle

    main

    DbToolsBundle is a PHP tool designed to simplify frequent database-related tasks for developers. Its core capabilities include:

    • Backup: Creating copies of your databases.
    • Restore: Restoring databases from backups.
    • Anonymize: Protecting sensitive data by anonymizing database contents.

    For DevOps workflows, a Docker image is available to run these tools in containerized environments.

  2. Overview of DbToolsBundle features

    main

    DbToolsBundle is a PHP tool designed for managing database lifecycles. Key capabilities include:

    • Back up: Back up your database and manage dumps via CLI commands.
    • Restore: Restore previous database dumps easily.
    • Anonymize: Configure database anonymization using YAML files.
    • GDPR-friendly workflows: Facilitate GDPR best practices when importing production dumps into non-production environments.
    • Symfony Integration: Provides autoconfiguration and allows for anonymization using PHP attributes on Doctrine Entities.
  3. Choose a DbToolsBundle flavor

    main

    DbToolsBundle is available in four distinct flavors depending on your environment and framework requirements:

    1. Standalone: A pure PHP library usable in any Composer-managed project. Access the binary via vendor/bin/db-tools.
    2. Symfony: A dedicated bundle that integrates deeply with Symfony. It provides autoconfigured database connections via DBAL, Symfony Console commands, and attribute-based anonymization on Doctrine Entities.
    3. Docker: A containerized version based on FrankenPHP, ideal for CI/CD pipelines. Commands are executed via docker container run.
    4. Laravel (experimental): An integration for Laravel projects that provides Artisan commands and autoconfigured connections via the Illuminate database manager.
  4. Find or create extra anonymizer packs

    main

    DbToolsBundle uses a decentralized approach where the core bundle remains lightweight. If the core anonymizers do not meet your needs, you can:

    1. Search for official packs: Check the DbToolsBundle/packs-status repository for the status of official packs.
    2. Search GitHub: Look for community-contributed packs using the topic db-tools-bundle-pack.
    3. Create custom anonymizers: If no existing pack fits, you can implement your own custom anonymizers and share them with the community.
  5. Use the Password Anonymizer to set uniform user passwords

    main

    The password anonymizer allows you to set the same plain-text password for every user in your database. It uses the Symfony PasswordHasher Component to hash the provided plain password before storage.

    Configuration Options

    When configuring the password anonymizer, you can provide the following options:

    • algorithm: The hashing algorithm to use (e.g., 'sodium'). The default is 'auto'.
    • password: The plain-text password that will be set for every row. The default is 'password'.
  6. How DbToolsBundle achieves high performance

    main

    Unlike traditional PHP anonymization tools that follow a three-step process (Load data $\rightarrow$ Anonymize in PHP $\rightarrow$ Persist to DB), DbToolsBundle anonymizes directly with SQL queries.

    By leveraging the database management system (DBMS) to process data, the bundle avoids the overhead of pulling large datasets into PHP memory. The bundle generates optimized, complex update queries tailored specifically for the target database platform (SQLite, PostgreSQL, MariaDB, or MySQL) to ensure maximum efficiency.

  7. Configure the top-level structure for Symfony vs Standalone/Docker

    main

    The configuration structure depends on how you are running the toolset.

    • Symfony Bundle: You must wrap all configuration under a top-level db_tools key to avoid conflicts with other bundles.
    • Standalone Console / Docker: You must omit the db_tools top-level section and place configuration keys at the root of the file.

    Note on Paths:

    • In Symfony, use absolute paths (e.g., using %kernel.project_dir%).
    • In Standalone/Docker, relative paths are relative to the directory of the configuration file itself, unless a workdir is provided.
  8. How connection-level configuration inheritance works

    main

    Most configuration options in DbToolsBundle can be defined at two levels:

    1. Top-level: Applies the setting to all connections.
    2. Connection-level: Applies the setting only to a specific connection defined under the connections key.

    When working with multiple connections, any connection that does not explicitly specify an option will inherit the value from the top-level default configuration.

    # Example of top-level vs connection-level configuration
    db_tools:
        # Top-level default
        backup_excluded_tables: ['table1']
        connections:
            connection_one:
                # Overrides top-level
                backup_excluded_tables: ['table2']
            connection_two:
                # Inherits ['table1'] from top-level
                pass: true
  9. Use the iban-bic anonymizer

    main

    The iban-bic anonymizer is a multi-column anonymizer designed to anonymize an IBAN and a BIC code simultaneously. You can choose to anonymize just the IBAN, just the BIC, or both.

    Available Columns

    • iban: The International Bank Account Number.
    • bic: The Bank Identifier Code.

    Configuration Options

    OptionTypeDescription
    countrystringTwo-letter country code used for generating the IBAN with target country validation rules. Default is fr.
    sample_sizeintThe size of the generated random sample table. Default is 500.

    Implementation Methods

    You can configure this anonymizer using either a global db_tools.config.yaml file, a Symfony-specific config/anonymization.yaml file, or via PHP Attributes on your Doctrine entities.

    # Example configuration structure
    anonymization:
        default:
            customer:
                iban:
                    target: table
                    anonymizer: iban-bic
                    options:
                        iban: 'account_iban'
                        bic: 'account_bic'
  10. Understand backup storage and file naming

    main

    Backups are stored in a directory that can be configured. The default locations are:

    • Standalone: ./var/db_tools (relative to the yaml config file)
    • Symfony: %kernel.project_dir%/var/db_tools
    • Docker: /var/www/var/db_tools (inside the container)

    Files are organized into sub-directories by date. The filename format is {connection_name}-{date}{timestamp}.{extension}.

    Example for a backup of the default connection made on 2023-05-15 at 12:22:35:

    • Symfony path: %kernel.project_dir%/var/db_tools/2023/05/default-20230515122235.sql