Wordpress update script for cpanel

/Home /Journal /Services /Projects
#!/bin/bash

#################################################################################################
#																								#
#																								#
# 1. Provided a path /home/user/path-to-wordpress-folder/ finds the user & changes into the path#
# 2. Checks for wp-config.php existance & extracts db data										#
# 3. Finds site URL to crawl the pages															#
# 4. Enables debug																				#
# 5. Adjusts hosts file 																		#
# 6. Detects redirects and adapts																#
# 7. Generate db & files backup																	#
# 8. Attempts to update installation															#
# 9. fixes permissions and ownergroup															#
#10. Crawls pages again to look for errors														#
#11. compares crawls and if different report an issue with entries from error_log				#
#12. reverts changes																			#
#																								#
#																								#
#################################################################################################

URLRegex='(https?|ftp|file)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]'

if { cd "$1"; }; then

	# Extract user from path
	if [ "$1" == "" ]; then
		CPUSER=$(pwd | tr "/" " "| awk '{print $2}')
	else
		CPUSER=$(echo "$1" | tr "/" " "| awk '{print $2}')
	fi

	# Does wp-config.php exist?
    if test -f "wp-config.php"; then
		DB_NAME=$(grep "DB_NAME" wp-config.php | awk -F "'" '{print $4}')
		DB_USER=$(grep "DB_USER" wp-config.php | awk -F "'" '{print $4}')
		#DB_PASSWORD=$(grep "DB_PASSWORD" wp-config.php | awk -F "'" '{print $4}')
    else
        echo "wp-config.php file is missing!"

		# Generates a backup file and removes content
		while true; do
		read -p "Backup directory and remove it?(y/n) " yn
		case $yn in
			[Yy]* ) zip -qrm ../wp-"$(date +%Y%m%d%H%M%S)".zip .; echo "Backup generated and folder removed"; exit;;
			[Nn]* ) echo "exiting...";exit;;
			* )  echo "invalid choice";;
		esac
		done
    fi

    # Get site URL from Wordpress installation
    URL=$(wp option get siteurl --allow-root)

    if [[ $URL =~ $URLRegex ]]; then
        echo "URL: $URL"
    else
		URL="$2"
		if [[ $URL =~ $URLRegex ]]; then
			echo "URL: $URL"
		else
			echo "Something URL provided by wp-config.php incorrect and no domain was provided as 2nd argument."
			echo "URL: $URL"
			exit 1
		fi
    fi

	# Disable HTTPS while in the process
	MURL=$(sed 's/https/http/' <<< $URL)
	wp option update siteurl "$MURL" --allow-root

	# Enable debug & set path
	wp config set WP_DEBUG true --raw --allow-root
	wp config set WP_DEBUG_LOG \"./error_log\" --raw --allow-root

	#Hosts file backup, just in case...
	HOSTS=$(cat /etc/hosts)

	# Set domain in hosts file to access website locally
	IP=$(whmapi1 get_shared_ip user=$CPUSER | grep "ip:" | awk '{print $2}')
	DOMAIN=$(echo "$URL" | awk -F "/" '{print $3}')
	echo "Adding $IP $DOMAIN to /etc/hosts file..."
	echo "$IP $DOMAIN" >> /etc/hosts

    # Crawl pages via CURL and saves status code
    FR=""
    for i in $(wp post list --post_type=page --allow-root | grep "publish" |awk -F "\t" '{print $3}'); do
        RESULT=$(curl -I "$MURL/$i/" | head -n 1 | awk '{print $2}')

		# Detect 301 Redirect
		if [ "$RESULT" == "301" ] || [ "$RESULT" == "308" ] || [ "$RESULT" == "307" ]; then
			RESULT=$(curl -I "$MURL/$i/" | grep "location" | awk '{print $2}')
		fi
        FR+="$RESULT "
    done

    # Generate database and files backup
    echo "Generating a backup..."
    mysqldump "$DB_NAME" > "$DB_NAME.sql"
    zip -qr wp-"$(date +%Y%m%d%H%M%S)".zip .
	rm -f "$DB_NAME.sql"
    echo "Backup complete."

	# Attempt to update
    echo "Attempting to update Wordpress installation..."
    UPDATE=$(wp core update --allow-root)
	UPDATESTATUS=$(echo "$UPDATE"|awk -F ":" '{print $1}')
	if [ "$UPDATESTATUS" == "Success" ]; then
		echo "update complete."
	else

		echo "Something went wrong!"
		echo "$UPDATE"
		while true; do
		read -p "move backup to upfolder and remove content? (y/n) "
		case $yn in
			[Yy]* ) mv wp-*.zip ../; rm -fr .; mysql -e "DROP DATABASE $DB_NAME;"; mysql -e "DROP USER $DB_USER";echo "Backup moved and content removed!"; exit;;
			[Nn]* ) echo "exiting...";exit;;
			* )  echo "invalid choice";;
		esac
		done
		fixperms -a "$CPUSER"
		fixownergroup "$CPUSER"
		exit 1;
	fi

	# Fix permissions
    fixperms -a "$CPUSER"
    fixownergroup "$CPUSER"

	echo "checking website status..."
    LR=""
    for i in $(wp post list --post_type=page --allow-root | grep "publish" |awk -F "\t" '{print $3}'); do
        RESULT=$(curl -I "$MURL/$i/" | head -n 1 | awk '{print $2}')

		# Detect 301 Redirect
		if [ "$RESULT" == "301" ] || [ "$RESULT" == "308" ] || [ "$RESULT" == "307" ]; then
			RESULT=$(curl -I "$MURL/$i/" | grep "location" | awk '{print $2}')
		fi
        LR+="$RESULT "
    done

	# Revert changes to Wordpress config
	wp option update siteurl "$URL" --allow-root
	wp config set WP_DEBUG false --raw --allow-root

	# Removes /etc/hosts entry
	echo "$HOSTS" > /etc/hosts

	# Do results match?
    if ! [[ $FR == "$LR" ]]; then
        echo "Something went wrong!";
		echo "First Crawl Results: $FR"
		echo "Last Crawl Results: $LR"
		tail -n 100 error_log
    fi
else
    echo "directory does not exist!";
fi