File: //proc/1284358/cwd/bigscoots/wpo_theworks.sh
#!/bin/bash
source /bigscoots/includes/common.sh
exit_on_error() {
exit_code=$1
last_command=${@:2}
if [ "$exit_code" -ne 0 ]; then
>&2 echo "\"${last_command}\" command failed with exit code ${exit_code}."
exit "$exit_code"
fi
}
if grep -qi wpe_ wp-config.php
then
dbprefix=$(wpcli config get table_prefix)
mv wp-config.php wp-config.php.wpe
cp -rf ../backup/wp-config.php-orig wp-config.php
wpcli config set table_prefix ${dbprefix}
echo "######################################################################"
echo "YOU ARE TRYING TO USE A WP ENGINE wp-config.php, will use the backup wp-config.php"
echo "Please also scroll through the entire wp-config.php.wpe for any NON WPE options and pull those over if needed"
echo "If you are unsure of anything, ask in #team-chat / @justin"
echo "######################################################################"
fi
if [ -d /usr/local/cpanel/ ]
then
echo "######################################################################"
echo "You are trying to run this script on a cPanel server so going to quit."
echo "######################################################################"
exit
fi
date=$(date "+%Y-%m-%dT%H_%M_%S")
serverip=$($(which ifconfig) | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | head -1)
if [ -f /etc/csf/csf.allow ] && ! grep -q 67.202.70.92 /etc/csf/csf.allow; then
csf -a 67.202.70.92
fi
if [ -f /etc/csf/csf.allow ] && ! grep -q 69.162.173.37 /etc/csf/csf.allow; then
csf -a 69.162.173.37
fi
# Pre-checks
# remove add_filter should be used in a mu-plugin, otherwise breaks wp-cli
sed -i 's/add_filter/\/\/add_filter/g' wp-config.php
sed -i '/wp-salt.php/d' wp-config.php
if ! hash dos2unix 2>/dev/null
then
yum -y install dos2unix > /dev/null 2>&1
fi
dos2unix wp-config.php > /dev/null 2>&1
NGINX=$(which nginx)
### CPANEL Backup - site most reside in public_html
if [[ $1 == cpanel ]]
then
if [[ ! $(pwd | sed 's/\// /g' | grep -oE '[^ ]+$') == public ]]; then
echo "You are not curently in the public directory please cd into the proper directory then try again."
exit
fi
NEW_DB_NAME=$(wpcli config get DB_NAME)
NEW_DB_USER=$(wpcli config get DB_USER)
NEW_DB_PASSWORD=$(wpcli config get DB_PASSWORD)
backup=$(echo *.tar.gz | sed 's/.tar.gz//g')
mv "${backup}".tar.gz ..
rm -rf *
mv ../"${backup}".tar.gz .
tar -zxvf "$backup".tar.gz
sed -i '/gd-config.php/d' "$backup"/homedir/public_html/wp-config.php
sed -i '/SiteGround/d' "$backup"/homedir/public_html/wp-config.php
DB_NAME=$(wpcli config get DB_NAME --path="$backup"/homedir/public_html/)
mv "$backup"/mysql/"$DB_NAME".sql bigscoots.sql
mv "$backup"/homedir/public_html/* .
mv "$backup"/homedir/public_html/.htaccess .
fi
### WPENGINE -- Must be WPENGINE downloaded backup
if [[ $1 == wpe ]]
then
if [[ ! $(pwd | sed 's/\// /g' | grep -oE '[^ ]+$') == public ]]; then
echo "You are not curently in the public directory please cd into the proper directory then try again."
exit
fi
if [ -f site-archive-*.zip ]; then
mv site-archive-*.zip wp-config.php ..
rm -rf * .htaccess
mv ../site-archive-*.zip .
unzip site-archive-*.zip
fi
DB_CHARSET=$(wpcli config get DB_CHARSET)
DB_COLLATE=$(wpcli config get DB_COLLATE)
TABLE_PREFIX=$(wpcli config get table_prefix)
mv wp-config.php wp-config.php.wpe
mv ../wp-config.php .
if [ ! -f bigscoots.sql ]; then
mv wp-content/mysql.sql bigscoots.sql
fi
rm -rfv _wpeprivate
wpcli config set DB_CHARSET "$DB_CHARSET"
wpcli config set DB_COLLATE "$DB_COLLATE"
wpcli config set table_prefix "$TABLE_PREFIX"
wpcli db reset --yes
fi
### FLYWHEEL -- Must be a FLYWHEEL downloaded backup -- Need db prefix
if [[ $1 == flywheel ]]
then
read -e -p "Enter db table prefix:" -i "wp_" TABLE_PREFIX
TABLE_PREFIX=${TABLE_PREFIX:-wp_}
rm -rfv wp-content
unzip $(ls *.zip)
wpcli db reset --yes
wpcli config set table_prefix "$TABLE_PREFIX"
mv backup.sql bigscoots.sql
rsync -ahv --exclude wp-content files/ .
mv files/wp-content .
rm -rfv files
fi
### FRESH INSTALLS
if [[ $1 == fresh ]]
then
wpcli search-replace http: https: --skip-columns=guid,user_email
else
### Runs on everything except fresh installs
if [ ! -f wp-config.php ]; then
echo "wp-config.php doesn't exist, quitting. "
exit 1
fi
# remove unnecessary files / plugins
rm -rfv \
flywheel-config \
.user.ini \
wordfence-waf.php \
wp-content/cache \
wp-content/db.php \
wp-content/mu-plugins/cdn-cache-management* \
wp-content/mu-plugins/disable-cache-purge.php \
wp-content/mu-plugins/endurance* \
wp-content/mu-plugins/et-safe-mode \
wp-content/mu-plugins/force-strong-passwords \
wp-content/mu-plugins/*foundry* \
wp-content/mu-plugins/includes \
wp-content/mu-plugins/kinsta-mu-plugins* \
wp-content/mu-plugins/liquidweb_mwp.php \
wp-content/mu-plugins/lw_disable_nags.php \
wp-content/mu-plugins/lw-varnish-cache-purger.php \
wp-content/mu-plugins/mu-includes.php \
wp-content/mu-plugins/mu-plugin.php \
wp-content/mu-plugins/nexcess-mapps* \
wp-content/mu-plugins/pagely-* \
wp-content/mu-plugins/scribe \
wp-content/mu-plugins/pressable* \
wp-content/mu-plugins/site-management \
wp-content/mu-plugins/slt-force-strong-passwords.php \
wp-content/mu-plugins/stop-long-comments.php \
wp-content/mu-plugins/SupportCenterMUAutoloader.php \
wp-content/mu-plugins/synth* \
wp-content/mu-plugins/sysmsg \
wp-content/plugins/autoupdater \
wp-content/mu-plugins/ts-fail2spam.php \
wp-content/mu-plugins/wp-cli-login-server.php \
wp-content/mu-plugins/wpe* \
wp-content/mu-plugins/wpengine-common \
wp-content/mu-plugins/wp-nc-easywp* \
wp-content/mu-plugins/wp-stack-cache.php \
wp-content/mu-plugins/hostinger* \
wp-content/object-cache.php \
wp-content/plugins/bluehost-wordpress-plugin \
wp-content/plugins/hgwalkme \
wp-content/plugins/wp-plugin-hostgator \
wp-content/plugins/varnish-http-purge \
wp-content/plugins/litespeed-cache \
wp-content/plugins/mojo-marketplace-wp-plugin \
wp-content/plugins/wp-site-migrate \
wp-content/sgo-config.php \
wp-content/uploads/siteground-optimizer-assets \
>> ../log/theworksrun_"${date}".log
echo "#################################" >> ../log/theworksrun_"${date}".log
sed -i '/gd-config.php/d' wp-config.php
sed -i '/SiteGround/d' wp-config.php
# check for symlinked uploads directory
if [ -L wp-content/upgrade ]
then
rm -rf wp-content/upgrade
fi
if [ -n "$NEW_DB_NAME" ] && [ -n "$NEW_DB_USER" ] && [ -n "$NEW_DB_PASSWORD" ]
then
wpcli config set DB_NAME "${NEW_DB_NAME}"
wpcli config set DB_USER "${NEW_DB_USER}"
wpcli config set DB_PASSWORD "${NEW_DB_PASSWORD}"
unset NEW_DB_NAME NEW_DB_USER NEW_DB_PASSWORD
fi
# Execute SSH command and capture output
RESULT=$(if [ -f /root/.bigscoots/db/info ] && grep -q 'db: remote' /root/.bigscoots/db/info; then
echo 'remote'
DBHOST=$(grep dbhost /root/.bigscoots/db/info | awk '{print $2}')
DBCLIENT=$(grep dbclient /root/.bigscoots/db/info | awk '{print $2}')
echo ${DBHOST} ${DBCLIENT}
else
echo 'local'
fi)
# Check if the first part of the result is 'remote'
if [[ $RESULT == remote* ]]
then
# Extract DBHOST and DBCLIENT
DBHOST=$(echo $RESULT | cut -d ' ' -f 2)
DBCLIENT=$(echo $RESULT | cut -d ' ' -f 3)
wpcli config set DB_HOST ${DBHOST}
else
wpcli config set DB_HOST localhost
fi
if [ -f bigscoots.sql ]
then
if wpcli core is-installed > /dev/null 2>&1
then
echo
echo "###########################"
read -p "The database already contains a live site, please confirm you want to overwrite the database. Press O to overwrite or any other key to skip import." -n 1 -r
echo
echo "###########################"
echo
if [[ $REPLY =~ ^[Oo]$ ]]; then
echo
echo
temp_cnt=10
while [[ ${temp_cnt} -gt 0 ]]; do
printf "\rYou have %2d second(s) remaining to hit Ctrl+C to cancel that operation!" ${temp_cnt}
sleep 1
((temp_cnt--))
done
echo ""
wpcli db import bigscoots.sql --max_allowed_packet=1G
exit_on_error $? MySQL Import
mv bigscoots.sql ../bigscoots_original-"${date}".sql
else
echo "Skipping import of database."
fi
else
wpcli db import bigscoots.sql --max_allowed_packet=1G
exit_on_error $? MySQL Import
mv bigscoots.sql ../bigscoots_original-"${date}".sql
fi
fi
fi
if ! wpcli core is-installed
then
if [[ $1 == fresh ]]
then
wpcli core is-installed 2>>/tmp/check.error || mail -s "WPO theworks failed - When running on a FRESH INSTALL - WPCLI most likely failed to download WP - $HOSTNAME" [email protected] </tmp/check.error >/dev/null
exit 1
else
wpcli core is-installed 2>>/tmp/check.error || mail -s "WPO theworks failed - wp not installed or database info in wpconfig broken - $HOSTNAME" [email protected] </tmp/check.error >/dev/null
echo
echo
echo
echo
echo "Something went wrong."
echo "Could be.."
echo "Database credentials could be wrong.. testing:"
if wpcli db query "SELECT version();" > /dev/null 2>&1
then
echo "Was able to run a query so credentials are good."
else
wpcli db query "SELECT version();"
echo "Testing a DB Query failed. Is the database missing or using wrong filename, needs to be filenamed: bigscoots.sql"
fi
exit 1
fi
fi
if [[ ! $1 == fresh ]]
then
echo "#################################"
echo "Attempting to remove the following plugins if they exist:" >> ../log/theworksrun_"${date}".log
echo "" >> ../log/theworksrun_"${date}".log
wpcli plugin uninstall ionos-assistant ionos-help-center ionos-sso ionos-navigation ionos-performance breeze wordpress-starter sg-cachepress sg-security mojo-marketplace-wp-plugin bluehost-wordpress-plugin dreamhost-panel-login --deactivate 2>> ../log/theworksrun_"${date}".log
echo "#################################" >> ../log/theworksrun_"${date}".log
echo "Remvoing the following muplugins and unnecessary files" >> ../log/theworksrun_"${date}".log
# Remove crap from wp-config.php
if grep -q hosting_provider_filters.php wp-config.php
then
sed -i '/if (file_exists(ABSPATH . '\''hosting_provider_filters.php'\'')) {/,/}/d' wp-config.php
if grep -q hosting_provider_filters.php wp-config.php
then
bash /bigscoots/general/slack.sh "#wpo-theworks" ":warning: $(hostname) - ${serverip} - Detected hosting_provider_filters.php in wp-config.php but failed to remove it."
fi
fi
if ! wpcli config get DB_CHARSET > /dev/null 2>&1
then
wpcli config set DB_CHARSET "utf8"
fi
if ! wpcli config get DB_COLLATE > /dev/null 2>&1
then
wpcli config set DB_COLLATE ''
fi
if [ -L wp-content/mu-plugins/redis-cache-pro ] && [ ! -e wp-content/mu-plugins/redis-cache-pro ]
then
rm -f wp-content/mu-plugins/redis-cache-pro wp-content/mu-plugins/redis-cache-pro.php
fi
if [ -d wp-content/plugins/easyindex ]
then
if ! wpcli plugin is-active easyindex
then
EASYINDEX=1
fi
fi
if [ -d wp-content/plugins/cleantalk-spam-protect ]
then
wpcli option patch update cleantalk_settings data__set_cookies 0 > /dev/null 2>&1
fi
if [ -d wp-content/plugins/wp-rocket ]
then
rm -f wp-content/advanced-cache.php
wpcli plugin deactivate wp-rocket
wpcli plugin activate wp-rocket
fi
if wpcli plugin is-installed migrate-guru
then
wpcli plugin delete migrate-guru
fi
# Unset any manually defined uploads path
wpcli option set upload_path "" > /dev/null 2>&1
# Create cronjob if wpcron is disabled
shopt -s nocasematch;
WPCRON=$(wpcli config get DISABLE_WP_CRON 2>/dev/null)
if [[ "$WPCRON" =~ ^(on|true|1)$ ]];
then
bash /bigscoots/wpo/manage/wpcron.sh on "$(pwd | sed 's/\// /g' | awk '{print $4}')"
fi
shopt -u nocasematch
if [[ $(wpcli option get siteurl) =~ //www. ]]
then
sed -i -E 's/return 301 https:\/\/(www)?/return 301 https:\/\/www./g' /usr/local/nginx/conf/conf.d/$(pwd | sed 's/\// /g' | awk '{print $4}').conf
fi
fi
if [[ $(wpcli option get siteurl) =~ http:// ]]
then
HTTPDOMAIN=$(wpcli option get siteurl)
HTTPSDOMAIN=$(wpcli option get siteurl | sed 's/http:/https:/g')
wpcli search-replace "${HTTPDOMAIN}" "${HTTPSDOMAIN}" --skip-columns=guid,user_email --all-tables
fi
if [[ $(wpcli option get siteurl) =~ https:// ]]
then
/bigscoots/wpo_forcehttps.sh $(pwd | sed 's/\// /g' | awk '{print $4}')
fi
bash /bigscoots/wpo/extras/phplogging.sh
if [ ! -f bigscoots.php ]
then
rsync -ah /bigscoots/includes/bigscoots.php .
fi
touch apple-touch-icon-120x120-precomposed.png
touch apple-touch-icon-120x120.png
touch apple-touch-icon-152x152-precomposed.png
touch apple-touch-icon-152x152.png
touch apple-touch-icon-76x76-precomposed.png
touch apple-touch-icon-76x76.png
touch apple-touch-icon-precomposed.png
touch apple-touch-icon.png
unset NEW_DB_NAME
unset NEW_DB_USER
unset NEW_DB_PASSWORD
chown -R nginx: . &
find . -type f \! -perm 644 -exec chmod 644 {} \; &
find . -type d \! -perm 755 -exec chmod 755 {} \; &
ngxreload_t /bigscoots/wpo_theworks.sh
if [ -f .htaccess ]
then
echo
echo
echo
echo "################# Possible Redirects #################"
echo
echo
echo
grep -ai 'RedirectMatch\|Redirect 301\|Redirect 410\|RewriteRule\|(\[0-9\]{4})' .htaccess |grep -iv 'RewriteRule \. /index.php \[L]\|RewriteRule ^index\\.php$ - \[L]\|HTTP_AUTHORIZATION'
echo
echo
echo
echo "################# Possible Redirects #################"
echo
echo
echo
fi
TRIGGERCOUNT=$(wpcli db query "SHOW TRIGGERS;" 2>/dev/null | wc -l)
if (( "$TRIGGERCOUNT" >= 1 ))
then
echo "WARNING - Detected a MySQL Trigger which in most cases is malicious, ask Justin if needed."
echo
echo
wpcli db query "SHOW TRIGGERS;" 2>/dev/null
echo
echo
echo "WARNING - Detected a MySQL Trigger which in most cases is malicious, ask Justin if needed."
fi
if [ "$EASYINDEX" == 1 ]
then
echo "Plugin easyindex is detected, please make sure to update the paths in the settings:
https://$(pwd | sed 's/\// /g' | awk '{print $4}')/wp-admin/admin.php\?page=EasyIndex"
fi