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

Create autobackup.py

Browse files
Files changed (1) hide show
  1. autobackup.py +52 -0
autobackup.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, subprocess, time, threading
2
+
3
+ def run_backup():
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 backup")
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
+
23
+ cmd = [
24
+ "hf", "upload",
25
+ repo_id,
26
+ local_path,
27
+ "--repo-type", repo_type,
28
+ "--token", hf_token,
29
+ "--ignore-patterns", "*.git", "__pycache__",
30
+ ]
31
+
32
+ process = subprocess.Popen(
33
+ cmd,
34
+ stdout=subprocess.PIPE,
35
+ stderr=subprocess.STDOUT,
36
+ env=env,
37
+ text=True,
38
+ cwd="/tmp",
39
+ )
40
+
41
+ for line in iter(process.stdout.readline, ''):
42
+ print("🔄", line.strip())
43
+
44
+ def schedule_backups():
45
+ while True:
46
+ print("⏳ Running auto-backup...")
47
+ run_backup()
48
+ print("✅ Backup finished, sleeping 45m...")
49
+ time.sleep(45 * 60)
50
+
51
+ # Run in background
52
+ threading.Thread(target=schedule_backups, daemon=True).start()