MU-Migration

repository·master·Indexed 43 days ago

https://github.com/10up/mu-migration

A WP-CLI plugin designed to simplify moving WordPress sites between single installations and Multisite networks. It automates table prefix replacement and user ID mapping by exporting sites into portable zip packages containing metadata, user CSVs, and SQL database files. Key commands include `wp mu-migration export all`, `wp mu-migration import all`, and `wp mu-migration users update_passwords` for managing user credentials post-migration.

Tokens
3.5K
Snippets
17
Records
17
Agent score
52%

What's inside MU-Migration

  1. Install MU-Migration via WP-CLI

    master

    The easiest way to install MU-Migration is using the WP-CLI package manager. This requires WP-CLI version 0.23 or higher.

    If you encounter memory exhaustion errors during installation, it is a known WP-CLI issue that may require increasing the PHP memory limit.

    wp package install 10up/mu-migration
  2. Install MU-Migration as a plugin

    master

    Alternatively, you can install it manually:

    1. Clone the repository into your WordPress plugins/ folder.
    2. Run composer install to fetch necessary dependencies.
    3. Activate the plugin within WordPress.

    Important: You must install this plugin on both the source site (the site being moved) and the target Multisite installation.

    # After cloning to plugins/
    composer install
  3. Manage user passwords with `wp mu-migration update_passwords`

    master

    After a migration, you can use this command to reset or force password updates for users on a specific site.

    Arguments and Flags

    • <newpassword> (optional): The new password to set. If omitted, passwords will be reset to random secure values.
    • --blog_id=<id>: Target a specific subsite.
    • --reset: Resets passwords to random secure values.
    • --send_email: Sends a password reset email to all users.
    • --include=<users_id>: Only update these specific user IDs.
    • --exclude=<users_id>: Skip these specific user IDs.
    # Update all users on site 3 to a specific password
    wp mu-migration update_passwords 'new_weak_password' --blog_id=3
    
    # Reset all users to random passwords and email them
    wp mu-migration update_passwords --reset --blog_id=3 --send_email
  4. Import a site using `wp mu-migration import all`

    master

    Use the import all command to unpack a .zip file into your WordPress installation.

    • If importing into Multisite: It creates a new site within the network.
    • If importing into a single install: It overrides the existing single installation.

    Common Flags

    • --new_url=<url>: Set a new URL for the imported site (useful for staging or local environments).
    • --blog_id=<id>: Import the site into a specific existing subsite ID (overriding it).
    • --mysql-single-transaction: Wraps the SQL export in a single transaction. This is recommended for clustered MySQL environments to prevent overwhelming the database server.
    # Basic import
    wp mu-migration import all site.zip
    
    # Import with a new URL
    wp mu-migration import all site.zip --new_url=multisite.dev/site
    
    # Import into an existing subsite ID with a single transaction
    wp mu-migration import all site.zip --new_url=multisite.dev/site --blog_id=3 --mysql-single-transaction
  5. Export a site using `wp mu-migration export all`

    master

    Use the export all command to package a site into a .zip file. This package includes users, tables, and optionally plugins, themes, and uploads. This zip can then be moved to a Multisite network for import.

    Common Flags

    • --plugins: Include the plugins folder in the zip.
    • --themes: Include the themes folder in the zip.
    • --uploads: Include the uploads folder in the zip.
    • --blog_id=<id>: Export a specific subsite from an existing Multisite instance instead of the main site.
    • --tables=<comma_separated_list>: Export only the specified tables (must include default tables).
    • --non-default-tables=<comma_separated_list>: Include specific custom tables during a subsite export.
    # Export everything including files
    wp mu-migration export all site.zip --plugins --themes --uploads
    
    # Export a specific subsite from a multisite
    wp mu-migration export all subsite.zip --blog_id=2
    
    # Export a subsite with specific custom tables
    wp mu-migration export all subsite.zip --blog_id=1 --non-default-tables=wp_my_custom_table,wp_my_custom_table_2
  6. Reference: wp mu-migration import tables options

    master

    Options for the wp mu-migration import tables command.

    # <inputfile> : The name of the exported .sql file
    # --blog_id=<blog_id> : The ID of the blog to import tables into (Required)
    # --old_prefix=<old> : The original database prefix (default: current wpdb prefix)
    # --new_prefix=<new> : The new database prefix to apply
    # --original_blog_id=<ID> : The original blog ID (used to calculate upload paths)
    # --old_url=<olddomain> : The original site URL for search-replace
    # --new_url=<newdomain> : The new site URL for search-replace
  7. Reference: wp mu-migration import all options

    master

    Options for the wp mu-migration import all command.

    # <zipfile> : The name of the exported .zip file
    # --blog_id=<blog_id> : The ID of the blog to import into. If not provided in Multisite, a new site is created.
    # --new_url=<new_url> : The new URL for the site.
    # --mysql-single-transaction : If set, wraps the SQL import in a START TRANSACTION and COMMIT.
    # --uid_fields=<uid_fields> : Comma-separated list of user ID fields used to map post authors during import.
  8. Export users to CSV via WP-CLI

    master

    Use the wp mu-migration export users command to export WordPress users and their metadata to a .csv file. The export includes standard user fields and user meta, while automatically filtering out sensitive or environment-specific keys (like session_tokens, capabilities, or user-settings).

    Options:

    • <outputfile>: The name of the exported .csv file.
    • --blog_id=<blog_id>: The ID of the blog to export users from.

    Note: You can extend the exported user data or the default CSV headers using the following WordPress filters:

    • mu_migration/export/user/headers: Add custom columns to the CSV.
    • mu_migration/export/user/data: Add custom data to each user row.
    wp mu-migration export users output.csv --blog_id=2
  9. Export an entire site to a ZIP file via WP-CLI

    master

    Use the wp mu-migration export all command to create a comprehensive migration package in a single .zip file. This package includes site metadata (JSON), user data (CSV), and database tables (SQL). You can optionally include files from the WordPress filesystem.

    Options:

    • <zipfile>: The name of the resulting .zip file.
    • --blog_id=<blog_id>: The ID of the blog to export.
    • --tables=<table_list>: Specific tables to include in the SQL export.
    • --non-default-tables=<table_list>: Additional tables to include.
    • --plugins: Include the wp-content/plugins directory.
    • --themes: Include the active theme and child theme directories.
    • --uploads: Include the wp-content/uploads directory.
    • --verbose: Enable verbose output.
    wp mu-migration export all site.zip --plugins --themes --uploads
  10. Import a full site package via wp mu-migration import all

    master

    Import an entire site from a .zip package into a Multisite installation. This is a high-level command that orchestrates several sub-tasks:

    1. Extracts the zip package.
    2. Imports tables (using the tables command).
    3. Moves files: Plugins, themes, and uploads are moved to their correct locations.
    4. Imports users (using the users command).
    5. Updates post authors: Uses the generated user map to reassign post authors based on provided uid_fields.
    6. Flushes rewrite rules.

    A valid zip package must contain a .json metadata file, a .csv user file, and a .sql database file.

    wp mu-migration import all site.zip --uid_fields=_content_audit_owner
  11. Import database tables via wp mu-migration import tables

    master

    Import SQL tables from a single site instance into a Multisite environment. This command performs a search-and-replace for database prefixes and URLs, and updates the necessary tables to ensure compatibility with Multisite (e.g., updating the user_roles option key).

    Requirements: The sed utility must be installed on the system to handle prefix replacement in the .sql file.

    wp mu-migration import tables site.sql --old_prefix=wp_ --old_url=old_domain.com --new_url=new_domain.com