Automate Multiple WordPress Updates With Bash

Do you have lots of WordPress installations that you currently have to update manually? Fed up with logging into individual WordPress installations to perform your updates?

Below is a Bash script that can be used to quickly update multiple WordPress installations and their associated plugins from the command line.

You’ll also need to install WP-CLI.

#!/bin/bash

# Script to update WordPress installs and associated plugins

# List WordPress sites to update here in this format
# webserver user:path to wp installation:additional arguments
sites=(
    'user1:/var/websites/user1/public'
    'user2:/var/websites/user2/public/blog:--skip-plugins=addthis'
    'user3:/var/websites/user3/public/news'
    # etc
)

clear

# Loop through each site
for i in "${sites[@]}"; do

  IFS=':' read -r -a array <<< "$i"

  user="${array[0]}"
  dir="${array[1]}"
  args="${array[2]}"

  # Change to the site's directory
  cd $dir

  # See if WordPress core needs updating or not
  echo "Checking site $user"
  sudo -u $user wp-cli core check-update $args

  if [[ $(sudo -u $user wp-cli core check-update $args) = "Success: WordPress is at the latest version." ]]
  then
    echo "Skipping core update for $user"
  else
   # Core update needed, prompt
   read -p "Do you want to UPDATE WordPress Core for $user ? Y/N " -n 1 -r
   if [[ $REPLY =~ ^[Yy]$ ]]
   then
     echo
     # Do the core update
     sudo -u $user wp-cli core update $args

     # Update the db
     sudo -u $user wp-cli core update-db $args
   fi
  fi

  # Ask the user if they want to list the plugins for this site
  read -p "Do you want to list Plugin updates for $user ? Y/N " -n 1 -r
  echo
  if [[ $REPLY =~ ^[Yy]$ ]]
  then
    # Display the list of plugins
    sudo -u $user wp-cli plugin list $args

    # Ask the user if they want to update the plugins
    read -p "Do you want to UPDATE ALL Plugins for $user ? Y/N " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]
    then
      # Update the plugins
      sudo -u $user wp-cli plugin update --all $args
    fi
  fi
  echo "-----------------------------------------------------------"
done
echo "Finished!"
echo

Leave a reply

(won't be published)
Email us Web enquiry form