Spaces:
Runtime error
Runtime error
| import os, sys, tarfile | |
| from huggingface_hub import HfApi, hf_hub_download | |
| from datetime import datetime, timedelta | |
| api = HfApi() | |
| repo_id = os.getenv("HF_DATASET") | |
| token = os.getenv("HF_TOKEN") | |
| def restore(): | |
| try: | |
| print(f"--- [SYNC] ๅฏๅจๆขๅคๆต็จ, ็ฎๆ ไปๅบ: {repo_id} ---") | |
| if not repo_id or not token: | |
| print("--- [SYNC] ่ทณ่ฟๆขๅค: ๆช้ ็ฝฎ HF_DATASET ๆ HF_TOKEN ---") | |
| return False | |
| files = api.list_repo_files(repo_id=repo_id, repo_type="dataset", token=token) | |
| now = datetime.now() | |
| for i in range(5): | |
| day = (now - timedelta(days=i)).strftime("%Y-%m-%d") | |
| name = f"backup_{day}.tar.gz" | |
| if name in files: | |
| print(f"--- [SYNC] ๅ็ฐๅคไปฝๆไปถ: {name}, ๆญฃๅจไธ่ฝฝ... ---") | |
| path = hf_hub_download(repo_id=repo_id, filename=name, repo_type="dataset", token=token) | |
| with tarfile.open(path, "r:gz") as tar: tar.extractall(path="/root/.openclaw/") | |
| print(f"--- [SYNC] ๆขๅคๆๅ! ๆฐๆฎๅทฒ่ฆ็่ณ /root/.openclaw/ ---") | |
| return True | |
| print("--- [SYNC] ๆชๆพๅฐๆ่ฟ 5 ๅคฉ็ๅคไปฝๅ ---") | |
| except Exception as e: print(f"--- [SYNC] ๆขๅคๅผๅธธ: {e} ---") | |
| def backup(): | |
| try: | |
| day = datetime.now().strftime("%Y-%m-%d") | |
| name = f"backup_{day}.tar.gz" | |
| print(f"--- [SYNC] ๆญฃๅจๆง่กๅ จ้ๅคไปฝ: {name} ---") | |
| with tarfile.open(name, "w:gz") as tar: | |
| # ่ทฏๅพ่ฏดๆ๏ผsessions(็ฝๅ ณๅๅฒ), workspace(่ฎฐๅฟๆไปถ), agents(้ ็ฝฎ), memory(ๆง็็ฎๅฝ) | |
| for target in ["sessions", "workspace", "agents", "memory", "openclaw.json"]: | |
| full_path = f"/root/.openclaw/{target}" | |
| if os.path.exists(full_path): | |
| tar.add(full_path, arcname=target) | |
| api.upload_file(path_or_fileobj=name, path_in_repo=name, repo_id=repo_id, repo_type="dataset", token=token) | |
| print(f"--- [SYNC] ๅคไปฝไธไผ ๆๅ! ---") | |
| except Exception as e: print(f"--- [SYNC] ๅคไปฝๅคฑ่ดฅ: {e} ---") | |
| if __name__ == "__main__": | |
| if len(sys.argv) > 1 and sys.argv[1] == "backup": backup() | |
| else: restore() |