how to migrate WordPress site using wp cli

Understanding WP-CLI for WordPress Migration

WP-CLI stands for WordPress Command Line Interface, a powerful tool that simplifies WordPress administration and migration tasks via command-line. It’s an effective alternative to traditional graphical user interfaces, providing efficiency and control when handling complex operations like migrating a WordPress site.

Preparing for Migration with WP-CLI

Before initiating a migration, ensure WP-CLI is installed on both the source and destination servers. Most hosting providers support WP-CLI, but installation instructions can be found on the official WP-CLI website if it’s not pre-installed.

Step 1: Backup Your WordPress Site

Using WP-CLI, you can quickly generate a backup of your WordPress files and database. To do this, navigate to your WordPress directory and run:

wp db export

This command will create a SQL file containing your database. Next, to backup your WordPress files:

tar -czf nameofyourbackup.tar.gz /path/to/your/WordPress

This command compresses your WordPress directory into a single file.

Step 2: Transfer Files and Database

Upload the backup files (nameofyourbackup.tar.gz and the exported SQL file) to your new server using a secure method like SCP or FTP:

scp nameofyourbackup.tar.gz username@destinationserver:/path/to/destination
scp yourdatabase.sql username@destinationserver:/path/to/destination

Setting up the New Server

Upon successful transfer, log into your new server and navigate to the directory where you uploaded the files.

Step 3: Extract WordPress Files

Extract your WordPress files:

tar -xzf nameofyourbackup.tar.gz
Step 4: Create a New Database

Assuming MySQL is used, create a new database:

mysql -u root -p -e "CREATE DATABASE new_db_name; GRANT ALL PRIVILEGES ON new_db_name.* TO 'your_user'@'localhost' IDENTIFIED BY 'your_password'; FLUSH PRIVILEGES;"
Step 5: Import the Database

Import your database using WP-CLI:

wp db import yourdatabase.sql

Configuring WordPress

After importing your database, you will need to update the wp-config.php file to reflect the new database credentials and URL.

Step 6: Update Database Details

Edit the wp-config.php using a text editor or using WP-CLI:

wp config set DB_NAME new_db_name
wp config set DB_USER your_user
wp config set DB_PASSWORD your_password
Step 7: Update Site URL

If the domain remains the same, skip this step. If not, update your site URLs:

wp search-replace 'http://oldsiteurl.com' 'http://newsiteurl.com'

This WP-CLI command will replace all instances of the old URL with the new one in the database.

Step 8: Verify Installation

Make sure everything is properly configured by navigating to your site URL. Additionally, check if the WordPress admin dashboard is accessible and functioning.

Step 9: Adjust Permalinks

Access the WordPress dashboard, go to Settings > Permalinks and simply click “Save Changes” to reset the permalink structure and ensure all links are updated according to the new domain structure.

Best Practices and Troubleshooting

  1. Dry Runs: Before running potentially destructive operations (wp search-replace), use the --dry-run option to preview what will change.
  2. Keep Backups: Always retain backup files until you have fully verified that the new site works correctly.
  3. Test Thoroughly: Check for broken links, malfunctioning plugins, and theme issues that might have occurred during migration.

WP-CLI is a robust tool that can drastically reduce the time and effort involved in migrating a WordPress site. By following these command-line procedures, developers can efficiently move sites between hosts or servers, leveraging WP-CLI’s capabilities for a smooth transition.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *