#!/usr/bin/env bash set -euo pipefail timestamp() { date '+%Y-%m-%d %H:%M:%S' } log() { printf '[%s] %s\n' "$(timestamp)" "$*" } log_err() { printf '[%s] %s\n' "$(timestamp)" "$*" >&2 } SERVICE_NAME="vmess-domain-rotator" REMOVE_AUTH_FILES="1" usage() { cat <<'EOF' Usage: sudo bash scripts/uninstall_debian.sh This script will: - Stop and disable the systemd timer and service - Remove systemd unit files - Remove environment/auth files created by installer (default) - Keep your git repository and all files intact Options: --service-name Service base name (default: vmess-domain-rotator) --keep-auth-files Keep /etc/.env and /var/lib/ -h, --help Show help Examples: sudo bash scripts/uninstall_debian.sh sudo bash scripts/uninstall_debian.sh --keep-auth-files EOF } while [[ $# -gt 0 ]]; do case "$1" in --service-name) SERVICE_NAME="$2" shift 2 ;; --keep-auth-files) REMOVE_AUTH_FILES="0" shift ;; -h|--help) usage exit 0 ;; *) log_err "Unknown option: $1" usage exit 1 ;; esac done if [[ "$(id -u)" -ne 0 ]]; then log_err "Please run as root (use sudo)." exit 1 fi ENV_FILE="/etc/${SERVICE_NAME}.env" STATE_DIR="/var/lib/${SERVICE_NAME}" # Stop and disable timer if systemctl list-unit-files | grep -q "^${SERVICE_NAME}.timer"; then log "Stopping and disabling ${SERVICE_NAME}.timer..." systemctl disable --now "${SERVICE_NAME}.timer" || true fi # Stop service if running if systemctl list-unit-files | grep -q "^${SERVICE_NAME}.service"; then log "Stopping ${SERVICE_NAME}.service..." systemctl stop "${SERVICE_NAME}.service" || true fi # Remove systemd unit files log "Removing systemd unit files..." rm -f "/etc/systemd/system/${SERVICE_NAME}.service" rm -f "/etc/systemd/system/${SERVICE_NAME}.timer" if [[ "$REMOVE_AUTH_FILES" == "1" ]]; then log "Removing auth/env files..." rm -f "$ENV_FILE" rm -rf "$STATE_DIR" fi systemctl daemon-reload systemctl reset-failed log "" log "✓ Uninstall complete!" log "" if [[ "$REMOVE_AUTH_FILES" == "1" ]]; then log "Note: Service env/auth files have been removed." else log "Note: Service env/auth files were kept." fi log " Your git repository and project files have been preserved." log ""