Skip to content

Instantly share code, notes, and snippets.

@clcollins
Last active May 15, 2019 15:18
Show Gist options
  • Save clcollins/682edb10de8d1002f1e1 to your computer and use it in GitHub Desktop.
Save clcollins/682edb10de8d1002f1e1 to your computer and use it in GitHub Desktop.
WP-CLI Site Upgrade
#!/bin/bash
#
# TODO:
# How to handle NOT running the DB upgrade if it's not needed?
HOSTNAME="$(/bin/hostname)"
TMPDIR='/tmp'
MAILFROM="patch.adams@$HOSTNAME"
MAILTO="$@"
VALID_MAILTO=()
MAILSUB="WordPress patching results from ${HOSTNAME}"
MAILBODY=()
WPCMD="wp --no-color"
f_usage () {
echo "Usage: wp-upgrade.sh EMAIL [EMAIL]..."
exit 1
}
f_error () {
echo "Error: ${1}"
exit 1
}
f_validate_email () {
VALID="^(([-a-zA-Z0-9\!#\$%\&\'*+/=?^_\`{\|}~])+\.)*[-a-zA-Z0-9\!#\$%\&\'*+/=?^_\`{\|}~]+@\w((-|\w)*\w)*\.(\w((-|\w)*\w)*\.)*\w{2,4}$"
TEST=$(echo $1 |egrep $VALID)
if [ "x$TEST" = "x" ] ; then
echo "Invalid address: $1"
else
VALID_MAILTO+=("${1}, ")
fi
}
f_mail () {
for email in $MAILTO ; do f_validate_email $email ; done
/usr/sbin/sendmail -oi -t << EOF
FROM: ${MAILFROM}
TO: ${VALID_MAILTO[@]}
SUBJECT: $MAILSUB
"${MAILBODY}"
EOF
}
f_upgrade() {
$WPCMD core upgrade && \
# I'm not sure the following is actually working.
# WP doesn't seem to register that the DBs were updated
#
# time for url in $($WPCMD site list --fields=url --format=csv | tail -n +2) ;
# do $WPCMD --url=$url core update-db >> /dev/null
# done && \
$WPCMD plugin update --all --dry-run | awk -F\| '{print $2, $4}' && \
$WPCMD plugin update --all && \
$WPCMD theme update --all --dry-run |awk -F\| '{print $2, $3}' && \
$WPCMD theme update --all
}
if [[ -z $@ ]] ; then
f_usage
fi
# Download WP-CLI
WPCLI="https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar"
curl $WPCLI -o ${TMPDIR}/wp || f_error "Unable to download WP-CLI binary"
chmod +x ${TMPDIR}/wp
MAILBODY=$(f_upgrade)
$WPCMD core update-db >> /dev/null && \
f_mail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment