定制 makers99/wp-cli-db-export-clean 二次开发

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

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

makers99/wp-cli-db-export-clean

Composer 安装命令:

composer require makers99/wp-cli-db-export-clean

包简介

A WP-CLI command `wp db export-clean` to create a database dump without sensitive customer/user data.

README 文档

README

Adds the WP-CLI command wp db export-clean to create a MySQL database dump without sensitive data related to customers and optionally API secrets/credentials, while retaining all administrative users and their related data.

Revisions are excluded as well to minimize the dump file size.

Quick links: Usage | Integration | Installation | Support

Usage

wp db export-clean [<filepath>] [--remove-keys]

Arguments

The command accepts the result filename as argument. If omitted, it defaults to ./clean.sql.

Options

Option Description Default
--remove-keys Additionally remove options containing license keys and API credentials during dump. false

Examples

  • Create clean database dump in ./clean.sql:

    $ wp db export-clean
  • Create clean database dump in the user's home directory:

    $ wp db export-clean ~/clean.sql
  • Exclude plugin license keys and API keys in the clean database dump – useful when working with plugin vendor support or unknown freelancers:

    $ wp db export-clean --remove-keys

Integration

Supported plugins

Placing the filter hooks

wp db export-clean runs directly after WordPress is loaded (like wp db export), which means that only wp-config.php and plugins are loaded but WordPress is not bootstrapped with init hooks. You can place your hooks into a must-use plugin; for example:

wp-content/mu-plugins/wp-cli-db-export-clean.php:

<?php

/*
  Plugin Name: wp db export-clean Customizations
  Version: 1.0.0
  Description: Includes test users in the clean database dump.
*/

add_filter('wp-db-export-clean/allowed-emails', function ($allowed_emails) {
  return array_unique(array_merge($allowed_emails, [
    'test@example.com',
  ]));
});

Including more users in the database export

wp db export-clean only includes users having the role Administrator by default. Use the filter hook 'wp-db-export-clean/allowed-emails' to include more users in the database dump:

/**
 * Customizes list of email addresses to retain in clean database dump.
 *
 * @return array
 *   An array whose items are email addresses to keep.
 */
add_filter('wp-db-export-clean/allowed-emails', function ($allowed_emails) {
  global $wpdb;
  $users = $wpdb->get_col(
    $wpdb->prepare("SELECT u.user_email FROM {$wpdb->prefix}users u WHERE u.user_email LIKE '%%%s'", '@example.com')
  );
  return array_unique(array_merge($allowed_emails, $users));
});

In addition, you can include users by ID:

/**
 * Customizes list of user IDs to retain in clean database dump.
 *
 * @return array
 *   An array whose items are user IDs to keep.
 */
add_filter('wp-db-export-clean/allowed-user-ids', function ($allowedUserIds) {
  $allowedUserIds[] = 123;
  $allowedUserIds[] = 456;
  return array_unique($allowedUserIds);
});

Including more WooCommerce orders or subscriptions in the database export

/**
 * Customizes list of shop order/subscription IDs to retain in clean database dump.
 *
 * @return array
 *   An array whose items are shop_order IDs to keep.
 */
add_filter('wp-db-export-clean/allowed-order-ids', function ($allowedOrderIds) {
  $allowedOrderIds[] = 123456;
  return array_unique($allowedOrderIds);
});

Excluding more/custom data in the database export

Implement the following filter hook to customize the where conditions for all tables.

/**
 * Customizes select query conditions for each table in clean database dump.
 *
 * @return array
 *   An array whose keys are table names and whose values are SQL WHERE clause conditions.
 */
add_filter('wp-db-export-clean/table-wheres', function ($tableWheres) {
  global $wpdb;

  $tableWheres = array_merge($tableWheres, [
    "{$wpdb->prefix}my_log" => '1 = 0',
    "{$wpdb->prefix}my_userdata" => "user_id IN ({$allowedUserIds})",
  ]);
  return $tableWheres;
});

Excluded licenses and API keys

When passing the --remove-keys option, the following plugins are currently supported:

Installation

Install as package

  1. To install the latest version of this package for the current user:
    wp package install makers99/wp-cli-db-export-clean

Install as Git submodule

  1. Add the package as submodule.

    git submodule add --name wp-cli-db-export-clean git@github.com:makers99/wp-cli-db-export-clean.git .wp-cli/packages/db-export-clean
  2. Register the command for early WP-CLI bootstrap.

    echo -e "require:\n  - .wp-cli/packages/db-export-clean/package.php" >> wp-cli.yml

    Or manually:

    vi wp-cli.yml
    require:
      - .wp-cli/packages/db-export-clean/plugin.php

Install with Composer

  1. Install the package with Composer.

    composer config repositories.wp-cli-db-export-clean git https://github.com/makers99/wp-cli-db-export-clean.git
    composer require makers99/wp-cli-db-export-clean:dev-master

    Note: Do not use --dev to install as require-dev, because export-clean is typically used in production.

  2. Register the command for early WP-CLI bootstrap.

    echo -e "require:\n  - vendor/makers99/wp-cli-db-export-clean/package.php" >> wp-cli.yml

    Or manually:

    vi wp-cli.yml
    require:
      - vendor/makers99/wp-cli-db-export-clean/package.php

Support

MySQL errors during export

Add to wp-cli.yml in your site root folder:

db export:
  max-allowed-packet: 1G

Come create with us!

Originally authored by Bogdan Arizancu and Daniel Kudwien.

makers99/wp-cli-db-export-clean 适用场景与选型建议

makers99/wp-cli-db-export-clean 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 445 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 makers99/wp-cli-db-export-clean 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 445
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 5
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2023-03-17