File size: 4,316 Bytes
040a95f
 
442b7ff
040a95f
 
 
 
 
 
442b7ff
040a95f
 
 
442b7ff
 
 
040a95f
442b7ff
 
040a95f
 
 
 
 
 
 
 
 
442b7ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
040a95f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442b7ff
 
040a95f
 
 
442b7ff
040a95f
 
 
 
 
 
 
 
 
 
 
442b7ff
040a95f
442b7ff
 
 
040a95f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442b7ff
 
 
040a95f
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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()