diff --git a/modules/website/wordpress/wordpress-cron-manager.sh b/modules/website/wordpress/wordpress-cron-manager.sh index 9d27477..4ef1c4d 100755 --- a/modules/website/wordpress/wordpress-cron-manager.sh +++ b/modules/website/wordpress/wordpress-cron-manager.sh @@ -1018,6 +1018,118 @@ report_save() { fi } +# ADVANCED FEATURE: Automatic Rollback Support (OPT-19) +# Provides safety net for large-scale operations with ability to revert changes +# Creates checkpoint backups and enables reverting to known-good state +declare -g ROLLBACK_ENABLED=false +declare -g ROLLBACK_DIR="/tmp/wp-cron-rollback-$$" +declare -gA ROLLBACK_BACKUPS # Maps wp-config.php path to backup path + +# Initialize rollback system +rollback_init() { + if [ "$DRY_RUN" = "true" ]; then + return 0 # Skip rollback in dry-run mode + fi + + ROLLBACK_ENABLED=true + mkdir -p "$ROLLBACK_DIR" 2>/dev/null + + if [ ! -d "$ROLLBACK_DIR" ]; then + print_warning "Could not create rollback directory: $ROLLBACK_DIR" + ROLLBACK_ENABLED=false + return 1 + fi + + return 0 +} + +# Create checkpoint backup before modification +rollback_create_checkpoint() { + local wp_config="$1" + local backup_path="$ROLLBACK_DIR/$(basename "$wp_config").bak" + + if [ ! "$ROLLBACK_ENABLED" = "true" ]; then + return 0 + fi + + if ! cp "$wp_config" "$backup_path" 2>/dev/null; then + print_error "Failed to create rollback checkpoint: $wp_config" + return 1 + fi + + # Store mapping for later rollback + ROLLBACK_BACKUPS["$wp_config"]="$backup_path" + return 0 +} + +# Restore file from rollback checkpoint +rollback_restore_file() { + local wp_config="$1" + local backup_path="${ROLLBACK_BACKUPS[$wp_config]}" + + if [ ! -f "$backup_path" ]; then + print_error "No rollback checkpoint found for: $wp_config" + return 1 + fi + + if ! cp "$backup_path" "$wp_config" 2>/dev/null; then + print_error "Failed to restore from rollback checkpoint: $wp_config" + return 1 + fi + + print_success "Restored from checkpoint: $wp_config" + return 0 +} + +# Rollback all changes to checkpoint +rollback_all() { + if [ ! "$ROLLBACK_ENABLED" = "true" ]; then + print_error "Rollback not enabled" + return 1 + fi + + if [ ${#ROLLBACK_BACKUPS[@]} -eq 0 ]; then + print_info "No checkpoints to rollback" + return 0 + fi + + print_warning "Rolling back ${#ROLLBACK_BACKUPS[@]} modified files..." + + local failed=0 + for wp_config in "${!ROLLBACK_BACKUPS[@]}"; do + if ! rollback_restore_file "$wp_config"; then + failed=$((failed + 1)) + fi + done + + if [ $failed -eq 0 ]; then + print_success "All files restored successfully" + return 0 + else + print_error "Failed to restore $failed files" + return 1 + fi +} + +# Clean up rollback directory +rollback_cleanup() { + if [ -d "$ROLLBACK_DIR" ]; then + rm -rf "$ROLLBACK_DIR" 2>/dev/null + fi +} + +# Rollback trap handler (called on EXIT/INT/TERM) +rollback_on_interrupt() { + if [ "$ROLLBACK_ENABLED" = "true" ]; then + echo "" + print_warning "Operation interrupted" + if confirm "Rollback all changes?"; then + rollback_all + fi + fi + rollback_cleanup +} + # OPTIMIZATION: Build cron command consistently # Centralizes cron command format (appears 4 times throughout script) # Returns: cron command string for wp-cron.php execution