#!/usr/bin/env bash # shellcheck shell=bash # shellcheck disable=SC2329,SC2317 # "function is never invoked" / "command is unreachable" are disabled file-wide on # purpose. Three groups of functions here are reached in ways the analyser does not # follow: the parallel workers (started with `&` inside run_batched), the mode # functions (dispatched through `case "$MODE"` in run_mode), and the trap handlers. # Each of them is exercised by the test suite, which is the check that actually # matters. SC2317 is the same finding under its pre-0.11 name, so a host with an # older shellcheck reports the same verdict as CI. ############################################################################### # WordPress Maintenance Automation # # File: Bash_WP-CLI_Update.sh # Project: Bash WP-CLI Update # Repository: https://github.com/paulmann/Bash_WP-CLI_Update # License: MIT (see LICENSE) # Version: 9.1.0 # # Purpose # Run WP-CLI maintenance operations (core, plugins, themes, database, cron, # Astra Pro licence) over every WordPress installation listed in a site file, # each one as the system user that owns it. # # What the 8.0 line adds on top of the 6.x hybrids (8.0.x patches are fixes, not # features: see CHANGELOG.md for what each one changed) # - read-only modes: --health (inventory: versions, pending updates, sizes), # --secrets (masked scan of wp-config.php), --doctor (host diagnostics); # - a restore path for the backups this tool creates: --list-backups and # --restore-db --yes (the missing half of "we take backups"); # - fleet controls: --retry, --max-failures, --run-timeout, --skip-if-no-updates, # --lock-wait, per-site URL detection for multisite without --url; # - production guards: --min-free-mb (disk guard before a dump), --nice and # --ionice (do not starve the web server), --backup-age-days, --backup-compress; # - observability: --report FILE (JSON run report), --json-log (one JSON object # per log line), --notify URL / --mail-to for run summaries, --completion SHELL; # - plugin and theme policy: --exclude-themes, --hold-plugins, --hold-themes; # - config keys for the same, so a run does not have to be spelled out in cron. # # Usage # Bash_WP-CLI_Update.sh [options] # Bash_WP-CLI_Update.sh --check [--site PATH] # Bash_WP-CLI_Update.sh --status # # Design notes (read before changing anything) # 1. No command is ever built by string concatenation. Every WP-CLI call is a # bash array that becomes argv. The only place a string is handed to a # shell is the user switch, and there the arguments travel as positional # parameters, so no escaping is required at all (see run_as_user). # 2. `printf %q` is a bash extension. It must never be used to build a string # for `sh -c`, `su -c` without `-s`, or `ssh`. If you need it, force the # interpreter: `su -s /bin/bash ...`. # 3. The licence value is never an argument of any process. It is handed # over through a temporary file whose *path* goes on the command line, # and the child shell reads the value at run time. `ps` never shows it, # no log line contains it, and it is unlinked before the next site runs. # The file is 0644 for the duration of one call, because the process # reading it has already switched to the site owner; licence_open() # documents why every alternative is worse. # 4. Configuration is data, not code. A config file is parsed as KEY=VALUE; # it is never sourced. A line with a shell metacharacter rejects the file. # 5. Exit codes are a contract (see below) and are actually honoured: usage # errors are 2, not 1. # 6. Read-only modes never take a backup and never hold a write lock on the # database: --health, --secrets, --list-plugins, --verify, --list-backups and # --check must be safe to run every minute from monitoring. # 7. Anything that can destroy data (--restore-db) is a two-key operation: it # needs an explicit --yes AND a backup taken in the same run, and it refuses # to run in dry-run silence. # # Exit codes # 0 success (with --fail-on=any: every operation on every site succeeded) # 1 operational error (at least one WP-CLI operation failed) # 2 usage error (bad command line) # 3 environment error (bash too old, wp-cli missing, lock held, not root) # 4 configuration error (unreadable or rejected config file) # # Requirements # bash 5.1+, GNU coreutils and findutils, WP-CLI, one of runuser/sudo/su, # root (to switch into site owners). `flock` and `timeout` are used when # present and degrade gracefully when they are not. ############################################################################### if [ -z "${BASH_VERSION:-}" ]; then printf 'ERROR: this script requires bash, but another shell started it.\n' >&2 printf ' Run it as: bash %s [options]\n' "${0##*/}" >&2 exit 3 fi if ((BASH_VERSINFO[0] < 5 || (BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] < 1))); then printf 'ERROR: %s requires bash 5.1 or newer (found %s).\n' \ "${0##*/}" "${BASH_VERSION:-unknown}" >&2 exit 3 fi # Deliberately without -e: in a maintenance tool that walks a fleet, one failing # site must not abort the run, and every error path below is handled by hand. # `-u` stays on, because an unset variable here is always a bug. set -uo pipefail shopt -s inherit_errexit 2>/dev/null || true ############################################################################### # 1. Constants, exit codes, defaults ############################################################################### PROG_NAME="${0##*/}" SCRIPT_VERSION='9.1.0' # A literal backtick, spelled by code point: the config parser has to reject it, # and writing the character directly would make this file fail the project's own # "no backticks" audit in tests/test_static.sh. BACKTICK=$'\140' EXIT_OK=0 EXIT_ERROR=1 EXIT_USAGE=2 EXIT_ENV=3 EXIT_CONFIG=4 resolve_script_dir() { local src="${BASH_SOURCE[0]}" dir while [ -L "$src" ]; do dir="$(cd -P "$(dirname "$src")" >/dev/null 2>&1 && pwd)" src="$(readlink "$src")" [ "${src#/}" = "$src" ] && src="${dir}/${src}" done cd -P "$(dirname "$src")" >/dev/null 2>&1 && pwd } SCRIPT_DIR="$(resolve_script_dir)" || { printf 'ERROR: cannot resolve the script directory\n' >&2; exit "$EXIT_ENV"; } unset -f resolve_script_dir readonly SCRIPT_DIR PROG_NAME # Built-in defaults. Everything here is overridable by config file, environment # or command line, in that order (see section 4). DEFAULT_WP_CLI_PATH='/usr/local/bin/wp' DEFAULT_SITES_FILE="${SCRIPT_DIR}/wp-found.txt" DEFAULT_DISCOVER_SCRIPT="${SCRIPT_DIR}/Find_WP_Senior.sh" DEFAULT_LOG_FILE="${SCRIPT_DIR}/wp_cli_manager.log" DEFAULT_ERROR_LOG_FILE="${SCRIPT_DIR}/wp_cli_errors.log" DEFAULT_LOCK_FILE="${SCRIPT_DIR}/.wp-cli-update.lock" DEFAULT_LOG_MAX_BYTES=5242880 # 5 MiB DEFAULT_LOG_KEEP=3 DEFAULT_ERROR_OUTPUT_LINES=20 DEFAULT_TIMEOUT=0 # 0 = no timeout DEFAULT_KILL_AFTER=30 DEFAULT_TIMEOUT_SIGNAL='TERM' DEFAULT_ALLOW_ROOT='auto' # auto | always | never DEFAULT_COLOR='auto' # auto | always | never DEFAULT_LOG_LEVEL='info' # debug | info | warn | error DEFAULT_FAIL_ON='any' # any | all | never DEFAULT_OUTPUT_FORMAT='table' # table | json | csv | tsv DEFAULT_ASTRA_SLUG='astra-addon' DEFAULT_MAX_SITES=0 # 0 = no limit DEFAULT_JOBS=1 # 1 = sequential; N = batches of N sites DEFAULT_BACKUP='off' # off | db | full DEFAULT_KEEP_BACKUPS=3 DEFAULT_BACKUP_DIR='' # empty =