Skip to main content
  1. Posts/

How to migrate a Wordpress Website using the Command Line

·2152 words·11 mins·

Do you need to migrate a WordPress site from one server to another? In this article, we will see a detailed explanation of how to migrate a WordPress website. And we will be using the command line to make it as fast as possible.

By the end of this guide, will be ready to migrate any WordPress website and will hopefully have a better understanding of how WordPress works behind the scenes.

What do we need?>

What do we need? #

A configured server>

A configured server #

In this article, we will not cover how to configure a virtual host in a server. In other words, I am assuming that you already have a directory in your server that can serve a public website to your configured domain name.

If this is not the case, you will need to configure your Linux server with server software. I recommend that you use Apache2 or NGINX. Here’s an article on how to configure virtual hosts in Apache2 with Ubuntu.

You should also have PHP and MySQL installed on your server. Here is a guide on how to do that.

Shell access You will need an SFTP account with shell access for both servers in question (of course). In this article, I assume that we are working with Linux servers. If you don’t have shell access, check with your hosting provider to enable it.

To access your SFTP account, you usually need a username, a host and a password. In this tutorial, we will use the following 2 dummy SFTP accounts for our dummy servers:

Server 1:
username: user_of_server1
password: 123456789

Server 2:
username: user_of_server2
password: 98765432`

Buckle up. We will be migrating our WordPress website from server1 to server2.

WP-CLI (recommended) #

Once you have shell access to your server, ideally you would need to install WP-CLI on both servers.

WP-CLI (WordPress Command Line Interface) enables a set of commands that allow you to is the best way to interact with your WordPress website in the command line. It is free and very easy to install.

To check if you have WP-CLI installed on your server, run the following command:

$ wp --version

You should see the current version of WP-CLI that you have installed. If your server does not have WP-CLI installed, follow these instructions to install it.

Sometimes, your hosting provider will give you shell access but they won’t allow you to install binaries on your server. That means that you will not be able to run WP-CLI commands.

If that’s the case, don’t worry. In this tutorial, you will also have alternative methods that don’t require WP-CLI.

Step 1: Export your website’s database>

Step 1: Export your website’s database #

Image from Pixabay, by Tumisu There are several ways to do this using visual interfaces. If you use Plesk, for example, you can click on “export the dump” and you will download the database. There are also several plugins that allow you to do this directly inside WP Admin.

But we are more powerful than that: we use the command line.

Method 1: Using WP-CLI>

Method 1: Using WP-CLI #

WP-CLI comes with a very straightforward command that allows you to export your database. Connect to your server1 and run the following command:

$ wp db export my_database.sql This will create a file named my_database.sql in the place where you ran the command. This file contains all the data of your WordPress site.

What I usually do is run this command in the public folder of my website installation (the folder with wp-config.php). This makes your database file instantly downloadable like this:

Remember to delete the file from your server after you download it. You would never want your database to be publicly accessible via an open URL.

We will use this file later to import our database into our server2.

Method 2: Without WP-CLI>

Method 2: Without WP-CLI #

To do the same thing with MySQL commands, you will need to find out the name of your WordPress database.

You can find it in your wp-config.php file. Just open it and check the following line:

/** The name of the database for WordPress */
define( 'DB_NAME', 'my_website_database' );

In this case, my database name is my_website_database.

With this information, you can run the following command on your server:

$ mysqldump my_website_database > file_name.sql

Just like WP-CLI, this command will create a new SQL file with the name of your choosing. This is your website’s database.

Note that you might need root access to run this command. If that is the case, just run it as root like this:

$ mysqldump -uroot -p my_website_database > file_name.sql

-uroot indicates that you want to run the command as root and -p indicates that you will use a password as an authentication method. Now just type your root password and you should be good.

Bravo! Now we have our database file. We will need to import this file to our server2 database, so keep it somewhere safe.

Step 2: Migrate your website source code>

Step 2: Migrate your website source code #

Image by mohamed Hassan from Pixabay Now that you have our database file, let’s move everything to our new server:

This means, all the media, the source code, the configurations, etc. In short, all the files –or everything that is not in the database.

Why not use FileZilla for this?>

Why not use FileZilla for this? #

I’ve seen many people migrate their files using FileZilla. That’s indeed what is recommended by most educators. FileZilla is very good for beginners because it has a very simple interface and it allows you to upload things directly to your server from your computer.

But what happens if you want to move your files from one remote server to another? Do you first use FileZilla to download everything to your computer and then upload it to the other server?

What a waste of time.

You can do the same thing with a single command. Then lean back while the server’s bandwidth manages all the work and you make yourself a cup of coffee.

Migrate your website using the command line>

Migrate your website using the command line #

The fastest and easiest way to move files from one Linux server to another is to use rsync.

In short, rsync stands for remote sync. It recognizes the files that are already in your target server and copies only the new files and the new bits. It can be several times faster than using a simple remote copy command like scp.

The command takes two parameters. The first one is the origin from where you want to copy the files, and the second one is the location where you want to copy them to. Run the following command in your Server one to copy everything to your new server:

$ rsync -az "your/website/root/directory" ""

Note that you can use this command from your own computer too if you’re migrating a website from your local environment to a server.

For reference, here’s what we did in this command:

-az flags: they indicate that you want to copy everything in the directory you are specifying, including subfolders. You can add a v at the end like this -azv if you want the command to be “verbose”. That means that you will see each file that is being transferred in real-time instead of just a blank space.

your_website_root" doesn’t necessarily need to be inside quotes, but it helps if your folder names include other symbols or if you are copying all the files inside a folder like this: ”/home/public/*”.

It’s the same for the second argument (your target server), but you just need to specify which remote machine it is by preceding the path with the user and server where it’s located. Don’t forget the colons : at the end of the host address.

Step 3: Configure (or create) your database>

Step 3: Configure (or create) your database #

Image from WikiMedia Now you have all your source code and files in Server 2, you will need to configure your database. The rest of the tutorial assumes that you are connected to Server 2 via SSH and that’s where you are running your commands.

If you were given an already configured server, you might have a database already installed with a user and password assigned. That’s the best-case scenario. In that case, this information might look like this:

user: my_sql_user
dabase: my_db_name
password: 123456789

If you don’t have this information, you will have to create your own MySQL database and create a user and password that WordPress will use. Follow these instructions to create your database and a user of that database with all the privileges.

Step 4: Edit WP Config>

Step 4: Edit WP Config #

Image by Pexels from Pixabay The next step is to configure your WordPress website. The file that manages this is wp-config.php and it’s located at the root of your WordPress installation.

What you will need to do is update this file with the database information that we just got.

Of course, you could download the file with FileZilla, edit it with a text editor and then re-upload it to your server. But a faster way is to use the terminal. You can either use nano or vim for this. Here is a quick tutorial on how to use vim.

Make sure to update the following lines in the file with the information you have about your database:

/** The name of the database for WordPress */
define('DB_NAME', 'database_name_here');
/** MySQL database username */
define('DB_USER', 'username_here');
/** MySQL database password */
define('DB_PASSWORD', 'password_here');
/** MySQL hostname */
define('DB_HOST', 'localhost');

Also, remember to update the table prefix setting in case your tables have a different prefix. This is also a line in the wp-config.php file.

$table_prefix = 'wp_';

That’s all. Now your WordPress installation is connected to your database. Hurray! You are ready to import all your data to the database you created!

Step 5: Import your database into your new server>

Step 5: Import your database into your new server #

Image by Pexels from Pixabay By this time you should have all your source code in your target server and your WordPress installation configured.

But note that if you try to access your website from a web browser, you will see an empty WordPress installation, even though all your files are already on your server. That’s because we haven’t imported our database.

Do you remember the file that you exported in step 1? That file, however you chose to name it (e.g. my_database.sql) is what we will need to import to our new database.

But first, are you keeping the same domain name for your website or are you changing the domain name? Remember to update your domain name in your database if you want to use a new domain name.

Method 1: Import your database using WP-CLI Now let’s see how to import your database to your WordPress installation. WP-CLI is the easiest way to do this, but it requires that your filewp-config.php be already configured with the correct data.

That is because WP-CLI reads this file and uses that information to perform its tasks. That’s why it’s important to finish step 4 before using WP-CLI to do this.

Locate the file that you created with your database, and rub the following command inside your WordPress installation in your server2:

$ wp db import path_to/my_database.sql

Now your database should be complete too!

Method 2: Import your database without WP-CLI Another possible way of doing this is with the native MySQL commands. To do this, you need to know the name of the local database that is connected to your WordPress installation (the same that you entered in wp-config.php).

mysql your_database_name < /path_to/my_database.sql

If for some reason, you are not logged in to your server with the right permissions to alter this database, you will need to specify the user of the database. To do this, we use the flags -u and -p. Just paste the username right after the -u (without spaces). in this case:

mysql -umy_sql_user -p your_database < /path_to/my_database.sql

This will ask you to type the password for the root user.

That’s all!>

That’s all! #

Now your database should be correctly imported and your migration is finished. Feel free to check if it works on your target server.

You can tap yourself in the back because this was quite a complex thing after all. I hope this helped you not only learn to do server migrations, but also taught you more about how WordPress works and how the database interacts with the source code.

Feel free to contact me if you see an error in this article or if you think that something is not clear enough. Cheers!