Pick our brains...

How to use WP-CLI

I wanted to write some quick notes regarding the feedback that we got from our developers about how to export and import databases using WP-CLI. What a wonderful tool it is!

WP-CLI stands for “WordPress Command Line Interface”.

There are a bunch of commands you can use to manage WordPress easily and quickly without having to login to WordPress or cPanel and PHPMyAdmin.

You need to install it on the server (if not already installed) and also install it on your computer/machine.

How to install:

http://wp-cli.org/

Sync the db from production to dev using wp-cli:

** The example below uses an example client. They have a live site and a dev site already setup on her server. So these commands will export their live site database and import it to their dev database so we can do some more work on the dev site before implementing those changes to their live site.

  1. Ssh into server
    • name@email.com (example)
      • cPanel username followed by the “at” symbol then the domain name
  2. Enter the cPanel password when prompted
    • the password won’t appear on the screen as you type!
      • This was something I learned early on in my command line learning that totally confused me.
      • Trust it’s there as you type and press enter.
  3. Change to production folder
    • cd public_html
      • command for “change directory” followed by a space, then the directory of folder name you want to change to
  4. Export the production db
    • wp db export
      • commands for wp-cli start with “wp” followed by a space, then the command to “export the database”
  5. Move the file to the dev folder
    • mv drconkli_wp.sql ~/public_html_dev/
      • command for “move” followed by a space, then database name followed by a space, then path-on-server where you want it moved
  6. Change to the dev folder
    • cd ~/public_html_dev
      • command for “change directory” followed by a space, then path-on-server pointing to the folder or directory you want to change to/navigate to
  7. Import the db
    • wp db import drconkli_wp.sql
      • start with “wp” to use a wp-cli command, followed by a space, then the command to “import the database” followed by a space, then the database name to import
  8. Change the urls on the dev site
    • wp search-replace '//drconklindanao.com' '//dev.drconklindanao.com'
    • wp search-replace '//www.drconklindanao.com' '//dev.drconklindanao.com'

Use wp-cli to export a db on a server to your local machine.

  1. Follow steps 1 – 4 from above to ssh into the server and export the database that you want for your local dev site.
  2. Exit out of the server.
    • exit
      • use command “exit” to logout of the server and get back to your local machine
  3. Change to the folder where you want to copy the database
    • cd ~/Path/to-your-folder
  1. Use the “secure copy” command to copy the database from the remote server to your local computer/machine:
    • scp drconkli@drconklindanao.com:~/public_html/drconkli_wp.sql ./

      • scp is the command to “secure copy” followed by a space, then the user@server.com info tells the command to login to the server using the cpanel-username@domainname.com, followed by a colon then the path on that server where the database is located, including the database name, followed by a space then the location on your computer where you want the db securely copied. In this case ./ means “current folder” but you can use a path here instead.