diff --git a/README.md b/README.md index 6618001..4cb67e0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,24 @@ # WP Sync DB CLI An addon for [WP Sync DB](https://github.com/slang800/wp-sync-db) that allows you to execute migrations using a function call or via WP-CLI + +## Example usage +The following two variations of the `wpsdb migrate` command are possible by supplying either a profile id or connection info string. + +#### Target a user created profile already stored in the system/database +`wp wpsdb migrate --profile=1` + +**profile** represents the profile number as seen in the Migrate tab's "saved migration profiles" list in WP. + +#### Manually target a connection string (no profile) +`wp wpsdb migrate --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1` + +**connection-info** is the string found on the target site's Settings tab. The line break is replaced with the `\n` equivalent character in order to pass the whole string on a single line. + +**action** can be set to either `pull` or `push` depending on the direction of the DB sync + +**create-backup** is a bit field that indicates whether the DB should be backed up prior to a transfer. This defaults to 1. + +#### Target a profile and fallback to a connection string +`wp wpsdb migrate --profile=1 --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1` + +In this example we can attempt to target a profile that doesn't yet exist before the migration and instead fallback to the manual profile arguments provided. Once the migration is complete if we re-run this command, then the profile will have been migrated over and this will be used. This is useful from an automation point of view. diff --git a/class/command.php b/class/command.php index cee217e..c03a598 100644 --- a/class/command.php +++ b/class/command.php @@ -3,38 +3,104 @@ /** * Migrate your DB using WP Sync DB. */ -class WPSDBCLI extends WP_CLI_Command { +class WPSDBCLI extends WP_CLI_Command +{ /** - * Run a migration. + * Run a migration. Either profile id or connection-info + action are required. * * ## OPTIONS * - * + * [--profile=] * : ID of the profile to use for the migration. + * + * [--connection-info=] + * : Manual connection info for when a profile by the ID is not found. The above ID will be used to save a copy + * + * [--action=] + * : The type of action to perform against the target connection + * --- + * default: pull + * options: + * - pull + * - push + * --- * + * [--create-backup=] + * : Whether to take a backup before running the action. + * --- + * default: 1 + * options: + * - 0 + * - 1 + * --- + * * ## EXAMPLES * - * wp wpsdb migrate 1 + * wp wpsdb migrate --profile=1 + * wp wpsdb migrate --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1 + * wp wpsdb migrate --profile=1 --connection-info=https://example.com\n6AvE1jnBHIZtITuNCXj2eZArNM8uqNXC --action=pull --create-backup=1 * - * @synopsis + * @synopsis [--profile=] [--connection-info=] [--action=] [--create-backup=] * * @since 1.0 */ - public function migrate( $args, $assoc_args ) { - $profile = $args[0]; + public function migrate($args, $assoc_args) + { + $profile = null; + $manual_profile = []; - $result = wpsdb_migrate( $profile ); + // Target manually (maybe no database available yet) - if ( true === $result ) { - WP_CLI::success( __( 'Migration successful.', 'wp-sync-db-cli' ) ); + if ($assoc_args['profile']) { + $profile = $assoc_args['profile']; + } + if ($assoc_args['connection-info'] && $assoc_args['action']) { + // Preprocess some variables + $connection_info = stripcslashes($assoc_args['connection-info']); + $connection_info_segments = explode("\n", $connection_info); + $friendly_name = preg_replace("(^https?://)", "", $connection_info_segments[0]); + + // Create a default profile, that will save afterwards + $manual_profile = array( + 'connection_info' => $connection_info, + 'action' => $assoc_args['action'], + 'create_backup' => $assoc_args['create-backup'], + 'backup_option' => "backup_only_with_prefix", + 'select_backup' => null, + 'select_tables' => null, + 'table_migrate_option' => "migrate_only_with_prefix", + 'exclude_transients' => 1, + 'media_files' => 1, + 'remove_local_media' => 1, + 'save_migration_profile_option' => 0, + 'create_new_profile' => $friendly_name, + 'name' => $friendly_name, + 'save_computer' => 0, + 'gzip_file' => 1, + 'replace_guids' => 1, + 'exclude_spam' => 0, + 'keep_active_plugins' => 1, + 'exclude_post_types' => 0 + ); + } + + if ($profile == null && empty($manual_profile)) { + WP_CLI::warning(__('Either profile id or connection-info + action are required.', 'wp-sync-db-cli')); + WP_CLI::log('Usage: wpsdb migrate [--profile=] [--connection-info=] [--action=] [--create-backup=]'); return; } - WP_CLI::warning( $result->get_error_message() ); + $result = wpsdb_migrate($profile, $manual_profile); + + if (true === $result) { + WP_CLI::success(__('Migration successful.', 'wp-sync-db-cli')); + return; + } + + WP_CLI::warning($result->get_error_message()); return; } - } -WP_CLI::add_command( 'wpsdb', 'WPSDBCLI' ); +WP_CLI::add_command('wpsdb', 'WPSDBCLI'); diff --git a/class/wpsdb-cli.php b/class/wpsdb-cli.php index ab59116..67130a2 100644 --- a/class/wpsdb-cli.php +++ b/class/wpsdb-cli.php @@ -10,18 +10,19 @@ function __construct( $plugin_file_path ) { if( ! $this->meets_version_requirements( '1.4b1' ) ) return; } - function cli_migration( $profile ) { + function cli_migration( $profile, $manual_profile ) { global $wpsdb; $wpsdb_settings = get_option( 'wpsdb_settings' ); --$profile; if( ! $this->meets_version_requirements( '1.4b1' ) ) return $this->cli_error( __( 'Please update WP Sync DB.', 'wp-sync-db-cli' ) ); - if( ! isset( $profile ) ) return $this->cli_error( __( 'Profile ID missing.', 'wp-sync-db-cli' ) ); - if( ! isset( $wpsdb_settings['profiles'][$profile] ) ) return $this->cli_error( __( 'Profile ID not found.', 'wp-sync-db-cli' ) ); + if( ! isset( $profile ) && ! isset( $manual_profile ) ) return $this->cli_error( __( 'Profile ID missing.', 'wp-sync-db-cli' ) ); + if( ! isset( $manual_profile ) && ! isset( $wpsdb_settings['profiles'][$profile] ) ) return $this->cli_error( __( 'Profile ID not found.', 'wp-sync-db-cli' ) ); $this->set_time_limit(); $wpsdb->set_cli_migration(); - $profile = apply_filters( 'wpsdb_cli_profile_before_migration', $wpsdb_settings['profiles'][$profile] ); + // If we have a profile by the supplied ID, then use it. Otherwise, attempt to use the provided manual_profile + $profile = apply_filters( 'wpsdb_cli_profile_before_migration', $wpsdb_settings['profiles'][$profile] ? $wpsdb_settings['profiles'][$profile] : $manual_profile ); $connection_info = explode( "\n", $profile['connection_info'] ); $form_data = http_build_query( $profile ); diff --git a/composer.json b/composer.json index 7232579..aeb3c55 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "wp-sync-db/wp-sync-db-cli", + "name": "josephdsouza86/wp-sync-db-cli", "type": "wordpress-plugin", - "homepage": "https://github.com/wp-sync-db/wp-sync-db-cli", + "homepage": "https://github.com/josephdsouza86/wp-sync-db-cli", "license": "GPL-2.0", "description": "WP Sync DB CLI Addon", "keywords": ["plugin","wordpress","wp-sync-db","cli"], diff --git a/wp-sync-db-cli.php b/wp-sync-db-cli.php index e356bd0..c43b2e7 100644 --- a/wp-sync-db-cli.php +++ b/wp-sync-db-cli.php @@ -29,10 +29,10 @@ function wp_sync_db_cli_loaded() { } add_action( 'plugins_loaded', 'wp_sync_db_cli_loaded', 20 ); -function wpsdb_migrate( $profile ) { +function wpsdb_migrate( $profile, $manual_profile ) { global $wpsdb_cli; if( empty( $wpsdb_cli ) ) { return new WP_Error( 'wpsdb_cli_error', __( 'WP Sync DB CLI class not available', 'wp-sync-db-cli' ) ); } - return $wpsdb_cli->cli_migration( $profile ); + return $wpsdb_cli->cli_migration( $profile, $manual_profile ); }