makers99 / wp-cli-db-export-clean
WP-CLI 命令 `wp db export-clean`,用于创建不包含敏感客户/用户数据的数据库备份。
Requires
- php: >=7.4
- ifsnop/mysqldump-php: ^2.9
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
集成
支持的插件
- WordPress 核心(仅保留保留用户的帖子评论,省略修订版本、暂态和缓存)
- WooCommerce(仅保留保留用户的订单,省略计划操作和会话)
- WooCommerce Subscriptions(仅保留保留用户的订阅)
- Gravityforms(省略修订版本、条目和统计数据)
- wp-lister-amazon、wp-lister-ebay(省略源、任务和日志)
- Yoast wordpress-seo(省略索引跟踪、迁移和链接)
放置过滤器钩子
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
选项时,以下插件目前受到支持
- WooCommerce(PayPal)
- WooCommerce PayPal Plus
- WooCommerce PayPal Express Checkout(woocommerce-gateway-paypal-express-checkout)
- wp-lister-amazon
- wp-lister-ebay
- Gravityforms
- WooThemes(wp-all-import、wp-all-export)
- Optimus
- Elementor
- SearchWP
- Gravityforms Zero Spam
- WooCommerce Amazon Payments
安装
以包的形式安装
- 为当前用户安装此包的最新版本
wp package install makers99/wp-cli-db-export-clean
以Git子模块的形式安装
-
将包添加为子模块。
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
-
注册早期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安装
-
使用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
注意:不要使用
--dev
以require-dev
的形式安装,因为export-clean通常在生产环境中使用。 -
注册早期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