rekysda commited on
Commit
3d3526c
·
verified ·
1 Parent(s): 4cf3210

Create sync.py

Browse files
Files changed (1) hide show
  1. sync.py +68 -0
sync.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import tarfile
4
+ import time
5
+ from huggingface_hub import HfApi, hf_hub_download
6
+
7
+ api = HfApi()
8
+ repo_id = os.getenv("HF_DATASET")
9
+ token = os.getenv("HF_TOKEN")
10
+ FILENAME = "latest_backup.tar.gz"
11
+ BACKUP_PATH = "/root/.openclaw"
12
+
13
+ def restore():
14
+ """Restore data from Hugging Face Dataset on startup."""
15
+ if not repo_id or not token:
16
+ print("⚠️ Skip Restore: HF_DATASET or HF_TOKEN not set")
17
+ return False
18
+ try:
19
+ print(f"⬇️ Downloading {FILENAME} from {repo_id}...")
20
+ path = hf_hub_download(repo_id=repo_id, filename=FILENAME, repo_type="dataset", token=token)
21
+ if os.path.exists(path):
22
+ with tarfile.open(path, "r:gz") as tar:
23
+ tar.extractall(path=BACKUP_PATH)
24
+ print(f"✅ Success: Restored from {FILENAME}")
25
+ return True
26
+ except Exception as e:
27
+ print(f"ℹ️ Restore Note: No existing backup found or error: {e}")
28
+ return False
29
+
30
+ def backup():
31
+ """Backup data to Hugging Face Dataset periodically."""
32
+ if not repo_id or not token:
33
+ return
34
+ try:
35
+ temp_tar = "/tmp/latest_backup.tar.gz"
36
+ paths_to_backup = [
37
+ "sessions",
38
+ "agents/main/sessions",
39
+ "openclaw.json",
40
+ "credentials",
41
+ "agents/main/SOUL.md",
42
+ "agents/main/USER.md",
43
+ "agents/main/IDENTITY.md"
44
+ ]
45
+ with tarfile.open(temp_tar, "w:gz") as tar:
46
+ for p in paths_to_backup:
47
+ full_path = os.path.join(BACKUP_PATH, p)
48
+ if os.path.exists(full_path):
49
+ arcname = p
50
+ tar.add(full_path, arcname=arcname)
51
+ api.upload_file(
52
+ path_or_fileobj=temp_tar,
53
+ path_in_repo=FILENAME,
54
+ repo_id=repo_id,
55
+ repo_type="dataset",
56
+ token=token
57
+ )
58
+ print(f"✅ Backup {FILENAME} Success at {time.strftime('%X')}")
59
+ if os.path.exists(temp_tar):
60
+ os.remove(temp_tar)
61
+ except Exception as e:
62
+ print(f"❌ Backup Error: {e}")
63
+
64
+ if __name__ == "__main__":
65
+ if len(sys.argv) > 1 and sys.argv[1] == "backup":
66
+ backup()
67
+ else:
68
+ restore()