agenticverse/AgentNexa
0
1#!/usr/bin/env python32"""3Restore /opt/data from latest tar.gz backup on HF Dataset.4 5Env vars:6 HF_TOKEN HF access token with read permission7 HERMES_DATASET_REPO Source dataset repo8 HERMES_HOME Target directory (default: /opt/data)9"""10import os11import sys12import tarfile13 14from huggingface_hub import HfApi, hf_hub_download15 16 17def main() -> None:18 repo_id = os.environ.get("HERMES_DATASET_REPO")19 token = os.environ.get("HF_TOKEN")20 if not repo_id or not token:21 return22 23 state_dir = os.environ.get("HERMES_HOME", "/opt/data")24 os.makedirs(state_dir, exist_ok=True)25 26 try:27 api = HfApi(token=token)28 files = api.list_repo_files(repo_id=repo_id, repo_type="dataset")29 backups = sorted(30 (f for f in files31 if f.startswith("state/backup-") and (f.endswith(".tar") or f.endswith(".tar.gz"))),32 reverse=True,33 )34 if not backups:35 if "state/hermes.tar" in files:36 backups = ["state/hermes.tar"]37 else:38 print("[restore_from_dataset] No backups found.", file=sys.stderr)39 return40 41 for backup_file in backups:42 print(f"[restore_from_dataset] Attempting to restore from: {backup_file}")43 try:44 tar_path = hf_hub_download(45 repo_id=repo_id, repo_type="dataset", filename=backup_file, token=token46 )47 with tarfile.open(tar_path, "r:*") as tf:48 tf.extractall(state_dir)49 print(f"[restore_from_dataset] Successfully restored from {backup_file}")50 return51 except Exception as e:52 print(f"[restore_from_dataset] Failed to restore {backup_file}: {e}", file=sys.stderr)53 54 print("[restore_from_dataset] All backup restore attempts failed.", file=sys.stderr)55 56 except Exception as e:57 print(f"[restore_from_dataset] Restore process failed: {e}", file=sys.stderr)58 59 60if __name__ == "__main__":61 main()62 