Delete backup_restore.py
Browse files- backup_restore.py +0 -147
backup_restore.py
DELETED
|
@@ -1,147 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
#!/usr/bin/env python3
|
| 4 |
-
"""
|
| 5 |
-
OpenClaw Full Backup & Restore
|
| 6 |
-
- Backup directories: sessions, agent, memory, tasks
|
| 7 |
-
- Never overwrite openclaw.json
|
| 8 |
-
"""
|
| 9 |
-
|
| 10 |
-
import os
|
| 11 |
-
import sys
|
| 12 |
-
import tarfile
|
| 13 |
-
import tempfile
|
| 14 |
-
from pathlib import Path
|
| 15 |
-
|
| 16 |
-
# Config - Dataset name
|
| 17 |
-
DATASET_REPO = "syw1516/opencalw2-backup"
|
| 18 |
-
BACKUP_FILENAME = "sessions_backup.tar.gz" # 保持旧名称以兼容已有备份
|
| 19 |
-
|
| 20 |
-
# Backup directories
|
| 21 |
-
BACKUP_DIRS = [
|
| 22 |
-
Path("/root/.openclaw/agents/main/sessions"),
|
| 23 |
-
Path("/root/.openclaw/agents/main/agent"),
|
| 24 |
-
Path("/root/.openclaw/memory"),
|
| 25 |
-
Path("/root/.openclaw/tasks"),
|
| 26 |
-
]
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def get_hf_token():
|
| 30 |
-
"""Get HF token from multiple possible env vars"""
|
| 31 |
-
return (
|
| 32 |
-
os.environ.get("HF_TOKEN") or
|
| 33 |
-
os.environ.get("SPACE_TOKEN") or
|
| 34 |
-
os.environ.get("HUGGING_FACE_HUB_TOKEN") or
|
| 35 |
-
""
|
| 36 |
-
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
def backup():
|
| 40 |
-
"""Backup multiple directories to HF dataset"""
|
| 41 |
-
try:
|
| 42 |
-
from huggingface_hub import upload_file
|
| 43 |
-
|
| 44 |
-
token = get_hf_token()
|
| 45 |
-
if not token:
|
| 46 |
-
print("No HF token found, skipping backup")
|
| 47 |
-
return
|
| 48 |
-
|
| 49 |
-
# Check if any directory has content
|
| 50 |
-
dirs_to_backup = []
|
| 51 |
-
for d in BACKUP_DIRS:
|
| 52 |
-
if d.exists() and any(d.iterdir()):
|
| 53 |
-
dirs_to_backup.append(d)
|
| 54 |
-
|
| 55 |
-
if not dirs_to_backup:
|
| 56 |
-
print("No data to backup")
|
| 57 |
-
return
|
| 58 |
-
|
| 59 |
-
print(f"Backing up: {[str(d) for d in dirs_to_backup]}...")
|
| 60 |
-
temp_dir = tempfile.mkdtemp()
|
| 61 |
-
backup_path = os.path.join(temp_dir, BACKUP_FILENAME)
|
| 62 |
-
|
| 63 |
-
with tarfile.open(backup_path, "w:gz") as tar:
|
| 64 |
-
for d in dirs_to_backup:
|
| 65 |
-
arcname = str(d).replace("/root/.openclaw/", "")
|
| 66 |
-
tar.add(str(d), arcname=arcname)
|
| 67 |
-
|
| 68 |
-
print(f"Uploading to {DATASET_REPO}...")
|
| 69 |
-
upload_file(
|
| 70 |
-
repo_id=DATASET_REPO,
|
| 71 |
-
repo_type="dataset",
|
| 72 |
-
path_or_fileobj=backup_path,
|
| 73 |
-
path_in_repo=BACKUP_FILENAME,
|
| 74 |
-
token=token
|
| 75 |
-
)
|
| 76 |
-
print("Backup complete")
|
| 77 |
-
|
| 78 |
-
except ImportError:
|
| 79 |
-
print("huggingface_hub not installed, skipping backup")
|
| 80 |
-
except Exception as e:
|
| 81 |
-
print(f"Backup error: {e}")
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
def restore():
|
| 85 |
-
"""Restore from HF dataset (never overwrite config)"""
|
| 86 |
-
try:
|
| 87 |
-
from huggingface_hub import hf_hub_download
|
| 88 |
-
|
| 89 |
-
token = get_hf_token()
|
| 90 |
-
if not token:
|
| 91 |
-
print("No HF token found, skipping restore")
|
| 92 |
-
return
|
| 93 |
-
|
| 94 |
-
# Ensure parent directories exist
|
| 95 |
-
for d in BACKUP_DIRS:
|
| 96 |
-
d.parent.mkdir(parents=True, exist_ok=True)
|
| 97 |
-
|
| 98 |
-
print(f"Restoring from {DATASET_REPO}...")
|
| 99 |
-
temp_dir = tempfile.mkdtemp()
|
| 100 |
-
|
| 101 |
-
try:
|
| 102 |
-
downloaded = hf_hub_download(
|
| 103 |
-
repo_id=DATASET_REPO,
|
| 104 |
-
filename=BACKUP_FILENAME,
|
| 105 |
-
repo_type="dataset",
|
| 106 |
-
local_dir=temp_dir,
|
| 107 |
-
local_files_only=False,
|
| 108 |
-
token=token
|
| 109 |
-
)
|
| 110 |
-
except Exception as e:
|
| 111 |
-
if "404" in str(e) or "Not Found" in str(e):
|
| 112 |
-
print("No backup found (404), starting fresh")
|
| 113 |
-
return
|
| 114 |
-
raise
|
| 115 |
-
|
| 116 |
-
if os.path.exists(downloaded):
|
| 117 |
-
print(f"Found backup: {downloaded}")
|
| 118 |
-
# Extract all directories
|
| 119 |
-
with tarfile.open(downloaded, "r:gz") as tar:
|
| 120 |
-
for member in tar.getmembers():
|
| 121 |
-
tar.extract(member, path="/root/.openclaw/")
|
| 122 |
-
print(f" Extracted: {member.name}")
|
| 123 |
-
print("Restore complete (config not touched)")
|
| 124 |
-
else:
|
| 125 |
-
print("No backup found, starting fresh")
|
| 126 |
-
|
| 127 |
-
except Exception as e:
|
| 128 |
-
print(f"Restore error: {e}")
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
def main():
|
| 132 |
-
if len(sys.argv) < 2:
|
| 133 |
-
print("Usage: backup_restore.py [restore|backup]")
|
| 134 |
-
sys.exit(1)
|
| 135 |
-
|
| 136 |
-
command = sys.argv[1]
|
| 137 |
-
if command == "restore":
|
| 138 |
-
restore()
|
| 139 |
-
elif command == "backup":
|
| 140 |
-
backup()
|
| 141 |
-
else:
|
| 142 |
-
print(f"Unknown command: {command}")
|
| 143 |
-
sys.exit(1)
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
if __name__ == "__main__":
|
| 147 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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