Skip to content

Instantly share code, notes, and snippets.

@pacoorozco
Last active January 25, 2017 15:43
Show Gist options
  • Save pacoorozco/e117751a48626b2675cb5de6a6639501 to your computer and use it in GitHub Desktop.
Save pacoorozco/e117751a48626b2675cb5de6a6639501 to your computer and use it in GitHub Desktop.
Update several Wordpress Sites from a location using wp-update.sh
#!/usr/bin/env bash
##########################################################################
# Shellscript: Update WordPress sites recursively from a directory
# Author : Paco Orozco <[email protected]>
# Requires : wp-cli, wp-update.sh
##########################################################################
# Changelog
# 20161220: 0.1
# Inital release
##########################################################################
##########################################################################
# Configuration Section
##########################################################################
# Number of backups to maintain
NUM_OF_BACKUPS=2
##########################################################################
# DO NOT MODIFY BEYOND THIS LINE
##########################################################################
# Program name and version
PN=$( basename "$0" )
VER='0.1'
# Script exits immediately if any command within it exits with a non-zero status.
set -o errexit
# Script will catch the exit status of a previous command in a pipe.
set -o pipefail
# Script exits immediately if tries to use an undeclared variables.
set -o nounset
# Uncomment this to enable debug
# set -o xtrace
# Set VERBOSITY level.
# 0 - No messages are shown
# 1 - Some informational messages. Defaults
# 2 - Debug
VERBOSITY=1
WP_PATH=""
WP_UPDATE_PATH="wp-update.sh"
##########################################################################
# Functions
##########################################################################
function warn () {
local MESSAGE_TYPE=1
if [ "$1" = "-l" ] ; then
shift
MESSAGE_TYPE=$1
shift
fi
if (( "${MESSAGE_TYPE}" <= "${VERBOSITY}" )) ; then
if [ "$1" = "-n" ] ; then
shift; echo -n "$@"
else
echo "$@"
fi
fi
}
function crit () {
local ERROR=0
[ "$1" = "-e" ] && shift; ERROR=$1; shift
echo >&2 -e "${PN}: $@"
[ ${ERROR} ] && exit ${ERROR}
}
function usage () {
# Variables for formatting
U=$(tput smul) # Underline
RU=$(tput rmul) # Remove underline
B=$(tput bold) # Bold
N=$(tput sgr0) # Normal
cat <<-EOF
${B}Usage:${N}
${B}$0${N} -p ${U}directory${RU} [options]...
${B}Options:${N}
${B}-h${N} Display this help message
${B}-v${N} Enable VERBOSITY mode. Use -vv for debug.
${B}-q${N} Enable silent mode. No messages will be shown.
${B}-p${N} ${U}directory${RU}
Path where several WordPress blogs resides. This is a required flag.
${U}directory${RU}
├── blog1
│ ├── wp-config.php
│ └── blog-files
└── blog2...
This script will find all WP installations from this ${U}directory${RU}
and will backup up both the database and files before updating. Backups
will be stored in the following format inside every blog directory:
${U}directory${RU}
├── blog1
│ └── backups
│ └── wp-backup_YYYY-MM-DD_HH:MM:SS
│ ├── database.sql
│ └── files.tar.gz
└── blog2
${B}Example:${N}
$0 ${B}-p${N} ${U}/var/www${RU}
Minimal options. Will update all WordPress installations in the specified
directory.
EOF
}
function check-requirements () {
# Check if wp-cli is installed.
if ! hash ${WP_UPDATE_PATH} 2>/dev/null ; then
crit -e 1 "We require wp-update.sh.\nFor more info visit https://gist.github.com/pacoorozco/0b166e9595b82efdfd8547e22ba98bf0"
fi
}
##########################################################################
# Main
##########################################################################
# Resetting OPTIND is necessary if getopts was used previously in the script.
OPTIND=1
# Process command line options
while getopts ":hvqp:" opt; do
case $opt in
h)
usage
;;
p)
WP_PATH=${OPTARG}
;;
v)
VERBOSITY=$((${VERBOSITY} + 1))
;;
q)
VERBOSITY=0
;;
?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires and arguement" >&2
exit 1
;;
esac
done
# Shift off the options and optional --.
shift "$((OPTIND - 1))"
# Run appropriate functions based on options provided
if [ -z "${WP_PATH}" ] ; then
usage
exit 1
fi
check-requirements
if (( "${VERBOSITY}" < "2" )) ; then
WP_UPDATE="${WP_UPDATE_PATH} -q"
else
WP_UPDATE="${WP_UPDATE_PATH} -v"
fi
for BLOG in $( find ${WP_PATH} -maxdepth 2 -name "wp-config.php" )
do
BLOG_PATH=$( dirname ${BLOG} )
if [ "${VERBOSITY}" = "1" ] ; then
warn -n "Updating WordPress site on ${BLOG_PATH}... "
else
warn "Updating WordPress site on ${BLOG_PATH}... "
fi
BACKUP_PATH="${BLOG_PATH}/backups"
${WP_UPDATE} -p ${BLOG_PATH} -b ${BACKUP_PATH} -n ${NUM_OF_BACKUPS} || true
if [ "$?" = "0" ] ; then
warn "OK."
else
warn "FAIL."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment