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:
@@ -4,6 +4,7 @@ Supports encryption, backups, and configurable config path.
|
|||||||
Thread-safe with atomic writes.
|
Thread-safe with atomic writes.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import hashlib
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
@@ -49,6 +50,7 @@ class ServerStore:
|
|||||||
self._statuses_lock = threading.Lock()
|
self._statuses_lock = threading.Lock()
|
||||||
self._file_lock = threading.Lock()
|
self._file_lock = threading.Lock()
|
||||||
self._last_backup_time: float = 0
|
self._last_backup_time: float = 0
|
||||||
|
self._last_backup_hash: str = ""
|
||||||
self._terminal_font_size: int = 11
|
self._terminal_font_size: int = 11
|
||||||
self._servers_file: str = DEFAULT_SERVERS_FILE
|
self._servers_file: str = DEFAULT_SERVERS_FILE
|
||||||
self._load_settings()
|
self._load_settings()
|
||||||
@@ -192,9 +194,18 @@ class ServerStore:
|
|||||||
if now - self._last_backup_time >= _BACKUP_INTERVAL:
|
if now - self._last_backup_time >= _BACKUP_INTERVAL:
|
||||||
self._auto_backup()
|
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):
|
def _auto_backup(self):
|
||||||
|
current_hash = self._data_hash()
|
||||||
|
if current_hash == self._last_backup_hash:
|
||||||
|
self._last_backup_time = time.time()
|
||||||
|
return
|
||||||
try:
|
try:
|
||||||
self.create_backup()
|
self.create_backup()
|
||||||
|
self._last_backup_hash = current_hash
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.warning(f"Auto-backup failed: {e}")
|
log.warning(f"Auto-backup failed: {e}")
|
||||||
|
|
||||||
@@ -207,6 +218,7 @@ class ServerStore:
|
|||||||
dst = os.path.join(BACKUP_DIR, name)
|
dst = os.path.join(BACKUP_DIR, name)
|
||||||
shutil.copy2(self._servers_file, dst)
|
shutil.copy2(self._servers_file, dst)
|
||||||
self._last_backup_time = time.time()
|
self._last_backup_time = time.time()
|
||||||
|
self._last_backup_hash = self._data_hash()
|
||||||
log.info(f"Backup created: {name}")
|
log.info(f"Backup created: {name}")
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user