R1000 commited on
Commit
6faa78f
·
verified ·
1 Parent(s): 5e4d7a3

Create sync_manager.py

Browse files
Files changed (1) hide show
  1. sync_manager.py +107 -0
sync_manager.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import time
4
+ import shutil
5
+ import zipfile
6
+ from pathlib import Path
7
+ from huggingface_hub import HfApi, hf_hub_download, list_repo_files
8
+
9
+ # =========================
10
+ # CONFIG
11
+ # =========================
12
+ api = HfApi()
13
+ REPO_ID = os.getenv("HF_DATASET")
14
+ TOKEN = os.getenv("HF_TOKEN")
15
+
16
+ BASE_DIR = Path("/root/.openclaw")
17
+ PREFIX = "openclawai_backup_"
18
+ KEEP_LAST = 5
19
+
20
+ # กรองเฉพาะไฟล์ที่ต้องการ และตัดไฟล์ระบบ/ไฟล์ชั่วคราวทิ้ง
21
+ ALLOWED_EXTENSIONS = {'.md', '.json', '.db', '.sqlite', '.py'}
22
+ IGNORE_PATTERNS = {'.lock', '.tmp', '.wal', '.shm', '__pycache__'}
23
+
24
+ # =========================
25
+ # UTILS
26
+ # =========================
27
+ def log(msg):
28
+ print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)
29
+
30
+ def is_valid_file(path: Path):
31
+ # ต้องมีนามสกุลที่กำหนด และไม่อยู่ในกลุ่ม Ignore
32
+ if path.suffix.lower() not in ALLOWED_EXTENSIONS:
33
+ return False
34
+ if any(pattern in path.name for pattern in IGNORE_PATTERNS):
35
+ return False
36
+ return True
37
+
38
+ # =========================
39
+ # CORE FUNCTIONS
40
+ # =========================
41
+ def create_zip(zip_path):
42
+ files = [p for p in BASE_DIR.rglob("*") if p.is_file() and is_valid_file(p)]
43
+
44
+ if not files:
45
+ log("⚠️ No important files found to backup")
46
+ return False
47
+
48
+ log(f"📦 Zipping {len(files)} files...")
49
+ with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as z:
50
+ for p in files:
51
+ z.write(p, p.relative_to(BASE_DIR))
52
+ return True
53
+
54
+ def backup():
55
+ if not REPO_ID or not TOKEN: return log("❌ Config missing")
56
+
57
+ zip_name = f"{PREFIX}{time.strftime('%Y%m%d_%H%M%S')}.zip"
58
+ zip_path = Path(f"/tmp/{zip_name}")
59
+
60
+ try:
61
+ if BASE_DIR.exists() and create_zip(zip_path):
62
+ log(f"📤 Uploading: {zip_name}")
63
+ api.upload_file(path_or_fileobj=str(zip_path), path_in_repo=zip_name,
64
+ repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
65
+
66
+ # Cleanup old backups
67
+ contents = list_repo_files(REPO_ID, repo_type="dataset", token=TOKEN)
68
+ old_backups = sorted([f for f in contents if f.startswith(PREFIX)])
69
+ for old in old_backups[:-KEEP_LAST]:
70
+ api.delete_file(path_in_repo=old, repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
71
+ log(f"🗑️ Deleted: {old}")
72
+ finally:
73
+ if zip_path.exists(): zip_path.unlink()
74
+
75
+ def restore():
76
+ try:
77
+ contents = list_repo_files(REPO_ID, repo_type="dataset", token=TOKEN)
78
+ backups = sorted([f for f in contents if f.startswith(PREFIX)])
79
+ if not backups: return log("⚠️ No backup found")
80
+
81
+ latest = backups[-1]
82
+ log(f"📥 Restoring: {latest}")
83
+ zip_path = hf_hub_download(repo_id=REPO_ID, filename=latest, repo_type="dataset", token=TOKEN)
84
+
85
+ if BASE_DIR.exists(): shutil.rmtree(BASE_DIR)
86
+ BASE_DIR.mkdir(parents=True, exist_ok=True)
87
+
88
+ with zipfile.ZipFile(zip_path, "r") as z:
89
+ z.extractall(BASE_DIR)
90
+ log("✅ Restore successful!")
91
+ except Exception as e:
92
+ log(f"❌ Restore failed: {e}")
93
+
94
+ # =========================
95
+ # MAIN
96
+ # =========================
97
+ if __name__ == "__main__":
98
+ action = sys.argv[1].lower() if len(sys.argv) > 1 else "restore"
99
+
100
+ if action == "backup":
101
+ log("🔄 Backup loop active (Hourly at :00)")
102
+ while True:
103
+ now = time.localtime()
104
+ time.sleep(max(0, (60 - now.tm_min) * 60 - now.tm_sec))
105
+ backup()
106
+ else:
107
+ restore()