WP-CLI is a powerful tool for managing WordPress. I use it to automate backups and migrations for the WordPress sites I manage.
This does not work for multisite! I will make a new guide for migrating multisites.
How to Migrate WordPress Site to New Server with WP-CLI
Note that this tutorial does not walk you through installing a web server stack like LEMP or LAMP. I of course recommend nginx and MariaDB.
This is the todo list for the tutorial
Old WordPress Server
- Install WP-CLI on the old server
- Backup the WordPress installation and database
- Transfer the backups to the new server
New WordPress Server
- Create a new database on the new server
- Restore the old database onto the new server’s database
- Install WordPress on the new server
Preparing the Old WordPress Server
Note throughout this section the old site is called wp-bullet
Install WP-CLI
sudo wget -q https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /usr/bin/wp
sudo chmod 755 /usr/bin/wp
Enter your WordPress folder
cd /var/www/wp-bullet
Export your database
mkdir -p ~/backups
wp db export ~/backups/wp-bullet.sql --allow-root --skip-themes --skip-plugins
Now tar your WordPress site folder, the period . takes all files and folders of the current directory you are in.
The -z
adds gzip compression
tar -czf ~/backups/wp-bullet.tar.gz .
To tar without compression you can use this command instead
tar -cf ~/backups/wp-bullet.tar .
Make sure to note the database prefix in wp-config.php as you will need to specify it for the restore to work on the new server
Now in your backups folder you have a database backup and the entire site backed up.
You should transfer these two files to your desktop or laptop computer via SFTP or FTP
Preparing the New WordPress Server
You should already have prepared your web stack on the new server.
Transfer your sql backup and the sitebackup tar file from your old server to your new server. I usually put them in ~/restore
.
Install WP-CLI, you will need PHP installed in order to run WP-CLI
sudo wget -q https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /usr/bin/wp
sudo chmod 755 /usr/bin/wp
Create an empty WordPress database
mysql -u root -p
These credentials will be used for the wp-config.php file.
CREATE USER wordpressuser@localhost IDENTIFIED BY 'passw0rd';
CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* TO wordpressuser@localhost IDENTIFIED BY 'passw0rd';
FLUSH PRIVILEGES;
quit;
Create your new WordPress directory
sudo mkdir -p /var/www/wp-bullet-dev
Enter the WordPress directory
cd /var/www/wp-bullet-dev
Download the WordPress core files
wp core download --allow-root
Create the wp-config.php file and specify the database credentials created in the previous MySQL step
For --prefix
specify the custom database prefix if you aren’t using the standard wp_
wp core config --dbname=wordpress --dbuser=wordpressuser --dbpass=passw0rd --allow-root --dbprefix=wp_
Now install WordPress
wp core install --url=wp-bullet-dev.com --title="WP Bullet Dev" --admin_user=wpbullet --admin_password=passw0rd --admin_email=admin@wp-bullet.com --skip-email --allow-root
If you know you use the normal permalink structure WP-CLI will do that too, Apache users will have to log in and save these settings to generate the .htaccess file
wp option update permalink_structure '/%postname%' --allow-root
It is now time to actually perform the restore of the old database and WordPress folder to your new site location
If you are running as the root user then transfer it to /root/restore/wp-bullet.sql
wp db import ~/restore/wp-bullet.sql --allow-root
Time to do a search and replace of your old sitename with your new site name, WP-CLI handles this for us
Do a test run first to make sure everything looks alright
wp search-replace wp-bullet.com wp-bullet-dev.com --dry-run --allow-root
If it all looks good then run the command again without --dry-run
to actually update the database
wp search-replace wp-bullet.com wp-bullet-dev.com --allow-root
Now unpack the tar backup to your new site WordPress folder while excluding the original wp-config.php since we already did that with wp-cli sorcery.
tar -xf ~/restore/wp-bullet.tar -C /var/www/wp-bullet-dev --exclude=wp-config.php
Change ownership of your WordPress installation to the recommended secure permissions.
sudo chown -R www-data:www-data /var/www/
sudo find /var/www/ -type f -exec chmod 644 {} +
sudo find /var/www/ -type d -exec chmod 755 {} +
Could you explain why use ” –skip-themes –skip-plugins” in wp db export?
So the steps are:
1. install WordPress
2. create database
3. Import database
4. Unpack backup file
Can we unpack backup file first then import database?