uninstall_debian.sh 2.2 KB

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