#!/bin/bash # # WordPress Update Notify 0.1, January 2010 # by Philipp Heckel; No license, feel free use it! # # WP-UN checks whether a local WordPress installation is # up-to-date and notifies the administrator if a new version # is available. # # Original blog post: # http://blog.philippheckel.com/2010/01/29/wp-un-wordpress-version-update-notification-with-cron/ # # REQUIREMENTS # Local mail server, e.g. postfix, sendmail, ... # # USAGE # $ ./wp-un [--test] # # AS CRONJOB # 0 6 * * * /usr/local/bin/wp-un /var/www/myblog admin@example.com # ### Parse Parameters #################################### PATH="/bin:/sbin:/usr/bin:/usr/sbin" if [ $# -le 1 ]; then echo "Usage: $0 [--test] " echo " e.g. $0 /var/www/wordpress mymail@example.com" exit 1 fi if [ "$1" == "--test" ]; then TEST=1 WORDPRESS="$2" NOTIFY_EMAIL="$3" else WORDPRESS="$1" NOTIFY_EMAIL="$2" fi URL="http://www.wordpress.org/latest.tar.gz" VERSION_FILE="$WORDPRESS/wp-includes/version.php" if [ ! -f "$VERSION_FILE" ]; then echo "ERROR: No WordPress installation found at $WORDPRESS" exit 2 fi if [ ! -x "$(which sendmail)" ]; then echo "ERROR: No sendmail compatible mail server found." exit 3 fi ### Script Start ########################################### ## Determine installed version if [ -n "$TEST" ]; then echo -n "Checking installed version... "; fi INSTALLED=$(cat "$VERSION_FILE" \ | grep '\$wp_version[ ]*=' \ | sed -r "s/.+'(.+)';/\1/") if [ -n "$TEST" ]; then echo "WordPress $INSTALLED"; fi ## Determine latest version if [ -n "$TEST" ]; then echo -n "Checking latest version... "; fi # Alternative to 'wget': curl -I "$URL" 2> /dev/null LATEST=$(wget -S --spider "$URL" 2>&1 \ | grep -o -E 'Content-Disposition.+.tar.gz' \ | sed -r 's/.+filename=wordpress-(.+)\.tar\.gz/\1/') if [ -n "$TEST" ]; then echo "WordPress $LATEST"; fi ## Notify if versions differ if [ -n "$TEST" -o "$INSTALLED" != "$LATEST" ]; then if [ -n "$TEST" ]; then if [ "$INSTALLED" != "$LATEST" ]; then echo -n "Update required; Sending notification to $NOTIFY_EMAIL... " else echo "Update not necessary; WordPress is up-to-date." echo -n "TEST-flag enabled: sending notfication to $NOTIFY_EMAIL... " fi fi # Send to e-mail address HOSTNAME="$(hostname)" # Alternative to 'sendmail': mailx -s "..." $NOTIFY_EMAIL echo -e "To: $NOTIFY_EMAIL\nSubject: Notification: WordPress $LATEST available\n\n" \ "The WordPress installation on host $HOSTNAME needs an update:\n\n" \ " Installed Version: WordPress $INSTALLED\n" \ " at: $WORDPRESS\n\n" \ " Latest Version: WordPress $LATEST\n" \ " Download: $URL" \ | sendmail -t "$NOTIFY_EMAIL" if [ -n "$TEST" ]; then echo "done."; fi fi