Skip to content

Instantly share code, notes, and snippets.

@Braunson
Created June 27, 2024 02:35
Show Gist options
  • Select an option

  • Save Braunson/31e02176106da8afbd57b3cf0a884bce to your computer and use it in GitHub Desktop.

Select an option

Save Braunson/31e02176106da8afbd57b3cf0a884bce to your computer and use it in GitHub Desktop.
WordPress bash script to leverage WP CLI to check core integrity of multiple sites on a shared server and email a report
#!/usr/bin/bash
# Email settings
EMAIL="foo@bar.ca"
SUBJECT="WordPress Core Integrity Check Results"
TMP_FILE="/tmp/wp_integrity_check_results.txt"
# Initialize flags
DISPLAY_ONLY=false
DEBUG=false
INCLUDE_SUCCESS=false
# Parse arguments
for arg in "$@"; do
case $arg in
--display)
DISPLAY_ONLY=true
;;
--verbose)
DEBUG=true
;;
--include-success)
INCLUDE_SUCCESS=true
;;
esac
done
# Clean up previous temporary file
rm -f $TMP_FILE
# Function to check integrity
check_wp_integrity() {
local wp_path="$1"
if [ -f "$wp_path/wp-includes/version.php" ]; then
if [ "$DEBUG" = true ]; then
echo "Checking integrity for: $wp_path" >> $TMP_FILE
# Debugging: Record current user and environment path
echo "Running as user: $(whoami)" >> $TMP_FILE
# echo "Using PATH: $PATH" >> $TMP_FILE
fi
# Run wp core verify-checksums and capture output
wp_cli_output=$(sudo -u www-data -- wp core verify-checksums --path="$wp_path" 2>&1)
# Check if the command was successful
if echo "$wp_cli_output" | grep -q "WordPress installation verifies against checksums"; then
if [ "$INCLUDE_SUCCESS" = true ]; then
echo "" >> $TMP_FILE # Add a blank line above the success heading
echo -e "\033[0;32m✔ WordPress installation verifies against checksums for $wp_path\033[0m" >> $TMP_FILE
fi
else
echo "" >> $TMP_FILE # Add a blank line above the error heading
echo -e "\033[0;31m❌ Error checking integrity for $wp_path:\033[0m" >> $TMP_FILE
echo "$wp_cli_output" | sed 's/^/ → /' >> $TMP_FILE # Nest error messages with spaces
fi
fi
}
# Check /var/www/html/*/docs
for wp_path in /var/www/html/*/docs; do
if [ -d "$wp_path" ]; then
check_wp_integrity "$wp_path"
fi
done
# Check /var/www/html/*/public_html
for wp_path in /var/www/html/*/public_html; do
if [ -d "$wp_path" ]; then
check_wp_integrity "$wp_path"
fi
done
# Display the results
if [ -s $TMP_FILE ]; then
if [ "$DISPLAY_ONLY" = true ]; then
echo "Integrity Check Results:"
# Add a blank line for separation
cat $TMP_FILE
else
mail -s "$SUBJECT" $EMAIL < $TMP_FILE
fi
else
if [ "$DISPLAY_ONLY" = true ]; then
echo "No warnings or errors found during integrity check."
fi
fi
# Clean up
rm -f $TMP_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment