Install WP-CLI | Setup WordPress / Export / Import Database by WP-CLI

Category : WordPress

In this tutorial we will cover, how to install WP-CLI and use it for WordPress database backup, database import, creating new WordPress instance.

How to install WP-CLI  (for all users)

Prerequisites

  • Root level SSH access to server
  • PHP 5.3.29 or higher version
  • WP-CLI support wordpress 3.7 or above versions only.

To test if your server has WP-CLI already installed or not, running the following command :

wp

If you see the error below wp-cli is not installed, you can continue with the tutorial to install it on the server.

[root@host ~]# wp –info
-bash: wp: command not found
If you see actual command output,  with the arguments listed then it means WP-CLI is already installed.
Installation Steps
1. We will first download wp-cli.phar file by following command

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

2. Give executable permission to the file downloaded

chmod +x wp-cli.phar

3.Finally, we will move the wp-cli.phar executable to global location to ensure all users have access.

sudo mv wp-cli.phar /usr/local/bin/wp

We have succesfully installed WP-CLI. To verify the same, run the following command

wp

Error: YIKES! It looks like you’re running this as root. You probably meant to run this as the user that your WordPress install exists under.If you REALLY mean to run this as root, we won’t stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.

If you’d like to continue as root, please run this again, adding this flag: –allow-root

If you’d like to run it as the user that this site is under, you can run the following to become the respective user:

sudo -u USER -i — wp

Seeing the error above is a good sign, it mean that the file executes correctly.

How to create WordPress Instance (site) witih WP-CLI

Prerequisites

  • Root level SSH access to server
  • WP-CLI installed.
  1. After making ssh connection to your server, navigate to the directory where you want to install the wordpress website
  2. Running the following command will install the wordpress instance. Do replace the arguments/parameters used in the command  like URL, Title, Admin Username, Password, and Admin Email.

wp core install url=your_domain title=Your_Blog_Title admin_user=username admin_password=password admin_email=your_email.com

Voila wordpress has been installed !

How to backup database using WP-CLI

Prerequisites

  • Root level SSH access to server
  • WP-CLI installed.

1.After connecting to the server, move to the directory where your wp-config file resides

cd /home/basezap/public_html

2. Run the following command to take backup :

wp export

This will generate .sql file in the current directory.

 

How to import database using WP-CLI

Prerequisites

  • Root level SSH access to server
  • WP-CLI installed.

1.After connecting to the server, move to the directory where your wp-config file resides

cd /home/basezap/public_html

2. Run the following command to import the database :

wp db import basezap_database.sql

This will successfully import the backed up db sql file into the WordPress database.

Once you’ve run the export command you’ll see confirmation output like the following:

Success: Imported to ‘../basezap_database.sql’.

Seeing the text above confirms your WordPress database has been restored.