portrino / database-export-anonymized
Anonymized Database Export - Exports the TYPO3 database to stdout like database:export, with personal data replaced by fake data
Package info
github.com/portrino/database-export-anonymized
Type:typo3-cms-extension
pkg:composer/portrino/database-export-anonymized
Requires
- php: ^8.2
- ext-pdo: *
- ext-pdo_mysql: *
- druidfi/mysqldump-php: ^2.0
- fakerphp/faker: ^1.23
- symfony/console: ^6.4 || ^7.0
- typo3/cms-core: ^12.4 || ^13.4 || ^14.3
Requires (Dev)
- ext-mysqli: *
- a9f/fractor-composer-json: ^1.0
- a9f/typo3-fractor: ^1.0
- armin/editorconfig-cli: ^2.2
- ergebnis/composer-normalize: ^2.30
- friendsofphp/php-cs-fixer: ^3.14
- helmich/typo3-typoscript-lint: ^3.1
- phpstan/phpstan: ^2.0
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-strict-rules: ^2.0
- phpunit/phpunit: ^10.5 || ^11.5
- rector/rector: ~2.8.5
- seld/jsonlint: ^1.9
- shipmonk/composer-dependency-analyser: ^1.8
- spaze/phpstan-disallowed-calls: ^4.0
- ssch/typo3-rector: ^3.0
- tomasvotruba/cognitive-complexity: ^1.0
- tomasvotruba/type-coverage: ^2.0
- typo3/cms-felogin: ^12.4 || ^13.4 || ^14.3
- typo3/coding-standards: ^0.9
- typo3/testing-framework: ^8.2 || ^9.7
Suggests
- johnykvsky/quidam: Seedable fake data generator for PHP 8.4+, used instead of fakerphp/faker once an adapter exists
Provides
None
Conflicts
None
Replaces
None
README
This extension provides the command database:export-anonymized. It exports the TYPO3 database to stdout like
database:export of the TYPO3 Console, but replaces personal data with fake data while it streams. A database
pulled from production to a developer machine holds no real customer data.
Important
Without a config file only the typo3-core preset is applied (with a warning on stderr): personal data in tables
of other extensions is then exported unchanged. Create a database-export-anonymized.php for every project and
make sure your deployment copies it to the server.
π₯ Installation
Composer
The extension can only be installed using composer, it depends on
druidfi/mysqldump-php and fakerphp/faker. Classic mode (non-Composer) installations are not supported.
Run the following command within your Composer based TYPO3 project:
composer require portrino/database-export-anonymized
The command runs on production, so require it as a regular (non-dev) dependency. It needs ext-pdo_mysql, even if
TYPO3 uses the mysqli driver, and works with MySQL and MariaDB only.
Supported are TYPO3 12.4, 13.4 and 14.3 on PHP 8.2 β 8.5 (TYPO3 12: up to PHP 8.4).
π§ Configuration
Put database-export-anonymized.php into the project root (next to composer.json) or into config/system/
(next to settings.php). If both exist, the one in the project root is used:
<?php declare(strict_types=1); use Portrino\DatabaseExportAnonymized\Core\Config\AnonymizerConfig; use Portrino\DatabaseExportAnonymized\Core\Config\TableRule; use Portrino\DatabaseExportAnonymized\Preset\Typo3CorePreset; return AnonymizerConfig::create() ->withLocale('de_DE') ->withPresets(new Typo3CorePreset()) ->exclude('tx_myext_import_log', 'tx_*_tmp') // not exported at all ->excludeData('tx_solr_*', 'tx_crawler_queue') // structure only // agency accounts keep their real data (presets never skip rows) ->table('fe_users', fn (TableRule $t): TableRule => $t ->skipIf(fn (array $row): bool => str_ends_with((string)$row['email'], '@agency.example'))) ->table('be_users', fn (TableRule $t): TableRule => $t ->skipIf(fn (array $row): bool => str_ends_with((string)$row['email'], '@agency.example'))) // Key-value table with one row per customer attribute, e.g. attribute = 'email', value = 'jane@example.com': // "value" is only replaced in rows holding an email address or a phone number, other rows stay unchanged ->table('tx_shop_customer_attribute', fn (TableRule $t): TableRule => $t ->column('value', 'safeEmail', group: 'email', when: fn (array $row): bool => $row['attribute'] === 'email') ->column('value', 'phoneNumber', group: 'phone', when: fn (array $row): bool => $row['attribute'] === 'phone'));
Column rules
column(string $column, $type = null, bool $unique = false, ?string $group = null, $fixed = null, ?Closure $when = null)
$typeβ one offirstName,lastName,name,userName,safeEmail,phoneNumber,streetAddress,postcode,city,company,url,iban,ipv4,sentence, or a closurefn (array $row, array $original): stringthat derives the value.$rowholds the columns of earlier rules already replaced,$originalthe row as read from the database.fixedβ a fixed value instead, e.g.''to empty a column or a known password hashwhenβfn (array $row, array $original): bool, restricts the rule to some rows. Rules of a column run in definition order and the first matching one wins, so put a rule withoutwhenlast.uniqueβ keeps the fake values of the group unique (e.g. for usernames)groupβ equal original values in the same group get the same fake value, in all tables and in every export. It defaults to the type; useemail,phone,firstNameandlastNamelike the preset.
skipIf(fn (array $row): bool) exports matching rows unchanged; rows can't be dropped from the dump.
Good to know:
NULLand empty values stay unchanged.- Rules of the project config for a column replace all preset rules of that column.
- A rule on a column that doesn't exist is reported as warning and ignored (rules of the preset silently).
- Binary columns (
blob,binaryβ¦) can only be emptied withfixed: '', numeric columns only take an int asfixedvalue. Other rules there stop the export before it starts.
The typo3-core preset
fe_users,be_users(admins included, TYPO3's_cli_user excepted): names, email, username, address, phone, company,wwwanddescriptionare replaced. A username that was the email address becomes the new fake email address.- The password of every user becomes
Password1!(Typo3CorePreset::PASSWORD_HASH). - Emptied:
mfa(TOTP secrets),be_users.password_reset_token,fe_users.felogin_forgotHash. - Exported without rows:
cf_*,cache_*,[bf]e_sessions,sys_history,sys_log,sys_refindex,sys_lockedrecords,sys_http_report.
Tip
For powermail, tt_address and femanager there are example rules. Copy the ones you need into your config and check them against your installation.
π Run
vendor/bin/typo3 database:export-anonymized > dump.sql vendor/bin/typo3 database:export-anonymized -e 'tx_myext_log*' --exclude-data 'tx_solr_*' | gzip > dump.sql.gz
Options:
-c, --connectionβ TYPO3 database connection, defaultDefault-e, --excludeβ table left out completely, repeatable; wildcards likedatabase:exportof the TYPO3 Console (fnmatch():cf_*,fe_session?,[bf]e_sessions)--exclude-dataβ table dumped without its rows, same wildcards, repeatable--configβ config file; without it<project root>/database-export-anonymized.phpis used, otherwiseconfig/system/database-export-anonymized.php
Only the dump goes to stdout. stderr shows the config file used (or the warning that none was found), warnings and
errors. On errors the exit code is 1, so use set -o pipefail when you pipe the dump.
There is no option for an export without anonymization, use database:export for that.
DDEV pull
As db_pull_command of a DDEV provider:
set -o pipefail ssh -C -p ${remoteServerPort} "${remoteServer}" "${remotePhpBinary} ${remoteProjectRoot}/bin/typo3 database:export-anonymized" | gzip > /var/www/html/.ddev/.downloads/db.sql.gz
β οΈ Limitations
- Fake values are reproducible for the same Faker version, locale and PHP version range: Faker seeds differently below and from PHP 8.3 on, so a Faker update or a PHP 8.2 β 8.3 upgrade changes them.
- Serialized or JSON values (e.g.
be_users.uc, powermail checkbox answers) can only be replaced as a whole. - Tables mapped to another connection (
TableMapping) are only part of an export of that connection. - Every value is generated separately, so exports of very large tables take longer than with
database:export.
π§βπ» Contributing
Please have a look at CONTRIBUTING.md. Development setup and conventions are described in
AGENTS.md.