Skip to content

Instantly share code, notes, and snippets.

@alexdyukov
Last active May 4, 2021 08:53
Show Gist options
  • Save alexdyukov/fc4a792e4343acdf6baf46c2715c498f to your computer and use it in GitHub Desktop.
Save alexdyukov/fc4a792e4343acdf6baf46c2715c498f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# FILE: cleanup_cms_caches.sh
# Brief: Detect CMS and clear its cache
# Author: @AlexDyukov
# Requirement: wp-cli, drush
if (( $# == 0 )); then
echo -e "site path not present\nusage: $0 /home/[a-z]/*/*/public_html/"
exit 1
fi
#-----------------------------------------------------------------------------
# Cleanup paths in args by deleting files/dirs,
# skip key files (blank in common)
#-----------------------------------------------------------------------------
cleanup_paths () {
for path in $@; do
find "$path" -not \( -name "index.php" \
-o -name "index.html" \
-o -name ".htaccess" \) \
-delete \
-print
done
}
#-----------------------------------------------------------------------------
# Detect CMS by mark file and clear its cache
#-----------------------------------------------------------------------------
clear_cms_caches () {
test -f cli/garbagecron.php \
&& (timeout 3m php cli/garbagecron.php; \
echo "Joomla cache cleared: $(pwd)" && return 0)
test -f wp-config.php \
&& (timeout 3m wp cache flush; \
echo "Wordpress cache cleared: $(pwd)" && return 0)
test -f vendor/autoload.php \
&& (timeout 3m drush cc all; \
echo "Drupal8 cache cleared: $(pwd)" && return 0)
test -f includes/database/prefetch.inc \
&& (timeout 3m drush cc all; \
echo "Drupal7 cache cleared: $(pwd)" && return 0)
test -f includes/database.inc \
&& (timeout 3m drush cc all; \
echo "Drupal6 cache cleared: $(pwd)" && return 0)
test -f engine/data/dbconfig.php \
&& (cleanup_paths engine/cache/*.tmp; \
echo "DLE cache cleared: $(pwd)" && return 0)
test -f ucp/ucp_main.php \
&& (cleanup_paths cache/tpl_* cache/ctpl_* cache/sql_*; \
echo "PHPBB3 cache cleared: $(pwd)" && return 0)
test -f bitrix/php_interface/dbconn.php \
&& (cleanup_paths bitrix/cache/* bitrix/managed_cache/*; \
echo "Bitrix cache cleared: $(pwd)" && return 0)
test -f tools/smarty/Smarty.class.php \
&& (cleanup_paths cache/smarty/cache/* cache/smarty/compile/*; \
echo "PrestaShop cache cleared: $(pwd)" && return 0)
test -f system/library/cart.php \
&& (cleanup_paths cache/cachefs/* vqmod/vqcache/*; \
echo "OpenCart_v1 cache cleared: $(pwd)" && return 0)
test -f system/storage/cache/index.html \
&& (cleanup_paths system/storage/cache/* vqmod/vqcache/*; \
echo "OpenCart_v2 cache cleared: $(pwd)" && return 0)
test -f system/system/library/cart/cart.php \
&& (cleanup_paths system/storage/cache/* vqmod/vqcache/*; \
echo "OpenCart_v2_3 cache cleared: $(pwd)" && return 0)
test -f manager/includes/config.inc.php \
&& (cleanup_paths assets/cache/*; \
echo "MODX1 cache cleared: $(pwd)" && return 0)
test -f core/config/config.inc.php \
&& (cleanup_paths core/cache/*; \
echo "MODX2 cache cleared: $(pwd)" && return 0)
test -f includes/dbimport.inc.php \
&& (cleanup_paths cache/*; \
echo "InstantCms1 cache cleared: $(pwd)" && return 0)
test -f app/etc/local.xml \
&& (cleanup_paths var/cache/*; \
echo "Magento cache cleared: $(pwd)" && return 0)
test -f wa-config/db.php \
&& (cleanup_paths wa-cache/*; \
echo "WebaSyst cache cleared: $(pwd)" && return 0)
test -f includes/modules/graduated_prices.php \
&& (cleanup_paths cache/*; \
echo "VaMShop cache cleared: $(pwd)" && return 0)
test -d tpls/content \
&& (cleanup_paths sys-temp/runtime-cache/fs-cache/* \
sys-temp/static-cache/*; \
echo "UMI cache cleared: $(pwd)" && return 0)
test -f admin/makebookmark.php \
&& (cleanup_paths tmp/cache/* \
tmp/templates_c/*; \
echo "Made Simple cache cleared: $(pwd)" && return 0)
test -d application/maxsite \
&& (cleanup_paths application/cache/*; \
echo "MaxSite cache cleared: $(pwd)" && return 0)
test -f core/config/connect.inc.php \
&& (cleanup_paths core/cache/*; \
echo "ShopCms cache cleared: $(pwd)" && return 0)
test -f config.local.php \
&& (cleanup_paths var/cache/; \
echo "CS-Cart cache cleared: $(pwd)" && return 0)
test -f application/helpers/admin_helper.php \
&& (cleanup_paths $(find system/cache/* -not -path system/cache/template_c* -print); \
echo "ImageCMS cache cleared: $(pwd)" && return 0)
test -d system/i18n \
&& (cleanup_paths application/cache/* application/logs/*; \
echo "Kohana Framework cache cleared: $(pwd)" && return 0)
test -d typo3conf \
&& (cleanup_paths typo3conf/temp_CACHED_*; \
echo "TYPO3 cache cleared: $(pwd)" && return 0)
test -f wa-config/db.php \
&& (cleanup_paths wa-cache/*; \
echo "WebaSyst3 cache cleared: $(pwd)" && return 0)
test -f modules/adminsite/admin/adminsite.admin.php \
&& (cleanup_paths cache/*; \
echo "Diafan cache cleared: $(pwd)" && return 0)
test -f fuel/app/config/development/db.php \
&& (cleanup_paths fuel/app/cache/*; \
echo "Fuel Framework cache cleared: $(pwd)" && return 0)
test -f vendor/october/rain/src/Support/helpers.php \
&& (timeout 3m php artisan cache:clear; \
echo "October CMS cache cleared: $(pwd)" && return 0)
test -f system/koharness.php \
&& (cleanup_paths application/cache/*; \
echo "ImageCMSv2 cache cleared: $(pwd)" && return 0)
}
export -f clear_cms_caches
export -f cleanup_paths
for sitepath in $@; do
if [[ ! -d $sitepath ]]; then
echo "site path $sitepath does not exist" && continue
fi
cd "$sitepath"
if (( $EUID != 0 )); then
clear_cms_caches && continue
fi
#-------------------------------------------------------------------------
# call clear_cms_caches function as user to prevent security fail
#-------------------------------------------------------------------------
FILE=$(find ./ -iname "*.php" -not -user root -print | head -1)
if [ ! -n "$FILE" ]; then
echo "skipping $sitepath : no php files means no CMS inside"
continue
fi
OWNER=$(stat -c '%U' "$FILE")
su "$OWNER" -s "$SHELL" -c "clear_cms_caches"
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment