ThongCoder commited on
Commit
bdba199
·
verified ·
1 Parent(s): 6bfa8ce

Create restore.py

Browse files
Files changed (1) hide show
  1. restore.py +46 -0
restore.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, subprocess
2
+
3
+ def restore_workspace():
4
+ repo_id = os.environ.get("BACKUP_REPO")
5
+ hf_token = os.environ.get("HF_TOKEN")
6
+ repo_type = "space" if "/" in repo_id else "dataset" # auto-detect
7
+
8
+ if not repo_id or not hf_token:
9
+ print("❌ BACKUP_REPO or HF_TOKEN missing, skipping restore")
10
+ return
11
+
12
+ env = os.environ.copy()
13
+ env["HF_HOME"] = "/tmp/hf_cache"
14
+ env["XDG_CACHE_HOME"] = "/tmp/xdg_cache"
15
+ env["TMPDIR"] = "/tmp"
16
+
17
+ os.makedirs(env["HF_HOME"], exist_ok=True)
18
+ os.makedirs(env["XDG_CACHE_HOME"], exist_ok=True)
19
+ os.makedirs(env["TMPDIR"], exist_ok=True)
20
+
21
+ local_path = "/workspace"
22
+ os.makedirs(local_path, exist_ok=True)
23
+
24
+ cmd = [
25
+ "hf", "download",
26
+ repo_id,
27
+ "--repo-type", repo_type,
28
+ "--token", hf_token,
29
+ "--local-dir", local_path,
30
+ "--exclude", ".git",
31
+ ]
32
+
33
+ print("🔄 Restoring workspace from backup...")
34
+ process = subprocess.Popen(
35
+ cmd,
36
+ stdout=subprocess.PIPE,
37
+ stderr=subprocess.STDOUT,
38
+ env=env,
39
+ text=True,
40
+ cwd="/tmp",
41
+ )
42
+
43
+ for line in iter(process.stdout.readline, ''):
44
+ print("📥", line.strip())
45
+
46
+ print("✅ Restore complete.")