Merge pull request #26145 from akhilnarang/fix-failed-backup-deletion

fix(backup): delete failed backup even if something fails during the backup process
This commit is contained in:
Akhil Narang 2024-04-25 18:09:52 +05:30 committed by GitHub
commit 9e8808f06c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -188,14 +188,12 @@ class BackupGenerator:
self.set_backup_file_name()
if not (last_db and last_file and last_private_file and site_config_backup_path):
self.take_dump()
self.add_to_rollback(lambda: os.remove(self.backup_path_db))
self.copy_site_config()
self.add_to_rollback(lambda: os.remove(self.backup_path_conf))
self.delete_if_step_fails(self.take_dump, self.backup_path_db)
self.delete_if_step_fails(self.copy_site_config, self.backup_path_conf)
if not ignore_files:
self.backup_files()
self.add_to_rollback(lambda: os.remove(self.backup_path_files))
self.add_to_rollback(lambda: os.remove(self.backup_path_private_files))
self.delete_if_step_fails(
self.backup_files, self.backup_path_files, self.backup_path_private_files
)
if frappe.get_system_settings("encrypt_backup"):
self.backup_encryption()
@ -492,6 +490,24 @@ download only after 24 hours."""
if self.rollback_callback:
self.rollback_callback.add(func)
def delete_if_step_fails(self, step: Callable, *paths: str):
"""
Deletes the given path if the given step fails
:param step: The step to execute
:param paths: The paths to delete
:return: Nothing
"""
try:
step()
except Exception as e:
for path in paths:
if os.path.exists(path):
os.remove(path)
raise e
for path in paths:
self.add_to_rollback(lambda: os.remove(path))
def _get_tables(doctypes: list[str], existing_tables: list[str]) -> list[str]:
"""Return a list of tables for the given doctypes that exist in the database."""