#!/usr/bin/env bash # Clear selected Besu blockchain databases (NUCLEAR OPTION) # This will require full re-sync from genesis. # Usage: # ./clear-blockchain-database.sh --vmid 2101 # ./clear-blockchain-database.sh --apply --vmid 2101 set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" source "${PROJECT_ROOT}/scripts/lib/load-project-env.sh" # Colors RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[✓]${NC} $1"; } log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; } # Check if pct is available if ! command -v pct &>/dev/null; then log_error "This script must be run on the Proxmox host (pct command not found)" exit 1 fi APPLY=false TARGET_VMIDS=() usage() { cat <<'EOF' Usage: ./clear-blockchain-database.sh --vmid [--vmid ...] [--apply] Options: --vmid Required. Limit destructive action to one or more VMIDs. --apply Perform deletion. Without this flag, the script prints the target VMIDs and exits. EOF } while [[ $# -gt 0 ]]; do case "$1" in --vmid) [[ $# -ge 2 ]] || { usage >&2; exit 2; } TARGET_VMIDS+=("$2") shift 2 ;; --apply) APPLY=true shift ;; -h|--help) usage exit 0 ;; *) echo "Unknown argument: $1" >&2 usage >&2 exit 2 ;; esac done [[ ${#TARGET_VMIDS[@]} -gt 0 ]] || { log_error "At least one --vmid is required for this destructive script."; usage >&2; exit 2; } echo "=========================================" echo "Clear Entire Blockchain Database" echo "=========================================" echo "" log_error "⚠️ ⚠️ ⚠️ CRITICAL WARNING ⚠️ ⚠️ ⚠️" echo "" log_error "This will:" log_error " 1. DELETE the entire blockchain database" log_error " 2. DELETE all transaction pools" log_error " 3. DELETE all caches" log_error " 4. Require FULL RE-SYNC from genesis" log_error " 5. Take SIGNIFICANT TIME to re-sync" echo "" log_warn "This is a NUCLEAR OPTION - use only if absolutely necessary" echo "" declare -A SERVICE_BY_VMID for vmid in 1000 1001 1002 1003 1004; do SERVICE_BY_VMID[$vmid]="besu-validator.service"; done for vmid in 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510; do SERVICE_BY_VMID[$vmid]="besu-sentry.service"; done for vmid in 2101 2102 2103 2201 2301 2303 2304 2305 2306 2307 2308 2400 2401 2402 2403 2420 2430 2440 2460 2470 2480; do SERVICE_BY_VMID[$vmid]="besu-rpc.service"; done if ! $APPLY; then log_warn "Dry-run only. Target VMIDs:" for vmid in "${TARGET_VMIDS[@]}"; do log_info " VMID $vmid (host $(get_host_for_vmid "$vmid")) service ${SERVICE_BY_VMID[$vmid]:-unknown}" done log_info "Re-run with --apply to proceed." exit 0 fi read -p "Type 'DELETE DATABASE' to confirm: " CONFIRM if [ "$CONFIRM" != "DELETE DATABASE" ]; then log_info "Aborted" exit 0 fi log_info "Stopping selected Besu nodes..." for vmid in "${TARGET_VMIDS[@]}"; do service="${SERVICE_BY_VMID[$vmid]:-besu.service}" if pct status "$vmid" 2>/dev/null | grep -q "running"; then log_info "Stopping VMID $vmid ($service)..." pct exec "$vmid" -- systemctl stop "$service" 2>/dev/null || pct exec "$vmid" -- systemctl stop besu.service 2>/dev/null || true fi done sleep 5 log_info "Clearing selected blockchain databases..." for vmid in "${TARGET_VMIDS[@]}"; do if pct status "$vmid" 2>/dev/null | grep -q "running"; then log_info "Clearing VMID $vmid..." # Clear database pct exec "$vmid" -- rm -rf /data/besu/database/* 2>/dev/null || true # Clear caches pct exec "$vmid" -- rm -rf /data/besu/caches/* 2>/dev/null || true # Clear any transaction pool files pct exec "$vmid" -- find /data/besu -type f -name "*pool*" -delete 2>/dev/null || true pct exec "$vmid" -- find /data/besu -type d -name "*pool*" -exec rm -rf {} \; 2>/dev/null || true log_success "✓ VMID $vmid cleared" fi done log_info "Starting selected Besu nodes..." for vmid in "${TARGET_VMIDS[@]}"; do service="${SERVICE_BY_VMID[$vmid]:-besu.service}" if pct status "$vmid" 2>/dev/null | grep -q "running"; then log_info "Starting VMID $vmid ($service)..." pct exec "$vmid" -- systemctl start "$service" 2>/dev/null || pct exec "$vmid" -- systemctl start besu.service 2>/dev/null || true fi done log_info "Waiting 30 seconds for services to start..." sleep 30 log_success "=========================================" log_success "Blockchain Database Cleared!" log_success "=========================================" log_info "" log_warn "⚠️ Nodes will now re-sync from genesis" log_warn "⚠️ This may take significant time" log_info "" log_info "Next steps:" log_info " 1. Wait for nodes to re-sync (monitor block numbers)" log_info " 2. Once synced, run: ./scripts/configure-ethereum-mainnet-final.sh" log_info ""