fix: skip auto-backup when data unchanged (sha256 hash comparison)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
chrome-storm-c442
2026-02-23 15:40:41 -05:00
parent 7bc2f1bded
commit 5f89ae3322

View File

@@ -4,6 +4,7 @@ Supports encryption, backups, and configurable config path.
Thread-safe with atomic writes.
"""
import hashlib
import json
import os
import shutil
@@ -49,6 +50,7 @@ class ServerStore:
self._statuses_lock = threading.Lock()
self._file_lock = threading.Lock()
self._last_backup_time: float = 0
self._last_backup_hash: str = ""
self._terminal_font_size: int = 11
self._servers_file: str = DEFAULT_SERVERS_FILE
self._load_settings()
@@ -192,9 +194,18 @@ class ServerStore:
if now - self._last_backup_time >= _BACKUP_INTERVAL:
self._auto_backup()
def _data_hash(self) -> str:
text = json.dumps(self._data, sort_keys=True, ensure_ascii=False)
return hashlib.sha256(text.encode("utf-8")).hexdigest()
def _auto_backup(self):
current_hash = self._data_hash()
if current_hash == self._last_backup_hash:
self._last_backup_time = time.time()
return
try:
self.create_backup()
self._last_backup_hash = current_hash
except Exception as e:
log.warning(f"Auto-backup failed: {e}")
@@ -207,6 +218,7 @@ class ServerStore:
dst = os.path.join(BACKUP_DIR, name)
shutil.copy2(self._servers_file, dst)
self._last_backup_time = time.time()
self._last_backup_hash = self._data_hash()
log.info(f"Backup created: {name}")
return name