Spaces:
Runtime error
Runtime error
Update sync_manager.py
Browse files- sync_manager.py +76 -23
sync_manager.py
CHANGED
|
@@ -15,6 +15,7 @@ TOKEN = os.getenv("HF_TOKEN")
|
|
| 15 |
|
| 16 |
BASE_DIR = Path("/root/.openclaw")
|
| 17 |
PREFIX = "openclawai_backup_"
|
|
|
|
| 18 |
KEEP_LAST = 5
|
| 19 |
|
| 20 |
# กรองเฉพาะไฟล์ที่ต้องการ และตัดไฟล์ระบบ/ไฟล์ชั่วคราวทิ้ง
|
|
@@ -28,7 +29,6 @@ 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):
|
|
@@ -54,54 +54,107 @@ def create_zip(zip_path):
|
|
| 54 |
def backup():
|
| 55 |
if not REPO_ID or not TOKEN: return log("❌ Config missing")
|
| 56 |
|
| 57 |
-
|
|
|
|
| 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(
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 105 |
backup()
|
| 106 |
else:
|
| 107 |
restore()
|
|
|
|
| 15 |
|
| 16 |
BASE_DIR = Path("/root/.openclaw")
|
| 17 |
PREFIX = "openclawai_backup_"
|
| 18 |
+
CONFIG_FILE_NAME = "openclaw.json"
|
| 19 |
KEEP_LAST = 5
|
| 20 |
|
| 21 |
# กรองเฉพาะไฟล์ที่ต้องการ และตัดไฟล์ระบบ/ไฟล์ชั่วคราวทิ้ง
|
|
|
|
| 29 |
print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)
|
| 30 |
|
| 31 |
def is_valid_file(path: Path):
|
|
|
|
| 32 |
if path.suffix.lower() not in ALLOWED_EXTENSIONS:
|
| 33 |
return False
|
| 34 |
if any(pattern in path.name for pattern in IGNORE_PATTERNS):
|
|
|
|
| 54 |
def backup():
|
| 55 |
if not REPO_ID or not TOKEN: return log("❌ Config missing")
|
| 56 |
|
| 57 |
+
timestamp = time.strftime("%Y%m%d_%H%M%S")
|
| 58 |
+
zip_name = f"{PREFIX}{timestamp}.zip"
|
| 59 |
zip_path = Path(f"/tmp/{zip_name}")
|
| 60 |
|
| 61 |
try:
|
| 62 |
if BASE_DIR.exists() and create_zip(zip_path):
|
| 63 |
+
log(f"📤 Uploading full backup: {zip_name}")
|
| 64 |
+
api.upload_file(
|
| 65 |
+
path_or_fileobj=str(zip_path),
|
| 66 |
+
path_in_repo=zip_name,
|
| 67 |
+
repo_id=REPO_ID,
|
| 68 |
+
repo_type="dataset",
|
| 69 |
+
token=TOKEN
|
| 70 |
+
)
|
| 71 |
|
| 72 |
+
# แถม: อัปโหลด openclaw.json แยกไว้เผื่อ Step 2 ของการ Restore
|
| 73 |
+
config_file = BASE_DIR / CONFIG_FILE_NAME
|
| 74 |
+
if config_file.exists():
|
| 75 |
+
api.upload_file(
|
| 76 |
+
path_or_fileobj=str(config_file),
|
| 77 |
+
path_in_repo=CONFIG_FILE_NAME,
|
| 78 |
+
repo_id=REPO_ID,
|
| 79 |
+
repo_type="dataset",
|
| 80 |
+
token=TOKEN
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
cleanup_old_backups()
|
| 84 |
+
log("✅ Backup successful")
|
| 85 |
finally:
|
| 86 |
if zip_path.exists(): zip_path.unlink()
|
| 87 |
|
| 88 |
+
def cleanup_old_backups():
|
| 89 |
+
try:
|
| 90 |
+
contents = list_repo_files(REPO_ID, repo_type="dataset", token=TOKEN)
|
| 91 |
+
old_backups = sorted([f for f in contents if f.startswith(PREFIX) and f.endswith(".zip")])
|
| 92 |
+
for old in old_backups[:-KEEP_LAST]:
|
| 93 |
+
api.delete_file(path_in_repo=old, repo_id=REPO_ID, repo_type="dataset", token=TOKEN)
|
| 94 |
+
log(f"🗑️ Deleted old backup: {old}")
|
| 95 |
+
except Exception as e:
|
| 96 |
+
log(f"⚠️ Cleanup failed: {e}")
|
| 97 |
+
|
| 98 |
def restore():
|
| 99 |
+
if not REPO_ID or not TOKEN:
|
| 100 |
+
log("❌ Missing HF config")
|
| 101 |
+
return False
|
| 102 |
+
|
| 103 |
try:
|
| 104 |
contents = list_repo_files(REPO_ID, repo_type="dataset", token=TOKEN)
|
| 105 |
+
|
| 106 |
+
# 1. Restore Full Backup (ZIP)
|
| 107 |
+
backups = sorted([f for f in contents if f.startswith(PREFIX) and f.endswith(".zip")])
|
| 108 |
+
if backups:
|
| 109 |
+
latest = backups[-1]
|
| 110 |
+
log(f"📥 Step 1: Restoring full backup from {latest}")
|
| 111 |
+
zip_path = hf_hub_download(
|
| 112 |
+
repo_id=REPO_ID, filename=latest, repo_type="dataset", token=TOKEN
|
| 113 |
+
)
|
| 114 |
+
if BASE_DIR.exists():
|
| 115 |
+
shutil.rmtree(BASE_DIR)
|
| 116 |
+
BASE_DIR.mkdir(parents=True)
|
| 117 |
+
log("📦 Extracting full backup...")
|
| 118 |
+
with zipfile.ZipFile(zip_path, "r") as z:
|
| 119 |
+
z.extractall(BASE_DIR)
|
| 120 |
+
log("✅ Full backup restored.")
|
| 121 |
+
else:
|
| 122 |
+
log("⚠️ No full backup found. Skipping Step 1.")
|
| 123 |
+
BASE_DIR.mkdir(parents=True, exist_ok=True)
|
| 124 |
|
| 125 |
+
# 2. Restore openclaw.json (แยกไฟล์ - เผื่อกรณีไฟล์ใน ZIP เก่ากว่า)
|
| 126 |
+
if CONFIG_FILE_NAME in contents:
|
| 127 |
+
log(f"📥 Step 2: Restoring {CONFIG_FILE_NAME} from Dataset")
|
| 128 |
+
try:
|
| 129 |
+
config_path = hf_hub_download(
|
| 130 |
+
repo_id=REPO_ID, filename=CONFIG_FILE_NAME, repo_type="dataset", token=TOKEN
|
| 131 |
+
)
|
| 132 |
+
shutil.copy(config_path, BASE_DIR / CONFIG_FILE_NAME)
|
| 133 |
+
log("✅ Config file restored.")
|
| 134 |
+
except Exception as e:
|
| 135 |
+
log(f"⚠️ Failed to restore config: {e}")
|
| 136 |
+
else:
|
| 137 |
+
log("⚠️ No separate openclaw.json found. Step 2 skipped.")
|
| 138 |
|
| 139 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
except Exception as e:
|
| 141 |
log(f"❌ Restore failed: {e}")
|
| 142 |
+
return False
|
| 143 |
|
| 144 |
# =========================
|
| 145 |
# MAIN
|
| 146 |
# =========================
|
| 147 |
if __name__ == "__main__":
|
| 148 |
+
# รับ action จาก command line (default: restore)
|
| 149 |
action = sys.argv[1].lower() if len(sys.argv) > 1 else "restore"
|
| 150 |
|
| 151 |
if action == "backup":
|
| 152 |
log("🔄 Backup loop active (Hourly at :00)")
|
| 153 |
while True:
|
| 154 |
now = time.localtime()
|
| 155 |
+
# รอจนถึงต้นชั่วโมงถัดไป
|
| 156 |
+
wait_sec = (60 - now.tm_min) * 60 - now.tm_sec
|
| 157 |
+
time.sleep(max(0, wait_sec))
|
| 158 |
backup()
|
| 159 |
else:
|
| 160 |
restore()
|