How to migrate your WooCommerce site to another host for free.

This DIY tutorial explains how to use WP-CLI to do a backup of a remote wordpress installation.

Table of Contents

Of course, there is a plugin to migrate WooCommerce! If there is a plugin to create rainbow unicorns on your site, a plugin to migrate a WooCommerce store doesn’t seem like too much of a stretch. That’s right! The plugin Duplicator does just that. It’s pretty easy – you install it on a site, and it will create an archive with an installer to restore the site on another hosting provider. Wpbeginners has a comprehensive guide on how to use Duplicator; I encourage you to have a look. However, it does require you to trust the installer, and it’s limited in options when you use the free version.

 

Fine, I want a DIY. What are the prerequisites?

This post targets new developers and people with basic command-line knowledge. If you have absolutely no idea how to use the command line, I’d recommend using the Duplicator plugin mentioned above.

We also assume a *Nix environment such as Linux, macOS or Windows 10 WSL2 and that you have a running, local version of WordPress on your development machine.

Migrating your WooCommerce site manually, the easy way.

First, connect to the remote server using SSH.

Second, install wp-cli. If you don’t know about wp-cli already, it’s the ultimate command-line tool for WordPress. It does just about anything you can think of in terms of maintenance. At Woogo Stores, we leverage that tool extensively for automation!

Downloading WP-CLI

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar && sudo mv wp-cli.phar /usr/local/bin/wp

You should do that setup on both machines – the one you want to migrate from and the one you want to migrate to

Putting the live site in Maintenance Mode (optional)

If you intend to shut down your server (for example, you’re changing hosts), it would be wise to stop it. This is to ensure there are no additional transactions on the database, and integrity is not compromised during the transfer. The easiest way to do that is to enable maintenance mode on WordPress, using the new tool we just downloaded, wp-cli.

NOTE

You can skip this step if you’re doing a backup for local or staging development when data integrity is not required!

cd /dir/of/wordpress/installation
wp maintenance-mode activate

You will see:


Enabling Maintenance mode...
Success: Activated Maintenance mode.

And if you visit the site, this is what you will see:

Great! now we’re ready to backup.

Backing up the WooCommerce Database

In the actual directory of your WordPress installation:

wp db export - --add-drop-table --single-transaction --add-drop-database | gzip > ./wordpress.sql.gz

Well that’s a mouthful! Let me break it down for you:

  1. We ask wp-cli to export the database to stdout with additional flags for mysqldump.
  2. --add-drop-table --add-drop-database will include drop statements in the backup to avoid running into duplicates. It will delete all the tables it tries to import first.
  3. --single-transaction will create a “snapshot” of the database when you run the command, making any additional data written (or edited) during that time excluded from the backup. This ensures data integrity at the time when the command runs.
  4. | gzip compresses the data – it’s extremely fast nowadays and absolutely worth doing.
  5. And we output to a file in the WordPress directory

This can take from a few seconds to hours, depending on the size of the database. Great, WooCommerce data migration is done! But what about the files?

Migrating the WooCommerce files

Here comes one of the most amazing tool in the linux arsenal – RSYNC. And no, it’s not a boys band from the 90’s

Hi Justin!

If you want to copy the files from the remote server to your computer, it’s easy! simply invoke rsync locally:

rsync -avzh USER@HOST:/PATH/TO/WORDPRESS/ /your/local/wordpress/dir

This command will copy all the files, recursively, from your server to your local installation. The best thing with rsync is that it will only copy the blocks of files that are new or that have changed. The second time you run, this will be almost instant.

Now let’s say you already have your wp-config.php all set up with your local database credentials – you probably don’t want to overwrite that. Fear not! Simply exclude the file:

rsync -avzh USER@HOST:/PATH/TO/WORDPRESS/ /your/local/wordpress/dir --exclude=wp-config.php

And here we go! See here for more usages of rsync, it’s really an amazing tool.

How to rsync from a remote to another remote

Unfortunately, rsync cannot be used between two remote locations. The best would be to SSH on the new server and rsync locally from the old host. However, if it’s not possible, you can create an ssh tunnel between the two hosts, with the traffic going through your machine. We can achieve that with this simple command:

ssh -R localhost:50000:DESTINATION_HOST:22 SOURCE_HOST 'rsync -e "ssh -p 50000" -vuar /var/www localhost:/var/www'

This command is a bit complex but let’s break it down:

  1. localhost:50000:DESTINATION_HOST:22 SOURCE_HOST – This creates a reverse ssh tunnel from the source host on port 50000 of your local machine, back to port 22 on the destination host.
  2. Then we run the rsync command from the origin server connect on port 50000 and bring the data back to localhost. this is “faked” by the command above, and we’re in reality copying to the destination host!
eeeeeeh

It’s ok! You can just ignore this step and rsync to your machine first, and then to the new host.

Restoring the database

This one is trivial – in your local wordpress install, after doing rsync:

ATTENTION!

By restoring the database, you will delete the existing one on your machine! If you had any changes you wanted to save, export the current database now!

wp db reset
gunzip < wordpress.sql.gz |  wp db import -

This decompresses the backup in the stdout and imports it directly in the database specified in wp-config.php

Changing the host name

Now you need to change the host of the remote server to the local one.

wp search-replace PRODUCTION-DOMAIN.COM NEW-DOMAIN.COM --network

Not if your migrating from HTTPS to an HTTP location (for example your localhost) you will need in addition to run:

wp search-replace https://PRODUCTION-DOMAIN.COM http://NEW-DOMAIN.COM --network

And that’s it, you’re good to go!

WooCommerce Migration ✅ Complete!

It might seem a bit daunting at first, but in reality, it’s just a few command lines that will do the job just as well as any plugin. This technique works to migrate from any host to any other, including your localhost. Of course, if you’re migrating to Woogo Stores, you won’t need to do any of that as it’s part of our free migration concierge services.

If you have any questions, please do hesitate to ask below or simply contact us if you have any troubles!

There are 5 things on your e-commerce site that you can do to maximize sales. Are you doing them?