File size: 7,312 Bytes
a2afe1d
 
29b0e0e
 
 
a2afe1d
 
fca54b2
 
 
 
 
 
 
 
 
a2afe1d
29b0e0e
a2afe1d
 
 
 
 
 
 
29b0e0e
a2afe1d
0961a21
fca54b2
29b0e0e
0961a21
29b0e0e
 
0961a21
29b0e0e
0961a21
 
29b0e0e
 
fca54b2
 
 
 
0961a21
 
 
 
fca54b2
 
 
 
 
0961a21
fca54b2
 
 
0961a21
 
 
 
 
 
fca54b2
 
 
 
29b0e0e
fca54b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29b0e0e
fca54b2
 
29b0e0e
 
 
fca54b2
a2afe1d
fca54b2
a2afe1d
fca54b2
 
 
0961a21
fca54b2
 
 
 
0961a21
fca54b2
 
 
 
0961a21
fca54b2
a2afe1d
0961a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fca54b2
 
0961a21
fca54b2
0961a21
 
 
 
 
 
 
 
 
 
 
 
 
fca54b2
0961a21
 
 
 
 
 
fca54b2
0961a21
fca54b2
 
 
 
 
 
 
 
 
 
 
 
 
 
0961a21
 
 
 
fca54b2
 
a2afe1d
29b0e0e
fca54b2
 
 
 
 
 
 
 
29b0e0e
fca54b2
 
0961a21
 
 
 
 
fca54b2
 
 
 
a2afe1d
 
fca54b2
 
 
 
 
 
 
 
 
 
 
 
 
 
29b0e0e
fca54b2
 
0961a21
 
fca54b2
 
0961a21
 
fca54b2
 
0961a21
 
 
fca54b2
 
 
a2afe1d
fca54b2
a2afe1d
fca54b2
a2afe1d
29b0e0e
a2afe1d
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import os
import time
import tarfile
import subprocess
from huggingface_hub import HfApi, hf_hub_download, login

# ================= CONFIG =================
DATASET        = "Vvbbnnn/Vps-tar"

ROOT_TAR       = "/tmp/root.tar.gz"
ROOT_FILE      = "root.tar.gz"

APT_FILE       = "apt.txt"
PIP_FILE       = "pip.txt"

SYNC_INTERVAL  = 3600

# πŸ” Auth
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
    raise ValueError("❌ HF_TOKEN not set")

login(token=HF_TOKEN)
api = HfApi()

# ================= HELPERS =================

# Use API to check existence instead of downloading full file
def exists(filename):
    try:
        info = api.get_paths_info(
            repo_id=DATASET,
            repo_type="dataset",
            paths=[filename]
        )
        return len(list(info)) > 0
    except Exception:
        return False

# ---------- SAVE DEPS ----------

def save_apt():
    print("πŸ“„ Saving apt...")
    subprocess.run(
        "dpkg --get-selections | grep -w 'install$' | awk '{print $1}' > /tmp/apt.txt",
        shell=True
    )

def save_pip():
    print("πŸ“„ Saving pip...")
    subprocess.run("/root/venv/bin/pip freeze > /tmp/pip.txt", shell=True)

# ---------- FILTER ----------

def should_skip(path):
    p = path.lower()
    if "node_modules" in p: return True
    if "npm" in p:          return True
    if "node" in p:         return True
    if "venv" in p:         return True
    if "__pycache__" in p:  return True
    if ".git" in p:         return True
    return False

# ---------- CREATE TAR ----------

def create_tar():
    print("πŸ“¦ Creating ROOT backup (strict clean)...")

    def filter_fn(tarinfo):
        if should_skip(tarinfo.name):
            return None
        return tarinfo

    with tarfile.open(ROOT_TAR, "w:gz") as tar:
        tar.add("/root", arcname="root", filter=filter_fn)

    size = os.path.getsize(ROOT_TAR) / 1024 / 1024
    print(f"βœ… root.tar created ({size:.2f} MB)")
    return True

# ---------- UPLOAD ----------

def upload(local, remote):
    print(f"⬆️ Uploading {remote}")
    api.upload_file(
        path_or_fileobj=local,
        path_in_repo=remote,
        repo_id=DATASET,
        repo_type="dataset"
    )
    print(f"βœ… Uploaded {remote}")

# ---------- DOWNLOAD ----------

def download(remote, local):
    print(f"⬇️ Downloading {remote}")
    try:
        local_dir = os.path.dirname(local)
        hf_hub_download(
            repo_id=DATASET,
            repo_type="dataset",
            filename=remote,
            local_dir=local_dir,
            local_dir_use_symlinks=False
        )
        return True
    except Exception as e:
        print(f"❌ Download failed for {remote}: {e}")
        return False

# ---------- SMART RESTORE ----------

