Skip to content

Instantly share code, notes, and snippets.

@jadonn
Created March 9, 2018 20:42
Show Gist options
  • Save jadonn/7a4c98758b544b56255aa32a6597015f to your computer and use it in GitHub Desktop.
Save jadonn/7a4c98758b544b56255aa32a6597015f to your computer and use it in GitHub Desktop.
Start of a shell script to check for WordPress updates for a site and to make a backup of the site if there are updates.
#1/bin/bash
create_subdomain()
{
cpapi2 SubDomain addsubdomain domain=$BUILD_NUMBER rootdomain=$HOME_DOMAIN dir=$HOME/$BUILD_NUMBER.$HOME_DOMAIN disallowdot=1
}
copy_files()
{
mkdir $HOME/$BUILD_NUMBER.$HOME_DOMAIN
cp -rfp ./. $HOME/$BUILD_NUMBER.$HOME_DOMAIN/
}
copy_database()
{
db_name=$(cat /dev/urandom | tr -dc 'A-Za-z0-9' | head -c6)
db_password=$(cat /dev/urandom | tr -dc 'A-Za-z0-9' | head -c16)
uapi Mysql create_database name=${USER}_${db_name}
uapi Mysql create_user name=${USER}_${db_name} password=$db_password
uapi Mysql set_privileges_on_database user=${USER}_${db_name} database=${USER}_${db_name} privileges=ALL%20PRIVILEGES
$WPCLI_PATH/wp db export - | mysql -D ${USER}_${db_name} -u ${USER}_${db_name} --password=$db_password
}
update_test_wpconfig()
{
cd $HOME/$BUILD_NUMBER.$HOME_DOMAIN/
$WPCLI_PATH/wp config set DB_NAME ${USER}_${db_name}
$WPCLI_PATH/wp config set DB_USER ${USER}_${db_name}
$WPCLI_PATH/wp config set DB_PASSWORD $db_password
echo $($WPCLI_PATH/wp config get DB_NAME)
echo $($WPCLI_PATH/wp config get DB_USER)
echo $($WPCLI_PATH/wp config get DB_PASSWORD)
}
update_db_urls()
{
$WPCLI_PATH/wp option update home http://$BUILD_NUMBER.$HOME_DOMAIN
$WPCLI_PATH/wp option update siteurl http://$BUILD_NUMBER.$HOME_DOMAIN
$WPCLI_PATH/wp search-replace http://$HOME_DOMAIN http://$BUILD_NUMBER.$HOME_DOMAIN
$WPCLI_PATH/wp search-replace https://$HOME_DOMAIN http://$BUILD_NUMBER.$HOME_DOMAIN
}
core_update_available=0
plugin_update_available=0
theme_update_available=0
echo -e "\nChanging into the document root"
cd $DOCUMENT_ROOT
echo -e "\nChecking for available updates"
echo -e "\nChecking for core updates"
core_update_check_result=$($WPCLI_PATH/wp core check-update --format=count)
if [[ $core_update_check_result == "1" ]]
then
echo -e "\tCore update available"
core_update_available=1
else
echo -e "\tNo core update available"
fi
echo -e "\nChecking for plugin updates"
plugin_update_check_result=$($WPCLI_PATH/wp plugin list --update=available --format=count)
if [[ $plugin_update_check_result != "0" ]]
then
echo -e "\tPlugin update(s) available"
plugin_update_available=1
else
echo -e "\tNo plugin updates available"
fi
echo -e "\nChecking for theme updates"
theme_update_available_result=$($WPCLI_PATH/wp theme list --update=available --format=count)
if [[ $theme_update_available_result != "0" ]]
then
echo -e "\tTheme update(s) available"
theme_update_available=1
else
echo -e "\tNo theme updates available"
fi
if [[ $core_update_available == "1" ]] || [[ $plugin_update_available == "1" ]] || [[ $theme_update_available == "1" ]]
then
echo -e "\nUpdates were found. Proceeding to create copy of WordPress website."
create_subdomain "$BUILD_NUMBER.$HOME_DOMAIN"
copy_files
copy_database
update_test_wpconfig
update_db_urls
else
echo -e "\nNo updates were found. No further action will be taken."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment