| #!/usr/bin/env bash |
| set -euo pipefail |
|
|
| REPO_ID="${REPO_ID:-umer07/vllm-deployement-backup}" |
| REPO_TYPE="${REPO_TYPE:-dataset}" |
| WORKDIR="${WORKDIR:-/root/fathom-restore}" |
| DOWNLOAD_LATEST=0 |
| START_SERVICES=0 |
| HF_TOKEN="${HF_TOKEN:-${HUGGING_FACE_HUB_TOKEN:-}}" |
|
|
| usage() { |
| cat <<'EOF' |
| Usage: |
| restore_fathom_vm.sh [--download-latest] [--start] [--workdir DIR] [--repo-id ID] [--hf-token TOKEN] |
|
|
| Restores the latest Fathom vLLM VM backup from Hugging Face. |
|
|
| Expected current runtime: |
| - vLLM via systemd: fathom-vllm.service |
| - vLLM OpenAI-compatible API: http://127.0.0.1:8000/v1 |
| - Backend: http://127.0.0.1:7860 |
| - Dashboard: http://127.0.0.1:3000 |
|
|
| The target VM should be a ROCm/vLLM-capable image. The DigitalOcean vLLM ROCm |
| marketplace image provides the expected container named "rocm". |
| EOF |
| } |
|
|
| while [[ $# -gt 0 ]]; do |
| case "$1" in |
| --download-latest) DOWNLOAD_LATEST=1; shift ;; |
| --start) START_SERVICES=1; shift ;; |
| --workdir) WORKDIR="$2"; shift 2 ;; |
| --repo-id) REPO_ID="$2"; shift 2 ;; |
| --hf-token) HF_TOKEN="$2"; shift 2 ;; |
| -h|--help) usage; exit 0 ;; |
| *) echo "Unknown argument: $1" >&2; usage; exit 1 ;; |
| esac |
| done |
|
|
| if [[ ${EUID} -ne 0 ]]; then |
| echo "Run this script as root." >&2 |
| exit 1 |
| fi |
|
|
| mkdir -p "${WORKDIR}" |
| cd "${WORKDIR}" |
|
|
| if [[ ${DOWNLOAD_LATEST} -eq 1 ]]; then |
| if [[ -z "${HF_TOKEN}" ]]; then |
| echo "HF_TOKEN is required with --download-latest." >&2 |
| exit 1 |
| fi |
| python3 -m pip install --break-system-packages -q -U huggingface_hub >/dev/null 2>&1 || true |
| export REPO_ID REPO_TYPE HF_TOKEN WORKDIR |
| python3 - <<'PY' |
| import json |
| import os |
| import re |
| import sys |
| from collections import defaultdict |
| from huggingface_hub import HfApi, hf_hub_download |
|
|
| repo_id = os.environ["REPO_ID"] |
| repo_type = os.environ["REPO_TYPE"] |
| token = os.environ["HF_TOKEN"] |
| workdir = os.environ["WORKDIR"] |
| api = HfApi(token=token) |
| files = api.list_repo_files(repo_id=repo_id, repo_type=repo_type) |
|
|
| required = { |
| "app": r"^vllm_deployement_backup_(\d{8}_\d{6})\.tar\.gz$", |
| "minio": r"^fathom_minio_data_(\d{8}_\d{6})\.tar\.gz$", |
| "neo4j_data": r"^fathom_neo4j_data_(\d{8}_\d{6})\.tar\.gz$", |
| "neo4j_logs": r"^fathom_neo4j_logs_(\d{8}_\d{6})\.tar\.gz$", |
| } |
| optional = { |
| "hf_cache": r"^fathom_hf_cache_(\d{8}_\d{6})\.tar\.gz$", |
| "adapters": r"^fathom_adapters_(\d{8}_\d{6})\.tar\.gz$", |
| "guide": r"^VM_RECOVERY_GUIDE_(\d{8}_\d{6})\.md$", |
| "manifest": r"^vllm_deployement_backup_manifest_(\d{8}_\d{6})\.txt$", |
| "restore": r"^restore_fathom_vm\.sh$", |
| } |
|
|
| groups = defaultdict(dict) |
| for name in files: |
| base = name.rsplit("/", 1)[-1] |
| if "/" in name and not name.startswith("backups/"): |
| continue |
| for key, pattern in {**required, **optional}.items(): |
| m = re.match(pattern, base) |
| if m: |
| groups[m.group(1) if key != "restore" else "restore"][key] = name |
|
|
| complete = [ts for ts, found in groups.items() if ts != "restore" and set(required) <= set(found)] |
| if not complete: |
| print("No complete backup set found.", file=sys.stderr) |
| sys.exit(1) |
|
|
| ts = sorted(complete)[-1] |
| targets = dict(groups[ts]) |
| if "restore" in groups: |
| targets.update(groups["restore"]) |
|
|
| print(f"Downloading backup timestamp {ts}") |
| for key, name in sorted(targets.items()): |
| path = hf_hub_download( |
| repo_id=repo_id, |
| repo_type=repo_type, |
| filename=name, |
| token=token, |
| local_dir=workdir, |
| ) |
| print(f"{key}: {path}") |
| PY |
| fi |
|
|
| latest_file() { |
| local pattern="$1" |
| find "${WORKDIR}" -maxdepth 3 -type f -name "${pattern}" -printf '%f\t%p\n' 2>/dev/null | sort | tail -n 1 | cut -f2- |
| } |
|
|
| APP_ARCHIVE="$(latest_file 'vllm_deployement_backup_*.tar.gz')" |
| MINIO_ARCHIVE="$(latest_file 'fathom_minio_data_*.tar.gz')" |
| NEO4J_DATA_ARCHIVE="$(latest_file 'fathom_neo4j_data_*.tar.gz')" |
| NEO4J_LOGS_ARCHIVE="$(latest_file 'fathom_neo4j_logs_*.tar.gz')" |
| HF_CACHE_ARCHIVE="$(latest_file 'fathom_hf_cache_*.tar.gz' || true)" |
| ADAPTERS_ARCHIVE="$(latest_file 'fathom_adapters_*.tar.gz' || true)" |
|
|
| for required_file in "${APP_ARCHIVE}" "${MINIO_ARCHIVE}" "${NEO4J_DATA_ARCHIVE}" "${NEO4J_LOGS_ARCHIVE}"; do |
| if [[ -z "${required_file}" || ! -f "${required_file}" ]]; then |
| echo "Missing required backup file in ${WORKDIR}." >&2 |
| exit 1 |
| fi |
| done |
|
|
| restore_volume() { |
| local volume="$1" |
| local archive="$2" |
| if [[ -z "${archive}" || ! -f "${archive}" ]]; then |
| echo "Skipping optional volume ${volume}; archive not present." |
| return |
| fi |
| echo "Restoring Docker volume ${volume}" |
| docker volume create "${volume}" >/dev/null |
| docker run --rm \ |
| -v "${volume}:/restore" \ |
| -v "$(dirname "${archive}"):/backup" \ |
| alpine sh -c "cd /restore && rm -rf ./* ./.??* 2>/dev/null || true && tar -xzf /backup/$(basename "${archive}")" |
| } |
|
|
| echo "Extracting application/vLLM archive at /" |
| tar -xzf "${APP_ARCHIVE}" -C / |
| chmod +x /usr/local/bin/fathom-vllm-start /usr/local/bin/fathom-vllm-stop 2>/dev/null || true |
| systemctl daemon-reload || true |
|
|
| restore_volume fathom_minio_data "${MINIO_ARCHIVE}" |
| restore_volume fathom_neo4j_data "${NEO4J_DATA_ARCHIVE}" |
| restore_volume fathom_neo4j_logs "${NEO4J_LOGS_ARCHIVE}" |
| restore_volume fathom_hf_cache "${HF_CACHE_ARCHIVE}" |
| restore_volume fathom_fathom_adapters "${ADAPTERS_ARCHIVE}" |
|
|
| if [[ ${START_SERVICES} -eq 1 ]]; then |
| echo "Starting vLLM service" |
| systemctl enable fathom-vllm.service >/dev/null 2>&1 || true |
| systemctl restart fathom-vllm.service || true |
| for i in $(seq 1 40); do |
| if curl -fsS http://127.0.0.1:8000/v1/models >/dev/null 2>&1; then |
| break |
| fi |
| sleep 15 |
| done |
|
|
| echo "Starting Docker stack" |
| cd /opt/fathom |
| docker compose up -d --build |
| fi |
|
|
| echo "Restore complete." |
|
|