File: /home/httpd/html/sexcamstube.com/public_html/wpupdate.sh
#!/bin/sh
# WordPress Upgrader (MojoHost)
# Description: Makes a backup of just the WordPress files, and then performs a WordPress upgrade.
# Revision: 13
# WordPress Download URL
WPDOWNLOADURL="http://wordpress.org/latest.tar.gz"
# WordPress downloaded source filename
WPFILENAME="latestWordPress.tar.gz"
########## Functions - BEGIN
# Colors
BLACK='\e[1;30m'
RED='\e[1;31m'
GREEN='\e[1;32m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
MAGENTA='\e[1;35m'
CYAN='\e[1;36m'
WHITE='\e[1;37m'
ENDCOLOR='\e[0m'
trim() {
# trims the beginning whitespace, and the trailing whitespace, up to the first newline, in which it truncates the input. So, this isn't any good for multiline trimming being submitted in one shot.
echo $1
}
randomString() {
##### usage: $(randomString 12)
##### where "12" is the number of random characters to return
if [[ "$1" -ge "1" ]]; then
echo "`</dev/urandom tr -dc A-Za-z0-9 | head -c${1}`"
else
echo 'fatal error: integer must be greater than zero for randomString function.' >&2
exit 182
fi
}
########## Functions - END
########## Main Code - BEGIN
echo
# Get the install directory (else, our current working directory)
installDir="`pwd`"
echo -e "Attempting to upgrade WordPress in: ${GREEN}${installDir}${ENDCOLOR}"
# Previous WordPress Install Check
if [ ! -d wp-admin ]; then
echo -e "${RED}WordPress not found. Exiting!${ENDCOLOR}"
exit 1
fi
# Install the needed CentOS components.
echo -n "Checking/Installing required packages ..... "
yum -yt -e 0 install expect.`uname -i`|grep -v "Nothing to do\|Setting up Install Process\|Excluding Packages in global exclude list\|already installed and latest version\|^Finished$\|^Parsing package install\|^Reading repository\|Setting up repositories"
echo -e "${GREEN}Done${ENDCOLOR}"
# Purge the old source file.
if [ -f /tmp/$WPFILENAME ]; then
echo -n "Removing old WordPress source file ........ "
rm -rf /tmp/$WPFILENAME
echo -e "${GREEN}Done${ENDCOLOR}"
fi
# Purge the old source folder.
if [ -d /tmp/wordpress ]; then
echo -n "Removing old WordPress source folder ...... "
rm -rf /tmp/wordpress && echo -e "${GREEN}Done${ENDCOLOR}"
fi
# Download latest WordPress version.
if [ ! -f /tmp/$WPFILENAME ]; then
echo -n "Downloading latest WordPress .............. "
wgetSourceBool=0
DOWNLOADATTEMPTS=1
MAXDOWNLOADATTEMPTS=5
while [ "$wgetSourceBool" -eq "0" ] && [ "$DOWNLOADATTEMPTS" -lt "$MAXDOWNLOADATTEMPTS" ]; do
wgetSourceBool="`wget $WPDOWNLOADURL -O /tmp/$WPFILENAME 2>&1|egrep -i '(saving|saved)'|wc -w`"
wgetSourceBool="$(trim $wgetSourceBool)"
# echo "wgetSourceBool: '$wgetSourceBool'"
# echo "wgetSourceBool: '$(trim $wgetSourceBool )'"
DOWNLOADATTEMPTS="`expr ${DOWNLOADATTEMPTS} + 1`"
if [ "$wgetSourceBool" -eq "0" ]; then
sleep 1
echo -n "Attempt $DOWNLOADATTEMPTS/$MAXDOWNLOADATTEMPTS... "
wgetSourceBool="`wget $WPDOWNLOADURL -O /tmp/$WPFILENAME 2>&1|grep \"Saving to\\|unspecified\"|wc -w`"
wgetSourceBool="$(trim $wgetSourceBool)"
fi
done
if [ "$wgetSourceBool" -gt "0" ]; then
echo -e "${GREEN}Done${ENDCOLOR}"
#echo -n "Installing WordPress base ................. "
tar -C /tmp -xzf /tmp/$WPFILENAME
# rm -f /tmp/$WPFILENAME
else
echo -e "${RED}Failed!${ENDCOLOR}"
echo ""
echo="`wget $WPDOWNLOADURL -O /tmp/$WPFILENAME` 2>&1"
echo "Unable to download the WordPress source code from: $WPDOWNLOADURL"
echo ""
exit 1
fi
fi
DBNAME="`grep DB_NAME wp-config.php|cut -f2 -d','|cut -f1 -d')'|tr -d "\'"|tr -d ' '`"
DBUSER="`grep DB_USER wp-config.php|cut -f2 -d','|cut -f1 -d')'|tr -d "\'"|tr -d ' '`"
DBPASS="`grep DB_PASSWORD wp-config.php|cut -f2 -d','|cut -f1 -d')'|tr -d "\'"|tr -d ' '`"
DBHOST="`grep DB_HOST wp-config.php|cut -f2 -d','|cut -f1 -d')'|tr -d "\'"|tr -d ' '`"
# Backup Folder Creation
BACKUPDIRBASE="wpbackup-mojo"
BACKUPDIR="$BACKUPDIRBASE/`date +%Y%m%d%H%M`"
if [ ! -d $BACKUPDIR ]; then
mkdir -p $BACKUPDIR
fi
# Backup the files that will be overwritten by the new WordPress tar contents
echo -n "Making backup of the destination files .... "
#find . -path "./wp-content/uploads*" -prune -o -print|cpio -dump $BACKUPDIR
FILES="`tar -tzf /tmp/${WPFILENAME} | grep -v '/$' | sed 's~^wordpress/~~g'`"
rm -f /tmp/$WPFILENAME
backedUpFiles="`tar -czvf ${BACKUPDIR}/wp-backup.tar.gz $FILES --ignore-failed-read 2>&1`"
if [ -f ${BACKUPDIR}/wp-backup.tar.gz ]; then
echo -e "${GREEN}Done (`echo "${backedUpFiles}" | grep -v 'No such file or directory'| wc -l` Files)${ENDCOLOR}"
else
echo -e "${RED}${BACKUPDIR}/wp-backup.tar.gz not found. Tar backup failed. Critical Error, Exiting!${ENDCOLOR}"
fi
# MySQLDump the WordPress database
echo -n "Dumping MySQL database .................... "
mysqldumpResult="`mysqldump -u${DBUSER} -p${DBPASS} -h${DBHOST} --database ${DBNAME} > ${BACKUPDIR}/db.sql`"
if [ "$?" -eq "0" ] && [ -f ${BACKUPDIR}/db.sql ]; then
echo -e "${GREEN}Done${ENDCOLOR}"
else
if [ ! -f ${BACKUPDIR}/db.sql ]; then
echo -e "${RED}${BACKUPDIR}/db.sql not found. Database dump failed. Critical error. Exiting!${ENDCOLOR}"
else
echo -e "${RED}A mysqldump error occurred: $mysqldumpResult${ENDCOLOR}"
fi
fi
# Applying Owner/Group/Permission fixes to the source files first.
apacheGroup="`grep Group /etc/httpd/conf/httpd.conf |grep -v "#"|awk '{print $2}'|head -n 1`"
if [ "$apacheGroup" == "nobody" ] || [ "$apacheGroup" == "apache" ]; then
if [ -f wp-config.php ]; then
ClientName="`grep :\`stat . --format=%u\`: /etc/passwd|cut -f1 -d:|head -n1`"
UIDName=$ClientName
# If user exists in /etc/passwd, use their UID and GID as the chown and chgrp IDs.
if [ "`grep "^${UIDName}:" /etc/passwd|awk -F: '{print $1}'|wc -l`" -eq "1" ]; then
InstallUID=`grep "^${UIDName}:" /etc/passwd|awk -F: '{print $3}'`
InstallGID=`grep "^${UIDName}:" /etc/passwd|awk -F: '{print $4}'`
else
echo -e " ${RED}I'm sorry but that is not a valid user.${ENDCOLOR}"
exit 1
fi
chown -R ${InstallUID}:${InstallGID} /tmp/wordpress/
chgrp -R $apacheGroup /tmp/wordpress/wp-content
chmod -R 0775 /tmp/wordpress/wp-content
else
echo -e "${RED}wp-config.php not found. Please run this in a folder that has WordPress installed.${ENDCOLOR}"
exit 1
fi
else
echo -e "${RED}Unable to determine the group for the Apache web server. Exiting.${ENDCOLOR}"
exit 1
fi
# Overwrite the core WordPress files
echo -n "Updating core WordPress files ............. "
/bin/cp -a /tmp/wordpress/* .
#chmod 775 wp-content/uploads
#chmod 664 .htaccess
chmod -x .htaccess
#chown ${InstallUID}:$apacheGroup .htaccess
echo -e "${YELLOW}Done${ENDCOLOR}"
# Set the salts if they were defined but never randomized
echo -n "Randomizing salt strings where needed ..... "
while [ "`grep -c 'put your unique phrase here' wp-config.php`" -gt "0" ]; do
# echo "phrases still needing to be replaced: `grep -c 'put your unique phrase here' wp-config.php`"
tempRandomString=$(randomString 100)
# sed inline replace fails on centos 3.9 for first occurrence replacements. it simply doesn't write to file
#sed -i -e "0,/put your unique phrase here/s/put your unique phrase here/${tempRandomString}/" wp-config.php
sed -e "0,/put your unique phrase here/s/put your unique phrase here/${tempRandomString}/" wp-config.php > wp-config.php.tmp
cat wp-config.php.tmp > wp-config.php
done
echo -e "${YELLOW}Done${ENDCOLOR}"
rm -f wp-config.php.tmp
# Installing wp-content/uploads no-php-or-html .htaccess
echo -n "Installing no-php-or-html .htaccess ....... "
htaccessContents="`wget --quiet wp-content/uploads/.htaccess http://fs01/files/no-php-or-html.htaccess -O -`"
if [ ! -f "wp-content/uploads/.htaccess" ]; then
# wget -O wp-content/uploads/.htaccess http://fs01/files/no-php-or-html.htaccess
WGETRESULT="`wget -O wp-content/uploads/.htaccess http://fs01/files/no-php-or-html.htaccess 2>&1|grep -c " saved "`"
if [ "${WGETRESULT}" -eq "1" ]; then
echo -e "${GREEN}Done${ENDCOLOR}"
else
echo -e "${RED}Failed! Unable to retrieve .htaccess from fs01.${ENDCOLOR}"
fi
elif [ "`cat wp-content/uploads/.htaccess`" == "${htaccessContents}" ]; then
echo -e "${GREEN}Done (wp-content/uploads/.htaccess content is identical)${ENDCOLOR}"
else
echo -e "${RED}Failed! (wp-content/uploads/.htaccess content is different)${ENDCOLOR}"
echo -e "${YELLOW}========== Contents of 'wp-content/uploads/.htaccess' - BEGIN${ENDCOLOR}"
echo -e "${YELLOW}`cat wp-content/uploads/.htaccess`${ENDCOLOR}"
echo -e "${YELLOW}========== Contents of 'wp-content/uploads/.htaccess' - END${ENDCOLOR}"
fi
## Setting up AutoFTP
#echo -n "AutoFTP Creation .......................... "
#ftpasswdPath="`which ftpasswd 2>/dev/null`"
#ftpasswdPath="$(trim $ftpasswdPath)"
#if [ "$ftpasswdPath" == "" ]; then
# echo -e "Installing autoupdate FTP account ......... ${RED}Failed! The 'ftpasswd' command could not be found!${ENDCOLOR}"
#else
# # only setup auto ftp for non cpanel and non direct admin installs. Which "implies" it's a MojoHost install.
# if [[ "$(isCpanelInstalled)" -eq "0" ]] || [[ "$(isDirectAdminInstalled)" -eq "0" ]]; then
# #define('FS_METHOD', 'ftpext');
# #define('FTP_HOST', 'localhost');
# #define('FTP_USER', 'username');
# #define('FTP_PASS', 'password');
#
# echo -n "Installing autoupdate FTP account ......... "
# if [[ "`grep "'FTP_HOST'\|'FTP_USER'\|'FTP_HOST'" wp-config.php | grep -v -P '^[ \t]*#'|wc -l`" -eq "0" ]]; then
# ftpPassword="$(randomString 40)"
# # echo "${ftpPassword}" | ftpasswd --name ${DBNAME}_autoftp --home ${installDir} --uid ${InstallUID} --gid ${InstallGID} --stdin
#
# FTPSETUPRESULT="`echo "${ftpPassword}" | ftpasswd --name ${DBNAME}_autoftp --home ${installDir} --uid ${InstallUID} --gid ${InstallGID} --stdin 2>&1`"
#
# FTPSETUP="`echo "${FTPSETUPRESULT}"| grep -c "ftpasswd: entry created"`"
# if [ "${FTPSETUP}" -eq "1" ]; then
# # auto ftp account was setup
# echo "define('FTP_HOST', 'localhost');" >> wp-config.php
# echo "define('FTP_USER', '${DBNAME}_autoftp');" >> wp-config.php
# echo "define('FTP_PASS', '${ftpPassword}');" >> wp-config.php
# echo -e "${GREEN}Done${ENDCOLOR}"
# else
# # auto ftp account setup failed
# echo -e "${RED}Failed!${ENDCOLOR}"
#
# echo
# echo "FTPSETUPRESULT - BEGIN"
# echo "${FTPSETUPRESULT}"
# echo "FTPSETUPRESULT - END"
# echo
# fi
# fi
# fi
#fi
#echo -e "${MAGENTA}Future Feature${ENDCOLOR}"
## Reset Owner/Group/Permissions
#echo -n "Resetting Owner/Group/Permissions ......... "
#echo -e "${MAGENTA}Future Feature${ENDCOLOR}"
## Chowning backup directory contents to the user
chown ${InstallUID}:${InstallGID} $BACKUPDIRBASE $BACKUPDIR ${BACKUPDIR}/db.sql ${BACKUPDIR}/wp-backup.tar.gz
echo "WordPress Backup Directory is: ${BACKUPDIR}"
ls -lh ${BACKUPDIR}
########## Main Code - END
###building a restore script
cat > ${BACKUPDIR}/restore.sh << EOFBA
#!/bin/bash
###################################################################################################
# auto generated restore script
# this script will restore the wp backup in this directory
# It will overwrite any files from ${BACKUPDIR}/wp-backup.tar.gz
# into ${installDir}
# and re-import the db backup.
#
# !!!!there is no turning back. The current live WP is not backed up when you restore!!!!
#
###################################################################################################
cd ${installDir}
tar xzf ${BACKUPDIR}/wp-backup.tar.gz -C ${installDir}
mysql -u${DBUSER} -p${DBPASS} -h${DBHOST} ${DBNAME} << ${BACKUPDIR}/db.sql
EOFBA