tao-shen Claude Opus 4.6 commited on
Commit
17404b5
Β·
1 Parent(s): 9c623ef

fix: exclude system binaries from rsync to prevent /bin/sh permission crash

Browse files

HF Dataset (git-based) doesn't preserve Unix file permissions, so
executables restored from backup lose their execute bits. /bin/sh
gets mode 0644 instead of 0755, causing PermissionError in subprocess.

Changes:
- Exclude /bin, /sbin, /lib*, /usr/bin, /usr/sbin, /usr/lib from rsync
(these come from Docker image build and shouldn't be overwritten)
- Exclude Docker-mounted files (/etc/hostname, hosts, resolv.conf)
- Add package list save/restore: save dpkg package list to dataset,
reinstall user-added packages on restore via apt-get
- This preserves user-installed packages without breaking system binaries

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Files changed (1) hide show
  1. ubuntu-server/git_sync_daemon.py +48 -2
ubuntu-server/git_sync_daemon.py CHANGED
@@ -27,9 +27,15 @@ ROOTFS = os.path.join(PERSIST_PATH, "rootfs")
27
 
28
  SYNC_INTERVAL = int(os.environ.get("SYNC_INTERVAL", "120"))
29
 
30
- # Only exclude kernel virtual filesystems and the data dir itself
 
 
 
31
  RSYNC_EXCLUDES = [
32
- "/proc", "/sys", "/dev", "/data",
 
 
 
33
  ]
34
 
35
  UPLOAD_IGNORE = [
@@ -150,6 +156,44 @@ def restore_system():
150
  log(f" Restore had issues (rc={rc}, {elapsed:.1f}s)")
151
 
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  def ensure_passwords():
154
  """Re-ensure user/root passwords after restore."""
155
  log("── PASSWORDS")
@@ -167,6 +211,7 @@ def save_and_upload():
167
  log("══════════════════════════════════════════════")
168
  log("── SAVE: rsync / β†’ /data/rootfs/")
169
  os.makedirs(ROOTFS, exist_ok=True)
 
170
 
171
  excludes = " ".join(f"--exclude='{e}'" for e in RSYNC_EXCLUDES)
172
  t0 = time.time()
@@ -237,6 +282,7 @@ def cmd_init():
237
  t0 = time.time()
238
  download_dataset()
239
  restore_system()
 
240
  ensure_passwords()
241
  elapsed = time.time() - t0
242
  log(f" Init complete ({elapsed:.1f}s)")
 
27
 
28
  SYNC_INTERVAL = int(os.environ.get("SYNC_INTERVAL", "120"))
29
 
30
+ # Exclude: kernel vfs, Docker-managed, and base system directories.
31
+ # System binaries (/bin, /sbin, /lib*) come from the Docker image build;
32
+ # restoring them from HF Dataset breaks permissions (git doesn't preserve mode bits).
33
+ # User-installed packages are tracked separately via dpkg package list.
34
  RSYNC_EXCLUDES = [
35
+ "/proc", "/sys", "/dev", "/data", "/run",
36
+ "/bin", "/sbin", "/lib", "/lib64",
37
+ "/usr/bin", "/usr/sbin", "/usr/lib",
38
+ "/etc/hostname", "/etc/hosts", "/etc/resolv.conf", "/etc/mtab",
39
  ]
40
 
41
  UPLOAD_IGNORE = [
 
156
  log(f" Restore had issues (rc={rc}, {elapsed:.1f}s)")
157
 
158
 
159
+ def restore_packages():
160
+ """Reinstall user-added packages from saved package list."""
161
+ pkg_list = os.path.join(PERSIST_PATH, "user-packages.list")
162
+ base_list = "/etc/base-packages.list"
163
+ if not os.path.exists(pkg_list):
164
+ log("── PACKAGES: no saved package list, skipping")
165
+ return
166
+ if not os.path.exists(base_list):
167
+ log("── PACKAGES: no base package list, skipping")
168
+ return
169
+ log("── PACKAGES: checking for user-installed packages")
170
+ try:
171
+ with open(base_list) as f:
172
+ base = set(f.read().strip().split("\n"))
173
+ with open(pkg_list) as f:
174
+ saved = set(f.read().strip().split("\n"))
175
+ to_install = sorted(saved - base)
176
+ if not to_install:
177
+ log(" No additional packages to install")
178
+ return
179
+ log(f" {len(to_install)} packages to reinstall: {to_install[:10]}...")
180
+ run(f"apt-get update -qq && apt-get install -y --no-install-recommends {' '.join(to_install)}")
181
+ except Exception as e:
182
+ log(f" βœ— Package restore error: {e}")
183
+
184
+
185
+ def save_package_list():
186
+ """Save list of currently installed packages for restore."""
187
+ pkg_list = os.path.join(PERSIST_PATH, "user-packages.list")
188
+ try:
189
+ rc, out = run("dpkg-query -W -f='${Package}\\n'")
190
+ if rc == 0 and out:
191
+ with open(pkg_list, "w") as f:
192
+ f.write(out + "\n")
193
+ except Exception as e:
194
+ log(f" βœ— Package list save error: {e}")
195
+
196
+
197
  def ensure_passwords():
198
  """Re-ensure user/root passwords after restore."""
199
  log("── PASSWORDS")
 
211
  log("══════════════════════════════════════════════")
212
  log("── SAVE: rsync / β†’ /data/rootfs/")
213
  os.makedirs(ROOTFS, exist_ok=True)
214
+ save_package_list()
215
 
216
  excludes = " ".join(f"--exclude='{e}'" for e in RSYNC_EXCLUDES)
217
  t0 = time.time()
 
282
  t0 = time.time()
283
  download_dataset()
284
  restore_system()
285
+ restore_packages()
286
  ensure_passwords()
287
  elapsed = time.time() - t0
288
  log(f" Init complete ({elapsed:.1f}s)")