| Server IP : 35.80.110.71 / Your IP : 216.73.216.221 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ip-172-31-21-44 6.17.0-1019-aws #19~24.04.1-Ubuntu SMP Tue Jun 23 18:53:06 UTC 2026 x86_64 User : ubuntu ( 1000) PHP Version : 8.3.31 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/wp/shared/scripts/ |
Upload File : |
#!/usr/bin/env bash
#
# Reset a demo's user-generated drift back to a known-good state.
#
# Default (soft reset): delete WC orders + non-admin users + spam comments + transients.
# Preserves the catalog, settings, pages, and the admin user. Designed to run weekly
# on demos that accept public input.
#
# --hard: drop the DB entirely and re-install WP core from secrets. Catalog is gone
# unless the demo's seed scripts are re-run afterward. Intended for "I broke
# something, restore to clean WP" scenarios.
#
# Usage:
# sudo reset-demo.sh --slug=<slug> [--hard]
# sudo reset-demo.sh --all # soft-reset every demo (not the hub)
#
set -euo pipefail
SLUG=""
HARD=0
ALL=0
for arg in "$@"; do
case "$arg" in
--slug=*) SLUG="${arg#*=}" ;;
--hard) HARD=1 ;;
--all) ALL=1 ;;
*) echo "Unknown arg: $arg"; exit 1 ;;
esac
done
log() { echo "[$(date +%H:%M:%S)] $*"; }
step() { echo; echo "=== $* ==="; }
ok() { echo " ✓ $*"; }
reset_one() {
local slug="$1"
local hard="$2"
local wp_path="/var/www/wp/projects/${slug}/public"
local secrets="/var/www/wp/shared/secrets/${slug}.env"
if [ ! -f "$wp_path/wp-config.php" ]; then
echo " ✗ $slug — no wp-config at $wp_path"
return 1
fi
log "Reset (hard=$hard): $slug"
if [ "$hard" -eq 1 ]; then
[ ! -f "$secrets" ] && { echo " ✗ no secrets file at $secrets — cannot hard reset"; return 1; }
# shellcheck disable=SC1090
source "$secrets"
# Snapshot before nuking, in case
sudo -u www-data wp db export - --path="$wp_path" 2>/dev/null \
| gzip > "/var/www/wp/shared/backups/${slug}/pre-reset-$(date +%Y%m%d-%H%M%S).sql.gz"
ok "snapshot saved"
mysql -e "DROP DATABASE IF EXISTS ${DB_NAME}; CREATE DATABASE ${DB_NAME} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
ok "DB dropped + recreated"
sudo -u www-data wp core install --path="$wp_path" \
--url="https://${DOMAIN}" \
--title="${WP_TITLE}" \
--admin_user="${WP_ADMIN_USER}" \
--admin_email="${WP_ADMIN_EMAIL}" \
--admin_password="${WP_ADMIN_PASSWORD}" \
--skip-email
ok "WP core reinstalled"
sudo -u www-data wp theme activate "${THEME}" --path="$wp_path" 2>&1 | tail -1
sudo -u www-data wp plugin activate sfp-blocks --path="$wp_path" 2>&1 | tail -1 || true
sudo -u www-data wp rewrite structure '/%postname%/' --hard --path="$wp_path" 2>&1 | tail -1 || true
else
# Soft reset: clear user-generated state, keep catalog + settings
local wp="sudo -u www-data wp --path=$wp_path"
# WooCommerce: delete orders, drafts, sessions
if $wp plugin is-active woocommerce 2>/dev/null; then
ORDERS=$($wp post list --post_type=shop_order --post_status=any --field=ID 2>/dev/null | tr '\n' ' ')
if [ -n "$ORDERS" ]; then
$wp post delete $ORDERS --force >/dev/null 2>&1 || true
ok "deleted $(echo $ORDERS | wc -w) orders"
fi
$wp wc tool run clear_sessions --user=admin >/dev/null 2>&1 || true
ok "WC sessions cleared"
fi
# Delete non-admin users (any user not user_login=admin)
NON_ADMIN=$($wp user list --field=ID --exclude="$($wp user get admin --field=ID 2>/dev/null)" 2>/dev/null | tr '\n' ' ')
if [ -n "$NON_ADMIN" ]; then
$wp user delete $NON_ADMIN --yes --reassign="$($wp user get admin --field=ID 2>/dev/null)" >/dev/null 2>&1 || true
ok "deleted $(echo $NON_ADMIN | wc -w) non-admin users"
fi
# Spam comments
SPAM=$($wp comment list --status=spam --field=ID 2>/dev/null | tr '\n' ' ')
if [ -n "$SPAM" ]; then
$wp comment delete $SPAM --force >/dev/null 2>&1 || true
ok "deleted $(echo $SPAM | wc -w) spam comments"
fi
# Transients + cache
$wp transient delete --all >/dev/null 2>&1 || true
$wp cache flush >/dev/null 2>&1 || true
ok "transients + cache cleared"
fi
log "Done: $slug"
}
mkdir -p /var/www/wp/shared/logs
if [ "$ALL" -eq 1 ]; then
step "Soft reset all demos"
for demo_root in /var/www/wp/projects/*/; do
slug=$(basename "$demo_root")
reset_one "$slug" 0
done
else
[ -z "$SLUG" ] && { echo "Missing --slug or --all"; exit 1; }
reset_one "$SLUG" "$HARD"
fi
echo "$(date -u +%FT%TZ) reset slug=${SLUG:-all} hard=$HARD" >> /var/www/wp/shared/logs/resets.log