Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active April 26, 2016 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/dc70d5dfd430e0b622c9de73e0633092 to your computer and use it in GitHub Desktop.
Save trepmal/dc70d5dfd430e0b622c9de73e0633092 to your computer and use it in GitHub Desktop.

Update core (and db), plugins, and themes, logging status to syslog. Ideal for running on a cronjob

Example of syslog

Apr 26 04:07:56 vvv vagrant: [WPAutoUpdate] Local WordPress Dev | http://local.wordpress.dev
Apr 26 04:08:27 vvv vagrant: [WPAutoUpdate] Core update: WordPress updated successfully to 4.5
Apr 26 04:08:28 vvv vagrant: [WPAutoUpdate] DB update: WordPress database already at latest db version 36686
Apr 26 04:08:30 vvv vagrant: [WPAutoUpdate] Plugin updates: Akismet, Yoast SEO
Apr 26 04:08:34 vvv vagrant: [WPAutoUpdate] Theme updates: Twenty Fifteen, Twenty Fourteen, Twenty Sixteen, Twenty Twelve

Specify path as first argument passed to script
./auto-update.sh /path/to/wp

#!/bin/bash
# self-update wp-cli
wp --allow-root cli update --major --minor --patch --yes --quiet
if [ -z $1 ]; then
path='';
else
path=`readlink -f $1`
path=" --path=$path"
fi
flags="$path --allow-root"
blogname=`wp $flags option get blogname`
bloghome=`wp $flags option get home`
logger "[WPAutoUpdate] $blogname | $bloghome"
#### #### ##### ######
# # # # # # #
# # # # # #####
# # # ##### #
# # # # # # #
#### #### # # ######
checkupd=`wp $flags core check-update --format=yaml | grep 'version: ' | tr -d ' '`
if [ ! -z $checkupd ]; then
updateto=${checkupd/version:/}
coreupdate=`wp $flags core update --no-color | grep "^Success:" | tr -d '\n'`
coreupdate=${coreupdate/Success: /}
coreupdate=${coreupdate%.}
logger "[WPAutoUpdate] Core update: ${coreupdate} to ${updateto}"
dbupdate=`wp $flags core update-db --no-color | grep "^Success:" | tr -d '\n'`
dbupdate=${dbupdate/Success: /}
logger "[WPAutoUpdate] DB update: ${dbupdate}"
else
logger "[WPAutoUpdate] Core update: WordPress has no pending updates"
fi
##### # # # #### # # # ####
# # # # # # # # ## # #
# # # # # # # # # # ####
##### # # # # ### # # # # #
# # # # # # # # ## # #
# ###### #### #### # # # ####
pluginupdate=`wp $flags plugin update --all --dry-run --format=summary | grep "from version"`
pluginlist=''
if [ -z "$pluginupdate" ]; then
pluginlist='No plugins updated';
else
while read -r plugin; do
pluginlist=$pluginlist"${plugin/ update from*/}, "
done <<< "$pluginupdate"
fi
logger "[WPAutoUpdate] Plugin updates: ${pluginlist%, }"
##### # # ###### # # ###### ####
# # # # ## ## # #
# ###### ##### # ## # ##### ####
# # # # # # # #
# # # # # # # # #
# # # ###### # # ###### ####
themeupdate=`wp $flags theme update --all --format=summary | grep "from version"`
themelist=''
if [ -z "$themeupdate" ]; then
themelist='No themes updated';
else
while read -r theme; do
themelist=$themelist"${theme/ updated successfully*/}, "
done <<< "$themeupdate"
fi
logger "[WPAutoUpdate] Theme updates: ${themelist%, }"
#### # ###### ## # # # # #####
# # # # # # ## # # # # #
# # ##### # # # # # # # # #
# # # ###### # # # # # #####
# # # # # # # ## # # #
#### ###### ###### # # # # #### #
logger "[WPAutoUpdate] `wp $flags cache flush`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment