Batch Upgrading WordPress installs on server

PSA: You shouldn’t just willy nilly upgrade your WordPress installs. It’s best to test your upgrade in a staging environment.

Every time a WordPress update comes out, I cringe having to update all the sites on my VPS server. I finally hunkered down today and got a shell script written to upgrade all WordPress installs on a server.

This solution will upgrade on one server, so If you have WordPress installs sprawled across multiple servers, you might want to look into a service like ManageWP.

The key is wp-cli

wp-cli is a command line tool that helps streamline WordPress tasks like upgrading, installing and configuring.

Installing wp-cli

Note: Right now, I haven’t successfully installed wp-cli globally, so for now, you will have to install wp-cli for each user on your server. Add a comment to this post if you have successfully done a global install.

Update: wp-cli changed their installation and you can now do a global installation.

Follow the wp-cli installation guide to install globally.

 

Creating the shell script

We’ll create a script that will upgrade wordpress, the database, all plugins, and all themes. You can edit this accordingly in the script below.

Create the file

$ vim push_all_wordpress_sites.sh

Paste the following into your new file:

#!/bin/bash

paths=(
  /home/USERNAME/path/to/wordpress
  /home/USERNAME/path/to/anothor/wordpress
)

for PATH_STRING in "${paths[@]}"
do
  :

  # regex to extract username from path
  re="\/home\/([^/]+)\/"
  if [[ $PATH_STRING =~ $re ]]; then

    # change to correct directory
    cd ${PATH_STRING}

    echo "Backing up" ${PATH_STRING} "Database..."
    /usr/bin/sudo -H -u ${BASH_REMATCH[1]} zsh -c '~/.wp-cli/bin/wp db export'

    # prompt user of upgrade
    echo "Upgrading: " ${PATH_STRING}

    # do wp-cli magic
    /usr/bin/sudo -H -u ${BASH_REMATCH[1]} bash -c '/usr/local/bin/wp core update'
    /usr/bin/sudo -H -u ${BASH_REMATCH[1]} bash -c '/usr/local/bin/wp core update-db'
    /usr/bin/sudo -H -u ${BASH_REMATCH[1]} bash -c '/usr/local/bin/wp plugin update --all'
    /usr/bin/sudo -H -u ${BASH_REMATCH[1]} bash -c '/usr/local/bin/wp theme update --all'

  fi

done

Note: This script assumes that your paths are full paths including the username folder.

As you’ll see in the “paths” variable in the top, you’ll want to change those placeholders to real paths on your server where your WordPress installs are located.

Executing the script

From here you just need to change the permissions of your new file:

$ chmod 755 push_all_wordpress_sites.sh

Then, you can run it anytime you want to upgrade all of your WordPress installs:

$ ./push_all_wordpress_sites.sh

6 thoughts on “Batch Upgrading WordPress installs on server

  1. This is not working anymore. Install path is 404 and installations is different, but once CLI installed script is not working too.

  2. Hmm, mine still seems to be working ok. It seems like you have an issue with wp-cli being installed properly. I know they changed their installation a while back to use the php wp-cli.phar --info command. Have you tried reinstalling?

  3. Koen says:

    Great script, i’ve removed the zsh part because my server doesn’t run zsh, but it works perfectly!

  4. Thanks for pointing that out, Koen! I’ve updated the script. I can’t assume everyone is running zshell. 🙂

  5. This is fantastic, the natural application for wp-cli in my opinion!

    Two things not working for me though:
    1. My command path is /usr/bin/wp
    2. I’m not a sudoer but I can run the wp command.

    Can I omit or change /usr/bin/sudo -H -u ${BASH_REMATCH[1]} so it works for a normal user?

  6. Thanks for pointing these out, Tom!

    1. I didn’t update the wp path for the new wp-cli installation (it used to be installed anywhere you wanted). I just updated the code for that.
    2. You don’t need sudo. The only reason I had it was because my WordPress installs spanned multiple users on the server and I never setup a correct www-data group. 🙁
    3. The easiest way to see if you need sudo is to remove it and see if you run into any permission issues when you run the script.

      Thanks for the feedback!

Leave a Reply

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