Skip to content

Commit

Permalink
Merge pull request #26145 from akhilnarang/fix-failed-backup-deletion
Browse files Browse the repository at this point in the history
fix(backup): delete failed backup even if something fails during the backup process
  • Loading branch information
akhilnarang committed Apr 25, 2024
2 parents e455b7b + ad55f59 commit 9e8808f
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions frappe/utils/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,12 @@ def get_backup(self, older_than=24, ignore_files=False, force=False):
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()
Expand Down Expand Up @@ -492,6 +490,24 @@ def add_to_rollback(self, func: Callable) -> None:
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."""
Expand Down

0 comments on commit 9e8808f

Please sign in to comment.