def get_tar_paths(tar_path):
    """
    Returns a set of absolute paths that exist inside the tar.
    Tar stores entries as e.g. 'root/subdir/file' β†’ '/root/subdir/file'
    """
    paths = set()
    with tarfile.open(tar_path, "r:gz") as tar:
        for member in tar.getmembers():
            # arcname was "root", so members look like "root/..." β†’ "/root/..."
            abs_path = "/" + member.name.lstrip("/")
            paths.add(abs_path.rstrip("/"))
    return paths

def smart_delete_root(tar_paths):
    """
    Walk /root and delete ONLY files/folders that do NOT exist in the tar.
    Skips protected paths (node/venv/npm etc).
    Uses topdown=False so deepest items are checked first,
    allowing empty dirs to be cleaned up safely.
    """
    print("🧹 Removing files not present in tar...")

    to_delete = []

    for dirpath, dirnames, filenames in os.walk("/root", topdown=False):
        # Check files
        for fname in filenames:
            full_path = os.path.join(dirpath, fname).rstrip("/")
            if should_skip(full_path):
                continue
            if full_path not in tar_paths:
                to_delete.append(full_path)

        # Check directories
        for dname in dirnames:
            full_path = os.path.join(dirpath, dname).rstrip("/")
            if should_skip(full_path):
                continue
            if full_path not in tar_paths:
                to_delete.append(full_path)

    for path in to_delete:
        print(f"  πŸ—‘οΈ  {path}")
        subprocess.run(["rm", "-rf", path])

    print(f"βœ… Removed {len(to_delete)} items not in tar")

def restore_root():
    """
    Smart restore:
      1. Read all paths that exist inside the tar
      2. Delete ONLY /root items NOT in the tar  (no full wipe)
      3. Extract tar β†’ overwrites changed files, adds missing ones
    """
    print("πŸ”„ Smart restoring /root...")

    tar_paths = get_tar_paths(ROOT_TAR)
    smart_delete_root(tar_paths)

    print("πŸ“‚ Extracting tar...")
    with tarfile.open(ROOT_TAR, "r:gz") as tar:
        try:
            # Python 3.12+ safe extraction filter
            tar.extractall("/", filter="data")
        except TypeError:
            # Fallback for older Python versions
            tar.extractall("/")

    print("βœ… Root restore complete")

# ---------- RESTORE DEPS ----------

def restore_apt():
    print("πŸ”„ Restoring apt...")
    subprocess.run("apt-get update", shell=True)
    subprocess.run("xargs -a /tmp/apt.txt apt-get install -y", shell=True)

def restore_pip():
    print("πŸ”„ Restoring pip...")
    subprocess.run("/root/venv/bin/pip install -r /tmp/pip.txt", shell=True)

def restore_node():
    print("πŸ”„ Restoring node...")
    subprocess.run(
        ["npm", "install", "--omit=dev"],
        cwd="/root/app"
    )

# ================= SYNC =================

def sync():
    print("πŸ”„ Sync started")

    save_apt()
    save_pip()

    upload("/tmp/apt.txt", APT_FILE)
    upload("/tmp/pip.txt", PIP_FILE)

    create_tar()
    upload(ROOT_TAR, ROOT_FILE)

    # Clean up all temp files
    for tmp_file in [ROOT_TAR, "/tmp/apt.txt", "/tmp/pip.txt"]:
        if os.path.exists(tmp_file):
            os.remove(tmp_file)
            print(f"🧹 Cleaned up {tmp_file}")

    print("βœ… Sync complete")

# ================= MAIN =================

def main():
    print("πŸš€ Full ROOT Backup System Started")

    first_run = not (
        exists(ROOT_FILE) and
        exists(APT_FILE) and
        exists(PIP_FILE)
    )

    # ---------- FIRST RUN ----------
    if first_run:
        print("πŸ†• First run β†’ creating initial backup")
        sync()

    # ---------- RESTORE ----------
    else:
        print("☁️ Restoring from dataset...")

        if not download(APT_FILE, "/tmp/apt.txt"):
            raise RuntimeError("❌ Failed to download apt.txt β€” aborting restore!")
        restore_apt()

        if not download(PIP_FILE, "/tmp/pip.txt"):
            raise RuntimeError("❌ Failed to download pip.txt β€” aborting restore!")
        restore_pip()

        # Most critical check β€” never touch /root without a valid tar
        if not download(ROOT_FILE, ROOT_TAR):
            raise RuntimeError("❌ Failed to download root.tar.gz β€” aborting restore to protect /root!")
        restore_root()

        restore_node()

    # ---------- LOOP ----------
    while True:
        print(f"⏳ Waiting {SYNC_INTERVAL}s...")
        time.sleep(SYNC_INTERVAL)
        sync()

if __name__ == "__main__":
    main()