Pull WordPress from Prod to Local with WP-CLI Bash Script

Recently I’ve taken to using a simple setup for local WordPress development (which you can read about and watch here: Local WordPress with Herd, DBngin, and WP-CLI).

When you’re not using a local dev tool that’s managed by your hosting company (for me that was usually WordPress VIP or WP Engine), you typically have to set up your own way to pull down any changes from production. You can definitely use a backup/staging plugin, but with WP-CLI running on any good host, why not speed the whole process up and run it with one simple terminal command?

Q: What about pushing changes from local to production? A: I use git! Content/database starts on prod and only comes down. Code goes up.

So here’s the script I’ve been using. Running it triggers a few things:

  1. Exports the database on production.
  2. Pulls the entire wp-content folder & database export down (using rsync, meaning it should skip any files you already have)
  3. Deletes the remote database dump
  4. Imports the database locally and runs search-replace to swap out the production domain with the staging domain.

That’s it! There’s probably other stuff you’ll have to set up on your local environment, and maybe you can add a workaround for media files and skip the uploads folder. Up to you.

There’s a few things to note:

  1. This assumes you already have a local WordPress environment set up.
  2. This file goes in the root folder of your local WordPress installation. You can run it by simply running bash pull.sh in your terminal.
  3. You’ll need to set it up with your SSH credentials, and then make sure the REMOTE_PATH points to the root folder of the production site (relative to where you land when you log in with those credentials), while the LOCAL_PATH points to your local wp-content folder.
  4. Check out the various exclude flags for things I don’t like to download, like hosting-specific plugins or logs.
  5. For non-essential sites, I update plugins / theme on production, so I have this pull down any updates to the plugins directory. You could exclude that as well.

That’s it. Any ideas on how to optimize it? I’d love to hear them.

Here’s the full script:

Edit: I did add one additional line to the end of my import script:

wp plugin install --activate create-block-theme

This installs and activates the Create Block Theme plugin on my local environment so that I can export any changes I make to the templates and version control them.

3 responses to “Pull WordPress from Prod to Local with WP-CLI Bash Script”

  1. Neil Batchelor Avatar

    That’s a really nice approach 🙂

  2. Anthony Jackson Avatar

    This is very cool, this demonstrates the power of bash and what is possible with bash! I my self have a bash script that automates the installation of WordPress on your own server. WordPress is hosted in a Docker container.

    Great article!

  3. Mateusz Avatar

    Nice one! Made myself something similar, but it kept breaking. I will definetely try this one out!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.