Download backup.py from TsunTzu/EB: direct link, hf CLI and curl.
- Browser
- Download file 4.32 kB
-
https://huggingface.co/spaces/TsunTzu/EB/resolve/main/backup.py
- Command line
-
hf download hf://spaces/TsunTzu/EB/backup.py
-
curl -L -o backup.py https://huggingface.co/spaces/TsunTzu/EB/resolve/main/backup.py
4.32 kB
| import os | |
| import subprocess | |
| import zipfile | |
| from huggingface_hub import HfApi, create_repo, delete_repo, hf_hub_download, list_datasets | |
| TOKEN = os.getenv("HF_TOKEN") | |
| REPO_PRIMARY = "TsunTzu/emby-backup-a" | |
| REPO_SECONDARY = "TsunTzu/emby-backup-b" | |
| CONFIG_PATH = "/config" | |
| ZIP_NAME = "emby_backup.zip" | |
| api = HfApi() | |
| # --- Backup ထဲတွင် မပါဝင်စေရန် ချန်လှပ်ထားမည့် Folder များ --- | |
| SKIP_FOLDERS = ['cache', 'logs', 'transcoding-temp', 'metadata/people', 'data/logs'] | |
| def get_active_repo(): | |
| if not TOKEN: | |
| return False, False | |
| try: | |
| datasets = [d.id for d in list_datasets(author="TsunTzu", token=TOKEN)] | |
| has_a = REPO_PRIMARY in datasets | |
| has_b = REPO_SECONDARY in datasets | |
| return has_a, has_b | |
| except Exception as e: | |
| print(f"Error listing datasets: {e}") | |
| return False, False | |
| def zip_config_smart(output_filename, source_dir): | |
| print(f"--- Creating Optimized Zip: {output_filename} ---") | |
| with zipfile.ZipFile(output_filename, 'w', zipfile.ZIP_DEFLATED) as zipf: | |
| for root, dirs, files in os.walk(source_dir): | |
| # Folder များကို စစ်ထုတ်ခြင်း | |
| rel_path = os.path.relpath(root, source_dir).lower() | |
| if any(skip in rel_path for skip in SKIP_FOLDERS): | |
| continue | |
| for file in files: | |
| file_path = os.path.join(root, file) | |
| # 50MB ထက်ကြီးသော ဖိုင်များကို ကျော်သွားရန် (ဥပမာ- ကြီးမားလွန်းသော logs/temp ဖြတ်သန်းမှုများ) | |
| if os.path.getsize(file_path) > 50 * 1024 * 1024: | |
| continue | |
| zipf.write(file_path, os.path.relpath(file_path, source_dir)) | |
| def save_config(): | |
| if not TOKEN: | |
| print("Error: HF_TOKEN missing!") | |
| return | |
| print("--- Running Media Sync before Backup ---") | |
| try: | |
| subprocess.run(["python3", "/sync.py"], check=True) | |
| except Exception as e: | |
| print(f"Sync issue before backup: {e}") | |
| has_a, has_b = get_active_repo() | |
| target_repo = REPO_SECONDARY if has_a else REPO_PRIMARY | |
| old_repo = REPO_PRIMARY if has_a else REPO_SECONDARY | |
| try: | |
| print(f"--- Creating Target Repo: {target_repo} ---") | |
| create_repo(repo_id=target_repo, repo_type="dataset", token=TOKEN, private=True, exist_ok=True) | |
| print("--- Zipping Config Folder Smartly ---") | |
| zip_config_smart(ZIP_NAME, CONFIG_PATH) | |
| print(f"--- Uploading to: {target_repo} ---") | |
| api.upload_file( | |
| path_or_fileobj=ZIP_NAME, | |
| path_in_repo="emby_backup.zip", | |
| repo_id=target_repo, | |
| repo_type="dataset", | |
| token=TOKEN | |
| ) | |
| if (has_a or has_b): | |
| print(f"--- Cleaning Old History: Deleting {old_repo} ---") | |
| try: | |
| delete_repo(repo_id=old_repo, repo_type="dataset", token=TOKEN) | |
| except Exception as e: | |
| print(f"Could not delete old repo: {e}") | |
| print(f"Backup Complete in {target_repo}! File: {ZIP_NAME}") | |
| if os.path.exists(ZIP_NAME): | |
| os.remove(ZIP_NAME) | |
| except Exception as e: | |
| print(f"Backup Failed: {e}") | |
| def restore_config(): | |
| if not TOKEN: | |
| print("HF_TOKEN missing, skipping restore.") | |
| return | |
| has_a, has_b = get_active_repo() | |
| target = None | |
| if has_a: target = REPO_PRIMARY | |
| if has_b: target = REPO_SECONDARY | |
| if not target: | |
| print("No backup found in both repos. Starting fresh.") | |
| return | |
| try: | |
| print(f"--- Restoring from: {target} ---") | |
| file_path = hf_hub_download(repo_id=target, filename="emby_backup.zip", repo_type="dataset", token=TOKEN) | |
| with zipfile.ZipFile(file_path, 'r') as zip_ref: | |
| zip_ref.extractall(CONFIG_PATH) | |
| print("Restore Success!") | |
| except Exception as e: | |
| print(f"Restore Failed: {e}") | |
| if __name__ == "__main__": | |
| import sys | |
| if len(sys.argv) > 1: | |
| if sys.argv[1] == "save": save_config() | |
| elif sys.argv[1] == "restore": restore_config() |