Skip to content

Instantly share code, notes, and snippets.

@Sam-R
Last active November 1, 2024 08:15
Show Gist options
  • Save Sam-R/40ec92b536912108a8425ab872618751 to your computer and use it in GitHub Desktop.
Save Sam-R/40ec92b536912108a8425ab872618751 to your computer and use it in GitHub Desktop.
Script to update all docker-compose based wordpress sites, plugins and themes in current or child directory
#!/bin/bash
###################
# Script to update all docker-compose based wordpress sites, plugins and themes
# Created by: Sam R.
# Created at: 2023-05-05
# Usage:
# - run the script from the directory, or parent directory, containing your docker-compose.yml files
# - the script will skip any directories that don't contain a docker-compose.yml or wordpress container
# - the script will update wordpress core, plugins and themes in each directory
#
# Example directory structure:
# /var/www/
# ├── docker-compose.yml ~ ingress proxy, etc
# ├── compose-wordpress-update.sh ~ this script
# ├── .env
# ├── site1.co.uk
# │ ├── docker-compose.yml ~ wordpress site
# │ ├── .env
# ├── site2.co.uk
# │ ├── docker-compose.yml ~ wordpress site
# │ ├── .env
# ├── site3.co.uk
# │ ├── docker-compose.yml ~ non-wordpress site
# │ ├── .env
###################
# Define the docker compose command
DOCKER_COMPOSE_CMD="sudo docker compose"
# find all directories containing docker compose files and store output in an array
mapfile -t docker_compose_directories < <(find . -not -path '*/.*' -type f -name "docker-compose.yml" 2>/dev/null -exec dirname {} \;)
for directory in "${docker_compose_directories[@]}"; do
echo "Trying: ${directory}"
if [[ -f "${directory}/docker-compose.yml" ]]; then
if [ "$(${DOCKER_COMPOSE_CMD} -f "${directory}/docker-compose.yml" --env-file "${directory}/.env" ps -q wordpress 2>/dev/null)" ]; then
echo "Updating $(basename ${directory})..."
if ! ${DOCKER_COMPOSE_CMD} -f "${directory}/docker-compose.yml" --env-file "${directory}/.env" pull && \
${DOCKER_COMPOSE_CMD} -f "${directory}/docker-compose.yml" --env-file "${directory}/.env" up -d; then
echo -e "\e[31m[ERROR] Failed to update WP CORE in $(basename ${directory})\e[0m"
exit 1
fi
if ! ${DOCKER_COMPOSE_CMD} -f "${directory}/docker-compose.yml" --env-file "${directory}/.env" run wordpress-cli wp plugin update --all; then
echo -e "\e[31m[ERROR] Failed to update WP PLUGINS in $(basename ${directory})\e[0m"
exit 1
fi
if ! ${DOCKER_COMPOSE_CMD} -f "${directory}/docker-compose.yml" --env-file "${directory}/.env" run wordpress-cli wp theme update --all; then
echo -e "\e[31m[ERROR] Failed to update WP THEMES in $(basename ${directory})\e[0m"
exit 1
fi
else
echo "Skipping $(basename ${directory}) because the container doesn't exist"
fi
else
echo "Skipping $(basename ${directory}) because docker-compose.yml doesn't exist"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment