Subversion Repositories sysadmin_scripts

Rev

Rev 41 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/env bash
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

echo "************************************************************************************************************"
echo "WordPress core file quick scan and repair.  (Written by Randell Miller of Creative Technology)";
echo "Version 1.0.0"
echo "************************************************************************************************************"

whoami=$(whoami)

extraflags=""

if [[ "$whoami" == "root" ]]; then
   extraflags=" --allow-root"
   echo -e "${RED}";
   echo "************************************************************************************************************"
   echo "NOTE:  You appear to be root.  Be sure to check all permissions and file ownership after script is complete."
   echo "************************************************************************************************************"
   echo -e "${NC}";
else
   echo ""
fi

#What version of WP are we using?
version=$(wp core version $extraflags)

echo "Found WordPress version $version.";

checkUpdate=$(wp core check-update $extraflags)

if [[ "$checkUpdate" == "Success: WordPress is at the latest version." ]]; then
   echo -e "${GREEN}WordPress is up to date.${NC}"
else
   echo -e "${YELLOW}It appears WordPress is not up to date.${NC}"
fi
echo ""

echo "Doing verify-checksums."

rm -f "tmp_changed" #clear tmp file
rm -f "tmp_shouldNotExist" #clear tmp file

wp core verify-checksums $extraflags 2>&1 | while read -r line; do
   if [[ "${line:0:46}" == "Warning: File doesn't verify against checksum:" ]]; then
      
      #We have a core file that's not right, need to refresh.
      echo -e "${RED}We have a file that has been changed (${line:47:${#line}}).${NC}"
      echo ${line:47:${#line}} >> "tmp_changed"
      
   elif [[ "${line:0:31}" == "Warning: File should not exist:" ]]; then
      
      #This is a file that doesn't belong here.  Set to quarentine.
      echo -e "${YELLOW}We have a file that shouldn't exist (${line:32:${#line}}).${NC}"
      echo ${line:32:${#line}} >> "tmp_shouldNotExist"
   fi
done

#get our changed files
if [ -f "tmp_changed" ]; then

   while read line; do
      changed+=($line)
   done < "tmp_changed"
   #clean up after ourselves
   rm -f "tmp_changed"
   
fi

#get our shouldNotExist files
if [ -f "tmp_shouldNotExist" ]; then

   while read line; do
      shouldNotExist+=($line)
   done < "tmp_shouldNotExist"
   #clean up after ourselves
   rm -f "tmp_shouldNotExist"

fi

#Are all of our Core Files ok?
echo ""
echo "Number of changed WordPress core files found: ${#changed[@]}";
if [[ ${#changed[@]} != "0" ]]; then
   #We have changed files.
   echo -e "${RED}Changed files detected.  Core file replace suggested.${NC}";
   read -p "Do you want to download a fresh copy of the WordPress core files? (y/N) " coreReplace
   if [[ "$coreReplace" == "Y" ]] || [[ "$coreReplace" == "y" ]]; then
      #Core replace
      wp core download --version=$version --force $extraflags
   fi
else
   echo -e "${GREEN}No changed core files detected.${NC}";
fi

#Do we have files that should not exist?
echo ""
echo "Number of files that should not exist found: ${#shouldNotExist[@]} ";
if [[ ${#shouldNotExist[@]} != "0" ]]; then
   #We have changed files.
   echo -e "${RED}Files detected where they should not.  Deletion suggested.${NC}";
   read -p "Do you want to Delete or Quarantine these files? {d/q/N) " handleExtras
   if [[ "$handleExtras" == "d" ]] || [[ "$handleExtras" == "D" ]]; then
      #Delete the files
      echo "Deleting files..."
      for i in "${shouldNotExist[@]}"
      do
         echo "Deleting $i";
          rm -f $i
      done
      echo -e "${GREEN}Files deleted.${NC}"
   elif [[ "$handleExtras" == "q" ]] || [[ "$handleExtras" == "Q" ]]; then
      #Quarantine files
      if [ ! -d "quarentine" ]; then
         mkdir "quarentine"
      fi
      now=`date +"%m_%d_%Y_%s"`
      qdir="quarentine/$now";
      if [ ! -d "$qdir" ]; then
         echo "Creating directory $qdir"
         mkdir $qdir
      fi
      
      for i in "${shouldNotExist[@]}"
      do
         mv $i "$qdir/${i##*/}"
         chmod -x "$qdir/${i##*/}"
      done
      echo -e "${GREEN}Files quarentined.${NC}"
   fi
else
   echo -e "${GREEN}No files detected where they should not be.${NC}";
fi

echo ""

#plugins
checkPlugins=$(wp plugin update --all --dry-run $extraflags | sed -n '/\bactive/p')

if [ -z "$checkPlugins" ]; then
   echo -e "${GREEN}All active plugins appear to be up to date.${NC}"
else
   echo -e "${RED}Plugins that have available updates:${NC}"
   echo "$checkPlugins"
fi
echo ""
#plugin checksums
rm -f "dirty"
re1="Checksum does not match"
wp plugin verify-checksums --all $extraflags 2>&1 | while read -r line; do
   if [[ "${line:0:45}" == "Warning: Could not retrieve the checksums for" ]]; then
      
      #This file doesn't have check sums.
      re="plugin\s*(\w+)"
      [[ $line =~ $re ]] && plugin="${BASH_REMATCH[0]}"
      echo -e "${YELLOW}Could not retrieve checksums for $plugin.${NC}"
      
      
   elif [[ "${line:0:7}" == "Success" ]]; then
      
      #Success Message
      echo -e "${GREEN}All Plugins checksums that could be tested matched.${NC}"
   elif [[ $line =~ $re1 ]]; then
      echo -e "${RED}$line${NC}"
      t=( $line )
      echo "${t[0]}" >> "dirty"
   fi
done

echo ""

if [ -f "dirty" ]; then
   mapfile -t myArray < dirty
   UNIQ_myArray=($(printf "%s\n" "${myArray[@]}" | sort -u))
   for i in "${UNIQ_myArray[@]}"
   do
      echo "The following plugin failed checksum verification:"
      wp plugin status $i
      echo ""
   done
   echo -e "${RED}At least one plugin failed checksum matching!${NC}"
else
   echo -e "${GREEN}All plugins passed checksum matching.${NC}"
fi



echo ""
echo "Script complete."