定制 printi/db-dump-file-anonymizer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

printi/db-dump-file-anonymizer

Composer 安装命令:

composer create-project printi/db-dump-file-anonymizer

包简介

Library to anonymize a MySQL dump file using fake data

README 文档

README

Overview

This PHP library can anonymize a database dump file with random/fake data, making it particularly useful for anonymizing data when restoring a live database in a non-live environment. This helps mitigate the risk of exposing real customer data if the non-live database is stolen, for example.

How it works

It reads a file with the DB dump in the local storage (or from STDIN), then it produces a new DB dump file (or outputs in STDOUT) based on the instructions about which columns should be anonymized.

It is much faster than trying to run multiple UPDATE requests to a running DB, because the DB needs to handle with many things like: checking the uniqueness of some columns, indexing, and stuff like that.

Install

To install this library for usage, just run:

composer create-project printi/db-dump-file-anonymizer

Usage

This library exports an executable script called anonymize-db-dump. It is installed in the folder ./vendor/bin/ when the library is required.

The script can be called this way:

$ ./vendor/bin/anonymize-db-dump [OPTIONS]

These are the available options:

  • -h or --help: to show the help
  • -i FILE or --input=FILE: to inform the input file (dump file of MySQL)
  • --stdin: to read input from STDIN
  • -o FILE or --output=FILE: to inform the output file
  • --stdout: to write the output in the STDOUT
  • -m MODIFICATIONS or --modifications=MODIFICATIONS: to inform the JSON of expected modifications
  • -f FILE or --modifications-file=FILE: to inform the JSON file with expected modifications
  • -l LOCALE or --locale=LOCALE: to inform the locale to be used by Faker
  • -q or --quiet: to ommit messages
  • -r SIZE or --read-buffer-size=SIZE: to inform the read buffer size (example: 100, 1KB, 1MB, 1GB)
  • -w SIZE or --write-buffer-size=SIZE: to inform the write buffer size (example: 100, 1KB, 1MB, 1GB)

This library uses Faker to generate the fake (anonymous) data. You will need to check the library to see the available options of formatters, arguments for formatters and locales.

Simple example:

$ php ./vendor/bin/anonymize-db-dump -i ./sample/dump.sql -o ./sample/dump.out.sql -f ./sample/modifications.json -l pt_BR

The example above will read the file ./sample/dump.sql, then produce a modified dump file ./sample/dump.out.sql using the modification instructions specified in the file ./sample/modifications.json and using the locale pt_BR (Brazilian Portuguese) to generate the fake data with localization.

If you want to generate the modified dump file in the STDOUT, you may replace -o ./sample/dump.out.sql by --stdout. This option is useful if you do not want to store the modified dump file and use the modified dump file directly to the mysql command to restore the data like the example bellow:

$ php ./vendor/bin/anonymize-db-dump -i ./sample/dump.sql --stdout -f ./sample/modifications.json -l pt_BR | mysql -uroot -proot -h localhost -D dbname

If you want to generate the modified dump file from the STDIN, you may replace -i ./sample/dump.sql by --stdin. This option is useful if you are getting the dump file from mysqldump directly like the example bellow:

$ mysqldump ... | php ./vendor/bin/anonymize-db-dump --stdin --o ./sample/dump.out.sql -f ./sample/modifications.json -l pt_BR

Specification of the JSON of modifications

The JSON to specify the modifications over the dump file uses this structure:

{
  "<TABLE_NAME>": {
    "<COLUMN_POSITION>": {
      "quote": <BOOLEAN_VALUE>,
      "format": <STRING_VALUE>,
      "args": <ARRAY_OF_ARGS>,
      "unique": <BOOLEAN_VALUE>,
      "optional": <BOOLEAN_VALUE>,
      "optional_weight": <FLOAT_VALUE>,
      "optional_default_value": <VALUE>
    }
  }
}

Basically, we need to specify the tables names, then the column positions of each table (starting from position 1), then the specification of the column.

The "quote" option is used to inform the value should be delimited by quotes (true) or not (false). For example, numeric values does not need to use quotes.

The "format" option is used to specify the format of the column according to the Faker formaters.

The "args" option is used to specify the array of arguments to be used when calling the specified format.

The "unique" option is used to specify the column is unique and should not repeat for that column of that table (it might repeat on other columns or other tables).

The "optional" option is used to specify whether the column is optional or not. When a column is optional, the library may select the default value for the field (if the default value is not specified, NULL is used).

The "optional_weight" is used to specify the probability to have an empty value. The value 0.0 indicates the default value will always be used, 1.0 indicates the default value will never be used, 0.2 indicates there is 20% of chance to use the default value.

The "optional_default_value" is used to specify the default value.

Example of modifications:

{
  "customers": {
    "2": {
      "quote": true,
      "format": "firstName"
    },
    "3": {
      "quote": true,
      "format": "lastName"
    },
    "4": {
      "quote": true,
      "format": "email",
      "unique": true
    },
    "5": {
      "quote": true,
      "format": "phoneNumber",
      "optional": true,
      "optional_weight": 0.9,
      "optional_default_value": null
    },
    "6": {
      "quote": true,
      "format": "lexify",
      "unique": true,
      "args": [
        "TEST?????????"
      ]
    }
  }
}

printi/db-dump-file-anonymizer 适用场景与选型建议

printi/db-dump-file-anonymizer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.53k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「dump」 「backup」 「anonymization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 printi/db-dump-file-anonymizer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 printi/db-dump-file-anonymizer 我们能提供哪些服务?
定制开发 / 二次开发

基于 printi/db-dump-file-anonymizer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-29