44from pathlib import Path
55from zipfile import ZipFile
66
7+ from mealie .core .config import get_app_settings
78from mealie .services ._base_service import BaseService
89from mealie .services .backups_v2 .alchemy_exporter import AlchemyExporter
910from mealie .services .backups_v2 .backup_file import BackupFile
@@ -13,6 +14,12 @@ class BackupSchemaMismatch(Exception): ...
1314
1415
1516class 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" )
0 commit comments