ThongCoder commited on
Commit
d15ca45
·
verified ·
1 Parent(s): 7bd555c

Create restore.py

Browse files
Files changed (1) hide show
  1. restore.py +64 -0
restore.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import tempfile
4
+ import shutil
5
+
6
+ BACKUP_REPO = os.environ.get("BACKUP_REPO")
7
+ HF_TOKEN = os.environ.get("HF_TOKEN")
8
+
9
+ if not BACKUP_REPO or not HF_TOKEN:
10
+ print("[Restore] Skipping: BACKUP_REPO or HF_TOKEN not set")
11
+ exit(0)
12
+
13
+ env = os.environ.copy()
14
+ env["HF_HOME"] = "/tmp/hf_cache"
15
+ env["XDG_CACHE_HOME"] = "/tmp/xdg_cache"
16
+ env["TMPDIR"] = "/tmp"
17
+ env["HF_TOKEN"] = HF_TOKEN
18
+
19
+ os.makedirs(env["HF_HOME"], exist_ok=True)
20
+ os.makedirs(env["XDG_CACHE_HOME"], exist_ok=True)
21
+ os.makedirs(env["TMPDIR"], exist_ok=True)
22
+
23
+ # Step 1: List repo files
24
+ print("[Restore] Listing backups in repo...")
25
+ result = subprocess.run(
26
+ ["huggingface-cli", "ls-files", BACKUP_REPO, "--repo-type", "dataset"],
27
+ stdout=subprocess.PIPE,
28
+ stderr=subprocess.STDOUT,
29
+ text=True,
30
+ env=env,
31
+ )
32
+ files = result.stdout.strip().splitlines()
33
+ backups = [f for f in files if f.endswith(".tar.gz")]
34
+
35
+ if not backups:
36
+ print("[Restore] No backups found")
37
+ exit(0)
38
+
39
+ # Step 2: Pick latest (they’re timestamped)
40
+ latest = sorted(backups)[-1]
41
+ print(f"[Restore] Found latest backup: {latest}")
42
+
43
+ # Step 3: Download
44
+ tmpfile = os.path.join(tempfile.gettempdir(), "restore.tar.gz")
45
+ subprocess.run(
46
+ ["huggingface-cli", "download", BACKUP_REPO, latest, "--repo-type", "dataset", "--local-dir", "/tmp/restore", "--force-download"],
47
+ check=True,
48
+ env=env,
49
+ )
50
+
51
+ downloaded = os.path.join("/tmp/restore", latest)
52
+
53
+ # Step 4: Extract into /home/vscode
54
+ print("[Restore] Extracting backup...")
55
+ if os.path.exists("/home/vscode"):
56
+ shutil.rmtree("/home/vscode")
57
+ os.makedirs("/home/vscode", exist_ok=True)
58
+
59
+ subprocess.run(
60
+ ["tar", "-xzf", downloaded, "-C", "/"],
61
+ check=True
62
+ )
63
+
64
+ print("[Restore] Completed")