umer07 commited on
Commit
baf2722
·
verified ·
1 Parent(s): 04c0408

Update latest Fathom vLLM backup pointer 20260417_053924

Browse files
Files changed (1) hide show
  1. latest/restore_fathom_vm.sh +179 -0
latest/restore_fathom_vm.sh ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ REPO_ID="${REPO_ID:-umer07/vllm-deployement-backup}"
5
+ REPO_TYPE="${REPO_TYPE:-dataset}"
6
+ WORKDIR="${WORKDIR:-/root/fathom-restore}"
7
+ DOWNLOAD_LATEST=0
8
+ START_SERVICES=0
9
+ HF_TOKEN="${HF_TOKEN:-${HUGGING_FACE_HUB_TOKEN:-}}"
10
+
11
+ usage() {
12
+ cat <<'EOF'
13
+ Usage:
14
+ restore_fathom_vm.sh [--download-latest] [--start] [--workdir DIR] [--repo-id ID] [--hf-token TOKEN]
15
+
16
+ Restores the latest Fathom vLLM VM backup from Hugging Face.
17
+
18
+ Expected current runtime:
19
+ - vLLM via systemd: fathom-vllm.service
20
+ - vLLM OpenAI-compatible API: http://127.0.0.1:8000/v1
21
+ - Backend: http://127.0.0.1:7860
22
+ - Dashboard: http://127.0.0.1:3000
23
+
24
+ The target VM should be a ROCm/vLLM-capable image. The DigitalOcean vLLM ROCm
25
+ marketplace image provides the expected container named "rocm".
26
+ EOF
27
+ }
28
+
29
+ while [[ $# -gt 0 ]]; do
30
+ case "$1" in
31
+ --download-latest) DOWNLOAD_LATEST=1; shift ;;
32
+ --start) START_SERVICES=1; shift ;;
33
+ --workdir) WORKDIR="$2"; shift 2 ;;
34
+ --repo-id) REPO_ID="$2"; shift 2 ;;
35
+ --hf-token) HF_TOKEN="$2"; shift 2 ;;
36
+ -h|--help) usage; exit 0 ;;
37
+ *) echo "Unknown argument: $1" >&2; usage; exit 1 ;;
38
+ esac
39
+ done
40
+
41
+ if [[ ${EUID} -ne 0 ]]; then
42
+ echo "Run this script as root." >&2
43
+ exit 1
44
+ fi
45
+
46
+ mkdir -p "${WORKDIR}"
47
+ cd "${WORKDIR}"
48
+
49
+ if [[ ${DOWNLOAD_LATEST} -eq 1 ]]; then
50
+ if [[ -z "${HF_TOKEN}" ]]; then
51
+ echo "HF_TOKEN is required with --download-latest." >&2
52
+ exit 1
53
+ fi
54
+ python3 -m pip install --break-system-packages -q -U huggingface_hub >/dev/null 2>&1 || true
55
+ export REPO_ID REPO_TYPE HF_TOKEN WORKDIR
56
+ python3 - <<'PY'
57
+ import json
58
+ import os
59
+ import re
60
+ import sys
61
+ from collections import defaultdict
62
+ from huggingface_hub import HfApi, hf_hub_download
63
+
64
+ repo_id = os.environ["REPO_ID"]
65
+ repo_type = os.environ["REPO_TYPE"]
66
+ token = os.environ["HF_TOKEN"]
67
+ workdir = os.environ["WORKDIR"]
68
+ api = HfApi(token=token)
69
+ files = api.list_repo_files(repo_id=repo_id, repo_type=repo_type)
70
+
71
+ required = {
72
+ "app": r"^vllm_deployement_backup_(\d{8}_\d{6})\.tar\.gz$",
73
+ "minio": r"^fathom_minio_data_(\d{8}_\d{6})\.tar\.gz$",
74
+ "neo4j_data": r"^fathom_neo4j_data_(\d{8}_\d{6})\.tar\.gz$",
75
+ "neo4j_logs": r"^fathom_neo4j_logs_(\d{8}_\d{6})\.tar\.gz$",
76
+ }
77
+ optional = {
78
+ "hf_cache": r"^fathom_hf_cache_(\d{8}_\d{6})\.tar\.gz$",
79
+ "adapters": r"^fathom_adapters_(\d{8}_\d{6})\.tar\.gz$",
80
+ "guide": r"^VM_RECOVERY_GUIDE_(\d{8}_\d{6})\.md$",
81
+ "manifest": r"^vllm_deployement_backup_manifest_(\d{8}_\d{6})\.txt$",
82
+ "restore": r"^restore_fathom_vm\.sh$",
83
+ }
84
+
85
+ groups = defaultdict(dict)
86
+ for name in files:
87
+ base = name.rsplit("/", 1)[-1]
88
+ if "/" in name and not name.startswith("backups/"):
89
+ continue
90
+ for key, pattern in {**required, **optional}.items():
91
+ m = re.match(pattern, base)
92
+ if m:
93
+ groups[m.group(1) if key != "restore" else "restore"][key] = name
94
+
95
+ complete = [ts for ts, found in groups.items() if ts != "restore" and set(required) <= set(found)]
96
+ if not complete:
97
+ print("No complete backup set found.", file=sys.stderr)
98
+ sys.exit(1)
99
+
100
+ ts = sorted(complete)[-1]
101
+ targets = dict(groups[ts])
102
+ if "restore" in groups:
103
+ targets.update(groups["restore"])
104
+
105
+ print(f"Downloading backup timestamp {ts}")
106
+ for key, name in sorted(targets.items()):
107
+ path = hf_hub_download(
108
+ repo_id=repo_id,
109
+ repo_type=repo_type,
110
+ filename=name,
111
+ token=token,
112
+ local_dir=workdir,
113
+ )
114
+ print(f"{key}: {path}")
115
+ PY
116
+ fi
117
+
118
+ latest_file() {
119
+ local pattern="$1"
120
+ find "${WORKDIR}" -maxdepth 3 -type f -name "${pattern}" -printf '%f\t%p\n' 2>/dev/null | sort | tail -n 1 | cut -f2-
121
+ }
122
+
123
+ APP_ARCHIVE="$(latest_file 'vllm_deployement_backup_*.tar.gz')"
124
+ MINIO_ARCHIVE="$(latest_file 'fathom_minio_data_*.tar.gz')"
125
+ NEO4J_DATA_ARCHIVE="$(latest_file 'fathom_neo4j_data_*.tar.gz')"
126
+ NEO4J_LOGS_ARCHIVE="$(latest_file 'fathom_neo4j_logs_*.tar.gz')"
127
+ HF_CACHE_ARCHIVE="$(latest_file 'fathom_hf_cache_*.tar.gz' || true)"
128
+ ADAPTERS_ARCHIVE="$(latest_file 'fathom_adapters_*.tar.gz' || true)"
129
+
130
+ for required_file in "${APP_ARCHIVE}" "${MINIO_ARCHIVE}" "${NEO4J_DATA_ARCHIVE}" "${NEO4J_LOGS_ARCHIVE}"; do
131
+ if [[ -z "${required_file}" || ! -f "${required_file}" ]]; then
132
+ echo "Missing required backup file in ${WORKDIR}." >&2
133
+ exit 1
134
+ fi
135
+ done
136
+
137
+ restore_volume() {
138
+ local volume="$1"
139
+ local archive="$2"
140
+ if [[ -z "${archive}" || ! -f "${archive}" ]]; then
141
+ echo "Skipping optional volume ${volume}; archive not present."
142
+ return
143
+ fi
144
+ echo "Restoring Docker volume ${volume}"
145
+ docker volume create "${volume}" >/dev/null
146
+ docker run --rm \
147
+ -v "${volume}:/restore" \
148
+ -v "$(dirname "${archive}"):/backup" \
149
+ alpine sh -c "cd /restore && rm -rf ./* ./.??* 2>/dev/null || true && tar -xzf /backup/$(basename "${archive}")"
150
+ }
151
+
152
+ echo "Extracting application/vLLM archive at /"
153
+ tar -xzf "${APP_ARCHIVE}" -C /
154
+ chmod +x /usr/local/bin/fathom-vllm-start /usr/local/bin/fathom-vllm-stop 2>/dev/null || true
155
+ systemctl daemon-reload || true
156
+
157
+ restore_volume fathom_minio_data "${MINIO_ARCHIVE}"
158
+ restore_volume fathom_neo4j_data "${NEO4J_DATA_ARCHIVE}"
159
+ restore_volume fathom_neo4j_logs "${NEO4J_LOGS_ARCHIVE}"
160
+ restore_volume fathom_hf_cache "${HF_CACHE_ARCHIVE}"
161
+ restore_volume fathom_fathom_adapters "${ADAPTERS_ARCHIVE}"
162
+
163
+ if [[ ${START_SERVICES} -eq 1 ]]; then
164
+ echo "Starting vLLM service"
165
+ systemctl enable fathom-vllm.service >/dev/null 2>&1 || true
166
+ systemctl restart fathom-vllm.service || true
167
+ for i in $(seq 1 40); do
168
+ if curl -fsS http://127.0.0.1:8000/v1/models >/dev/null 2>&1; then
169
+ break
170
+ fi
171
+ sleep 15
172
+ done
173
+
174
+ echo "Starting Docker stack"
175
+ cd /opt/fathom
176
+ docker compose up -d --build
177
+ fi
178
+
179
+ echo "Restore complete."