uninstall_debian.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SERVICE_NAME="vmess-domain-rotator"
  4. REMOVE_AUTH_FILES="1"
  5. usage() {
  6. cat <<'EOF'
  7. Usage: sudo bash scripts/uninstall_debian.sh
  8. This script will:
  9. - Stop and disable the systemd timer and service
  10. - Remove systemd unit files
  11. - Remove environment/auth files created by installer (default)
  12. - Keep your git repository and all files intact
  13. Options:
  14. --service-name <name> Service base name (default: vmess-domain-rotator)
  15. --keep-auth-files Keep /etc/<service>.env and /var/lib/<service>
  16. -h, --help Show help
  17. Examples:
  18. sudo bash scripts/uninstall_debian.sh
  19. sudo bash scripts/uninstall_debian.sh --keep-auth-files
  20. EOF
  21. }
  22. while [[ $# -gt 0 ]]; do
  23. case "$1" in
  24. --service-name)
  25. SERVICE_NAME="$2"
  26. shift 2
  27. ;;
  28. --keep-auth-files)
  29. REMOVE_AUTH_FILES="0"
  30. shift
  31. ;;
  32. -h|--help)
  33. usage
  34. exit 0
  35. ;;
  36. *)
  37. echo "Unknown option: $1" >&2
  38. usage
  39. exit 1
  40. ;;
  41. esac
  42. done
  43. if [[ "$(id -u)" -ne 0 ]]; then
  44. echo "Please run as root (use sudo)." >&2
  45. exit 1
  46. fi
  47. ENV_FILE="/etc/${SERVICE_NAME}.env"
  48. STATE_DIR="/var/lib/${SERVICE_NAME}"
  49. # Stop and disable timer
  50. if systemctl list-unit-files | grep -q "^${SERVICE_NAME}.timer"; then
  51. echo "Stopping and disabling ${SERVICE_NAME}.timer..."
  52. systemctl disable --now "${SERVICE_NAME}.timer" || true
  53. fi
  54. # Stop service if running
  55. if systemctl list-unit-files | grep -q "^${SERVICE_NAME}.service"; then
  56. echo "Stopping ${SERVICE_NAME}.service..."
  57. systemctl stop "${SERVICE_NAME}.service" || true
  58. fi
  59. # Remove systemd unit files
  60. echo "Removing systemd unit files..."
  61. rm -f "/etc/systemd/system/${SERVICE_NAME}.service"
  62. rm -f "/etc/systemd/system/${SERVICE_NAME}.timer"
  63. if [[ "$REMOVE_AUTH_FILES" == "1" ]]; then
  64. echo "Removing auth/env files..."
  65. rm -f "$ENV_FILE"
  66. rm -rf "$STATE_DIR"
  67. fi
  68. systemctl daemon-reload
  69. systemctl reset-failed
  70. echo ""
  71. echo "✓ Uninstall complete!"
  72. echo ""
  73. if [[ "$REMOVE_AUTH_FILES" == "1" ]]; then
  74. echo "Note: Service env/auth files have been removed."
  75. else
  76. echo "Note: Service env/auth files were kept."
  77. fi
  78. echo " Your git repository and project files have been preserved."
  79. echo ""