-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset-vps-setup.sh
More file actions
294 lines (258 loc) · 10.8 KB
/
Copy pathreset-vps-setup.sh
File metadata and controls
294 lines (258 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/bin/bash
# ============================================================
# Reset VPS Setup — Undo everything installed by secure-vps-setup.sh
# Run as root: sudo bash reset-vps-setup.sh
# ============================================================
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
print_status() { echo -e "${GREEN}[✓]${NC} $1"; }
print_warning() { echo -e "${YELLOW}[!]${NC} $1"; }
print_error() { echo -e "${RED}[✗]${NC} $1"; }
# --- Check root ---
if [ "$EUID" -ne 0 ]; then
print_error "Please run as root: sudo bash reset-vps-setup.sh"
exit 1
fi
DEV_USER="dev"
DEV_HOME="/home/$DEV_USER"
MANIFEST="/var/lib/vps-setup/installed-packages.manifest"
# ============================================================
# 1. Show what will be removed and prompt for confirmation
# ============================================================
echo ""
echo "========================================="
echo " Reset VPS Setup"
echo " Undo secure-vps-setup.sh"
echo "========================================="
echo ""
echo "This will remove:"
echo " - Claude Code binary and config"
echo " - Bashrc marker blocks added by setup"
echo " - Go toolchain (/usr/local/go, ~/go)"
echo " - Gradle (/opt/gradle-*)"
echo " - Miniconda (/opt/miniconda3)"
echo " - nvm (~/.nvm)"
echo " - pyenv (~/.pyenv)"
echo " - setup-github helper"
echo " - tmux config"
echo " - SSH keys and config (dev + root)"
echo " - GPG agent config"
echo " - Cron jobs (ClamAV daily, rkhunter weekly)"
echo " - fail2ban jail config"
echo " - ufw firewall (disabled)"
echo " - fd/bat symlinks"
echo " - Apt repos (Adoptium, GitHub CLI)"
echo " - Services (ClamAV, fail2ban)"
if [ -f "$MANIFEST" ]; then
PKG_COUNT=$(wc -l < "$MANIFEST" 2>/dev/null || echo 0)
echo " - Apt packages from manifest ($PKG_COUNT packages)"
else
echo " - Apt packages: (no manifest found, will skip)"
fi
echo " - Setup manifest (/var/lib/vps-setup)"
echo ""
read -rp "Proceed with reset? (y/N): " CONFIRM
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
print_warning "Aborted."
exit 0
fi
# ============================================================
# 2. Ask about dev user deletion (separate prompt)
# ============================================================
DELETE_DEV_USER="n"
if id "$DEV_USER" &>/dev/null; then
echo ""
read -rp "Also delete the '$DEV_USER' user and home directory? (y/N): " DELETE_DEV_USER
fi
echo ""
print_status "Starting reset..."
echo ""
# ============================================================
# 3. Claude Code
# ============================================================
print_status "Removing Claude Code..."
rm -f /usr/local/bin/claude
if [ -d "$DEV_HOME" ]; then
rm -rf "$DEV_HOME/.claude"
fi
# ============================================================
# 4. Bashrc blocks
# ============================================================
# Remove setup blocks from both dev and root shell config.
if [ -f "$DEV_HOME/.bashrc" ]; then
print_status "Removing setup blocks from .bashrc..."
# Old format: everything from single marker to EOF
sed -i '/^# --- Claude Code VPS additions ---$/,$d' "$DEV_HOME/.bashrc" 2>/dev/null || true
# New format: START/END marker blocks
sed -i '/# --- .* START ---/,/# --- .* END ---/d' "$DEV_HOME/.bashrc" 2>/dev/null || true
else
print_warning "No .bashrc found — skipping"
fi
if [ -f /root/.bashrc ]; then
print_status "Removing setup blocks from root .bashrc..."
sed -i '/^# --- Claude Code VPS additions ---$/,$d' /root/.bashrc 2>/dev/null || true
sed -i '/# --- .* START ---/,/# --- .* END ---/d' /root/.bashrc 2>/dev/null || true
sed -i '/# >>> conda initialize >>>/,/# <<< conda initialize <<</d' /root/.bashrc 2>/dev/null || true
else
print_warning "No root .bashrc found — skipping"
fi
# ============================================================
# 5. Go
# ============================================================
print_status "Removing Go..."
rm -rf /usr/local/go 2>/dev/null || true
if [ -d "$DEV_HOME" ]; then
rm -rf "$DEV_HOME/go" 2>/dev/null || true
fi
# ============================================================
# 6. Gradle
# ============================================================
print_status "Removing Gradle..."
if [ -f /var/lib/vps-setup/manifest-meta.txt ]; then
# shellcheck disable=SC1091
. /var/lib/vps-setup/manifest-meta.txt
fi
if [ -n "${GRADLE_VERSION:-}" ]; then
rm -rf "/opt/gradle-${GRADLE_VERSION}" 2>/dev/null || true
fi
# Backward-compatible cleanup for installations from older scripts
if [ ! -d "/opt/gradle-${GRADLE_VERSION:-}" ]; then
rm -rf /opt/gradle-* 2>/dev/null || true
fi
rm -f /usr/local/bin/gradle 2>/dev/null || true
# ============================================================
# 7. nvm
# ============================================================
print_status "Removing nvm..."
if [ -d "$DEV_HOME" ]; then
rm -rf "$DEV_HOME/.nvm" 2>/dev/null || true
fi
# ============================================================
# 8. pyenv
# ============================================================
print_status "Removing pyenv..."
if [ -d "$DEV_HOME" ]; then
rm -rf "$DEV_HOME/.pyenv" 2>/dev/null || true
fi
# ============================================================
# 8b. Miniconda
# ============================================================
print_status "Removing Miniconda..."
rm -rf /opt/miniconda3 2>/dev/null || true
rm -f /usr/local/bin/conda 2>/dev/null || true
# Remove conda init block from .bashrc
if [ -f "$DEV_HOME/.bashrc" ]; then
sed -i '/# >>> conda initialize >>>/,/# <<< conda initialize <<</d' "$DEV_HOME/.bashrc" 2>/dev/null || true
fi
# ============================================================
# 9. setup-github + legacy cc session manager
# ============================================================
print_status "Removing setup-github (and legacy cc if present)..."
rm -f "$DEV_HOME/.local/bin/setup-github" 2>/dev/null || true
# Legacy cleanup: previous versions shipped a 'cc' session manager
rm -f "$DEV_HOME/.local/bin/cc" 2>/dev/null || true
rm -f "$DEV_HOME/.local/share/bash-completion/completions/cc" 2>/dev/null || true
rm -rf "$DEV_HOME/.claude/cc-sessions" 2>/dev/null || true
# ============================================================
# 10. tmux config
# ============================================================
print_status "Removing tmux config..."
rm -f "$DEV_HOME/.tmux.conf" 2>/dev/null || true
# ============================================================
# 11. SSH config
# ============================================================
print_status "Removing SSH keys and config..."
rm -f "$DEV_HOME/.ssh/id_ed25519" 2>/dev/null || true
rm -f "$DEV_HOME/.ssh/id_ed25519.pub" 2>/dev/null || true
rm -f "$DEV_HOME/.ssh/agent-env" 2>/dev/null || true
rm -f "$DEV_HOME/.ssh/allowed_signers" 2>/dev/null || true
rm -f "$DEV_HOME/.ssh/config" 2>/dev/null || true
rm -f /root/.ssh/config 2>/dev/null || true
# ============================================================
# 12. GPG config
# ============================================================
print_status "Removing GPG agent config..."
rm -f "$DEV_HOME/.gnupg/gpg-agent.conf" 2>/dev/null || true
# ============================================================
# 13. Cron jobs
# ============================================================
print_status "Removing cron jobs..."
rm -f /etc/cron.daily/clamav-scan 2>/dev/null || true
rm -f /etc/cron.weekly/rkhunter-scan 2>/dev/null || true
rm -f /etc/logrotate.d/vps-security 2>/dev/null || true
# ============================================================
# 14. fail2ban
# ============================================================
print_status "Removing fail2ban jail config..."
rm -f /etc/fail2ban/jail.local 2>/dev/null || true
systemctl restart fail2ban 2>/dev/null || true
# ============================================================
# 15. ufw
# ============================================================
print_status "Disabling ufw..."
ufw disable 2>/dev/null || true
# ============================================================
# 16. Symlinks
# ============================================================
print_status "Removing symlinks..."
rm -f /usr/local/bin/fd 2>/dev/null || true
rm -f /usr/local/bin/bat 2>/dev/null || true
# ============================================================
# 17. Apt repos
# ============================================================
print_status "Removing apt repos and keys..."
rm -f /etc/apt/sources.list.d/adoptium.list 2>/dev/null || true
rm -f /etc/apt/sources.list.d/github-cli.list 2>/dev/null || true
rm -f /etc/apt/sources.list.d/caddy-stable.list 2>/dev/null || true
rm -f /etc/apt/sources.list.d/microsoft-prod.list 2>/dev/null || true
rm -f /etc/apt/keyrings/adoptium.gpg 2>/dev/null || true
rm -f /usr/share/keyrings/githubcli-archive-keyring.gpg 2>/dev/null || true
rm -f /usr/share/keyrings/caddy-stable-archive-keyring.gpg 2>/dev/null || true
# .NET installed via dotnet-install.sh (not apt), remove its dir + symlink
rm -rf /usr/share/dotnet 2>/dev/null || true
rm -f /usr/local/bin/dotnet 2>/dev/null || true
# ============================================================
# 18. Services
# ============================================================
print_status "Stopping and disabling services..."
systemctl stop clamav-daemon clamav-freshclam fail2ban caddy 2>/dev/null || true
systemctl disable clamav-daemon clamav-freshclam fail2ban caddy 2>/dev/null || true
# ============================================================
# 19. Apt packages
# ============================================================
if [ -f "$MANIFEST" ]; then
mapfile -t PACKAGES < "$MANIFEST" 2>/dev/null || true
if [ "${#PACKAGES[@]}" -ne 0 ]; then
print_status "Purging apt packages from manifest..."
apt purge -y -- "${PACKAGES[@]}" 2>/dev/null || true
else
print_warning "Manifest is empty — skipping apt purge"
fi
else
print_warning "No manifest at $MANIFEST — skipping apt purge"
fi
# ============================================================
# 20. Dev user
# ============================================================
if [[ "$DELETE_DEV_USER" =~ ^[Yy]$ ]]; then
print_status "Deleting user '$DEV_USER'..."
pkill -u "$DEV_USER" 2>/dev/null || true
userdel -r "$DEV_USER" 2>/dev/null || true
else
print_warning "Keeping user '$DEV_USER'"
fi
# ============================================================
# 21. Manifest
# ============================================================
print_status "Removing setup manifest..."
rm -rf /var/lib/vps-setup 2>/dev/null || true
# ============================================================
# 22. apt autoremove
# ============================================================
print_status "Running apt autoremove..."
apt autoremove -y 2>/dev/null || true
echo ""
print_status "Reset complete."
echo ""