The Lab Migrate WordPress From cPanel with Command Lines (SSH, RSYNC & WP-CLI)

Hardcore Cpanel Migration

We have recently migrated all our sites away from cPanel to clean, fast Ubuntu servers. Here is a tutorial to help you do the same!

19.01.17
Migrate WordPress From cPanel with Command Lines (SSH, RSYNC & WP-CLI)

We are using ServerPilot to manage the server apps and accounts so we have some specific instructions. However, we have tried to include general instructions for alternate setups. This guide is tailored to moving WordPress sites as apps and assumes you have wp-cli installed on your destination server, why wouldn’t you!

Before you begin, you must make sure SSH is enabled on both the destination and source server (Shared Hosting may not always allow this).

 

1. Access destination server via SSH

  • Make sure ssh-copy-id is installed on your machine by running the below command. You can skip this step if it’s already installed.

brew install ssh-copy-id

  • Now let’s fire up ssh-copy-id which will simplify the process if you need to access the server in the future.

$ ssh-copy-id USERNAME@IPADDRESS

  • Next, use your server details to SHH into the destination server. Use the below command, replacing username and ipaddress with the correct records.

$ ssh USERNAME@IPADDRESS

  • Are you using ServerPilot? Use the below commands to move the existing wp-config.php from ~/apps/APPNAME/public and remove the public directory.

$ cd ~/apps/APPNAME

$ mv public/wp-config.php .

$ rm -rf public

 

2. Generate an SSH key for the destination server

  • Firstly, let’s check for an existing SSH key

$ cat ~/.ssh/id_rsa.pub

  • If one exists, copy it and skip the next step. Otherwise, use the below command to generate a new key.
  • To use the default settings, just hit Enter for each prompt.

$ ssh-keygen

  • Now we can print the public SSH key we just created and copy it

$ cat ~/.ssh/id_rsa.pub

 

3. Add destination server’s Public SSH key to source server

  • There are a few ways of achieving this. If you have access to the cPanel of the source server, head into the SSH Access settings.
  • Open Manage SSH Keys $ Import Key
  • Paste in the destination server’s public SSH key (the one we just created) to the Public Key field
  • Add an arbitrary name to the Name field and click Import
  • On the following screen, find the SSH key and click Manage $ Authorize

 

4. Login to the source server via the destination server

The destination server is now authorised to access the source server.

  • From within the destination server, use the following command with the details from the source server (these are displayed in the cPanel)

$ ssh username@ipaddress

Note: You can use the WHM/cPanel domain as ipaddress

If you cannot access the source server, you may need to use a different port. For some servers you can use ssh -p 4001 username@ipaddress. The destination server’s IP address may need to be authorised.

  • Now let’s export the database from the source server

$ cd public_html

$ wp db export

This will generate a databasename.sql file which we will use shortly.

If you get an error, WP-CLI may not be installed. You will have to export the database manually with phpMyAdmin or a plugin like UpdraftPlus.

  • Now you can exit the source server with the below command

$ exit

 

5. RSYNC the source directory to the destination server

  • First, ensure you are in the correct directory on the destination server such as ~/apps/appname
  • Now we can use rsync the source directory into the destination directory. Make sure to replace the sourceuser and sourceip to the relevant details from the source server.

$ rsync -avzh sourceuser@sourceip:~/public_html .

Note: Ensure your don’t type a trailing slash (/) on the source directory or this won’t work.

If you get an error, you may need to use a different SSH port. Add -e “ssh -p 4001” to the end of the command to use a different port.

  • If using ServerPilot, we will need to amend the wp-config.php file. First, rename the public_html folder to public

$ mv public_html public

  • Now let’s check the source table prefix in wp-config.php

$ cat public/wp-config.php | grep table_prefix

  • Edit the new wp-config.php to include the correct table prefix

$ nano wp-config.php

  • Move this updated wp-config.php into the public folder to overwrite the source wp-config.php

$ mv wp-config.php public/.

 

6. Import your database

Make sure you’re inside the public directory on the destination server.

$ cd public

First, we need to reset the database. This empties the old database ready for the new import.

$ wp db reset

Now let’s import the .sql file you previously exported into public_html on the source server (this is your database)

$ wp db import filename.sql

Finally, remove the old database to save space and reduce risk of stolen data

$ rm FILENAME.sql

 

7. Done (Read: Drink Beer)

Samantha

Written by Samantha

Samantha is our resident web designer and front-end developer wizard. She is knowledgable in all things digital design and loves to bring new and fresh ideas to our company.