Spaces:
Paused
Paused
| import os, subprocess, time, threading | |
| def run_backup(): | |
| repo_id = os.environ.get("BACKUP_REPO") | |
| hf_token = os.environ.get("HF_TOKEN") | |
| repo_type = "space" if "/" in repo_id else "dataset" # auto-detect | |
| if not repo_id or not hf_token: | |
| print("❌ BACKUP_REPO or HF_TOKEN missing, skipping backup") | |
| return | |
| env = os.environ.copy() | |
| env["HF_HOME"] = "/tmp/hf_cache" | |
| env["XDG_CACHE_HOME"] = "/tmp/xdg_cache" | |
| env["TMPDIR"] = "/tmp" | |
| os.makedirs(env["HF_HOME"], exist_ok=True) | |
| os.makedirs(env["XDG_CACHE_HOME"], exist_ok=True) | |
| os.makedirs(env["TMPDIR"], exist_ok=True) | |
| local_path = "/workspace" | |
| cmd = [ | |
| "hf", "upload", | |
| repo_id, | |
| local_path, | |
| "--repo-type", repo_type, | |
| "--token", hf_token, | |
| ] | |
| process = subprocess.Popen( | |
| cmd, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT, | |
| env=env, | |
| text=True, | |
| cwd="/tmp", | |
| ) | |
| for line in iter(process.stdout.readline, ''): | |
| print("🔄", line.strip()) | |
| def schedule_backups(): | |
| while True: | |
| print("⏳ Running auto-backup...") | |
| run_backup() | |
| print("✅ Backup finished, sleeping 45m...") | |
| time.sleep(45 * 60) | |
| # Run in background | |
| threading.Thread(target=schedule_backups, daemon=True).start() | |