Skip to content

Instantly share code, notes, and snippets.

@phlbnks
Last active April 16, 2021 18:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save phlbnks/d270e78d065fbe8b838706cefaf4499f to your computer and use it in GitHub Desktop.
Save phlbnks/d270e78d065fbe8b838706cefaf4499f to your computer and use it in GitHub Desktop.
Utility script to help manage WordPress sites on an EasyEngine server (but not limited to EE)
#!/bin/bash
# Help / usage info.
USAGE=$'WordPress Helper script, built for EasyEngine but should be widely compatible.\nWithout any args it will search /var/www/ for WordPress sites, loop over them in alphabetical order and check for core and plugin updates.\nAlso accepts:\n\t--sites=[space seperated list of site paths relative to /var/www]\n\t--update=[plugins|wp|all].'
# Die function for exiting on errors.
die () {
echo "${1}, exitting..." >&2 ; echo ; exit 1
}
# Read options.
for i in "$@"
do
case $i in
-h|--help)
HELP="true" # Display help / usage info.
shift # past argument with no value
;;
--sites=*)
SITES="${i#*=}" # Space seperated list of sites paths under /var/www.
shift # past argument=value
;;
--update=*)
UPDATE="${i#*=}" # Do updates - plugins | themes | wp | all.
shift # past argument=value
;;
*)
# Unknown option.
;;
esac
done
# What am I.
echo ========================
echo WordPress helper
echo ========================
# Display help / usage.
if [ "$HELP" == "true" ]; then
echo "$USAGE"
echo ; exit 1
fi
# Webroot.
www_dir='/var/www'
# If no site(s) passed as arg, find within webroot.
if [ -z "${SITES}" ]; then
for site_path in $( cd $www_dir ; find . -type d -name 'wp-admin' | sort -n ); do
SITES="${SITES} ${site_path}"
done
fi
# Output info messages.
if [ -z "${UPDATE}" ]; then
echo ;
echo === "Checking for updates" ===
echo ;
else
echo ;
echo === Running updates ===
echo ;
fi
# Loop over sites.
for site in ${SITES}; do
# Pre-flight checks.
[ -z $site ] && \
die "Error: no site(s) found or specified"
[ -r ${www_dir}/${site} ] || \
die "Error: '${www_dir}/${site}' does not exist"
# Move to current site dir.
cd ${www_dir}/${site};
# We're checking for updates.
if [ -z "${UPDATE}" ]; then
echo $(wp option get siteurl) \(v$(wp core version)\);
echo $(wp core check-update);
wp plugin list --format=csv --fields=name,status,update,version,update_version | awk -F',' '$3 == "available" {print "Plugin update: ",$1,"\t",$2,"\t",$4,"->",$5}' | column -t;
wp theme list --format=csv --fields=name,status,update,version,update_version | awk -F',' '$3 == "available" {print "Theme update: ",$1,"\t",$2,"\t",$4,"->",$5}' | column -t;
echo ========================
fi
if [ "${UPDATE}" == "plugins" ]; then
echo $(wp option get siteurl);
wp plugin update --all
echo ========================
elif [ "${UPDATE}" == "themes" ]; then
echo $(wp option get siteurl);
wp theme update --all
echo ========================
elif [ "${UPDATE}" == "wp" ]; then
echo $(wp option get siteurl);
wp core update
echo ========================
elif [ "${UPDATE}" == "all" ]; then
echo $(wp option get siteurl);
wp core update
wp plugin update --all
wp theme update --all
echo ========================
fi
done;
@phlbnks
Copy link
Author

phlbnks commented Feb 13, 2017

Usage:
$ wget -qO wp_helper.sh https://gist.githubusercontent.com/emirpprime/d270e78d065fbe8b838706cefaf4499f/raw/
$ chmod +x wp_helper.sh (you may need to sudo this depending on your setup)
./wp_helper.sh --help
Related blogpost

@solracsf
Copy link

solracsf commented Sep 5, 2017

@emirpprime Thanks, but it finds my first blog, updates it, but not the others, with error:

line 82: cd: apps/./myblog/public/wp-admin: No such file or directory but that directory exists.

A simple

ls apps/./myblog/public/wp-admin

shows the dir content.

All blogs are under apps/myblog[1,2,3,...]/public and i've passed www_dir='apps'

@solracsf
Copy link

solracsf commented Sep 5, 2017

Fixed, i've passed full path, like /srv/websites/apps instead of just apps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment