makers99/wp-cli-db-export-clean

WP-CLI 命令 `wp db export-clean`,用于创建不包含敏感客户/用户数据的数据库备份。

1.0.0 2023-03-17 22:16 UTC

This package is auto-updated.

Last update: 2024-08-29 12:49:08 UTC


README

添加 WP-CLI 命令 wp db export-clean,用于创建不包含与客户和可选 API 机密/凭证相关的敏感数据的 MySQL 数据库备份,同时保留所有管理用户及其相关数据。

同时排除修订版本以最小化备份文件大小。

快速链接: 用法 | 集成 | 安装 | 支持

用法

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

参数

该命令接受结果文件名作为参数。如果省略,则默认为 ./clean.sql

选项

示例

  • ./clean.sql 中创建干净的数据库备份

    $ wp db export-clean
  • 在用户的主目录中创建干净的数据库备份

    $ wp db export-clean ~/clean.sql
  • 在干净的数据库备份中排除插件许可证密钥和 API 密钥 - 当与插件供应商支持或未知自由职业者合作时很有用

    $ wp db export-clean --remove-keys

集成

支持的插件

放置过滤器钩子

wp db export-clean 在 WordPress 加载后直接运行(类似于 wp db export),这意味着仅加载 wp-config.php 和插件,但 WordPress 未使用 init 钩子引导。您可以将钩子放入强制使用插件;例如

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',
  ]));
});

在数据库导出中包括更多用户

wp db export-clean 默认仅包括具有管理员角色的用户。使用过滤器钩子 'wp-db-export-clean/allowed-emails' 在数据库备份中包括更多用户

/**
 * 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));
});

此外,您还可以通过 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);
});

在数据库导出中包括更多 WooCommerce 订单或订阅

/**
 * 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);
});

在数据库导出中排除更多/自定义数据

实现以下过滤器钩子以自定义所有表的 where 条件。

/**
 * 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;
});

排除许可证和 API 密钥

当传递 --remove-keys 选项时,以下插件目前受到支持

安装

以包的形式安装

  1. 为当前用户安装此包的最新版本
    wp package install makers99/wp-cli-db-export-clean

以Git子模块的形式安装

  1. 将包添加为子模块。

    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. 注册早期WP-CLI引导命令。

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

    或手动

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

使用Composer安装

  1. 使用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

    注意:不要使用--devrequire-dev的形式安装,因为export-clean通常在生产环境中使用。

  2. 注册早期WP-CLI引导命令。

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

    或手动

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

支持

导出时的MySQL错误

将以下内容添加到您的站点根目录下的wp-cli.yml文件中

db export:
  max-allowed-packet: 1G

与我们一起来创造吧!

最初由Bogdan ArizancuDaniel Kudwien创作。