Skip to content

Commit 8b4111d

Browse files
fix: Imported API keys not working on a new server #6477 (#6496)
Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com> Co-authored-by: Michael Genson <genson.michael@gmail.com>
1 parent 9d601ea commit 8b4111d

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

mealie/services/backups_v2/backup_v2.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55
from zipfile import ZipFile
66

7+
from mealie.core.config import get_app_settings
78
from mealie.services._base_service import BaseService
89
from mealie.services.backups_v2.alchemy_exporter import AlchemyExporter
910
from mealie.services.backups_v2.backup_file import BackupFile
@@ -13,6 +14,12 @@ class BackupSchemaMismatch(Exception): ...
1314

1415

1516
class BackupV2(BaseService):
17+
EXCLUDE_DIRS = {"backups", ".temp"}
18+
EXCLUDE_FILES = {"mealie.db", "mealie.log"}
19+
EXCLUDE_EXTENTIONS = {".zip"}
20+
21+
RESTORE_FILES = {".secret"}
22+
1623
def __init__(self, db_url: str | None = None) -> None:
1724
super().__init__()
1825

@@ -33,10 +40,6 @@ def _postgres(self) -> None:
3340

3441
def backup(self) -> Path:
3542
# sourcery skip: merge-nested-ifs, reintroduce-else, remove-redundant-continue
36-
exclude = {"mealie.db", "mealie.log", ".secret"}
37-
exclude_ext = {".zip"}
38-
exclude_dirs = {"backups", ".temp"}
39-
4043
timestamp = datetime.datetime.now(datetime.UTC).strftime("%Y.%m.%d.%H.%M.%S")
4144

4245
backup_name = f"mealie_{timestamp}.zip"
@@ -48,11 +51,11 @@ def backup(self) -> Path:
4851
zip_file.writestr("database.json", json.dumps(database_json))
4952

5053
for data_file in self.directories.DATA_DIR.glob("**/*"):
51-
if data_file.name in exclude:
54+
if data_file.name in self.EXCLUDE_FILES:
5255
continue
5356

54-
if data_file.is_file() and data_file.suffix not in exclude_ext:
55-
if data_file.parent.name in exclude_dirs:
57+
if data_file.is_file() and data_file.suffix not in self.EXCLUDE_EXTENTIONS:
58+
if data_file.parent.name in self.EXCLUDE_DIRS:
5659
continue
5760

5861
zip_file.write(data_file, f"data/{data_file.relative_to(self.directories.DATA_DIR)}")
@@ -62,11 +65,20 @@ def backup(self) -> Path:
6265
def _copy_data(self, data_path: Path) -> None:
6366
for f in data_path.iterdir():
6467
if f.is_file():
68+
if f.name not in self.RESTORE_FILES:
69+
continue
70+
71+
shutil.copyfile(f, self.directories.DATA_DIR / f.name)
6572
continue
6673

6774
shutil.rmtree(self.directories.DATA_DIR / f.name)
6875
shutil.copytree(f, self.directories.DATA_DIR / f.name)
6976

77+
# since we copied a new .secret, AppSettings has the wrong secret info
78+
self.logger.info("invalidating appsettings cache")
79+
get_app_settings.cache_clear()
80+
self.settings = get_app_settings()
81+
7082
def restore(self, backup_path: Path) -> None:
7183
self.logger.info("initializing backup restore")
7284

@@ -105,5 +117,4 @@ def restore(self, backup_path: Path) -> None:
105117
self.logger.info("restoring data directory")
106118
self._copy_data(contents.data_directory)
107119
self.logger.info("data directory restored successfully")
108-
109120
self.logger.info("backup restore complete")

tests/unit_tests/services_tests/backup_v2_tests/test_backup_v2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ def test_database_restore_data():
249249

250250
settings = get_app_settings()
251251
backup_v2 = BackupV2(settings.DB_URL)
252+
253+
backup_v2.directories.BACKUP_DIR.mkdir(parents=True, exist_ok=True)
252254
original_data_backup = backup_v2.backup()
253255

254256
try:

0 commit comments

Comments
 (0)