Skip to content

Commit

Permalink
feat: ignore tar: file changed as we read it during backups (backport
Browse files Browse the repository at this point in the history
#26462) (#26495)

* feat: ignore `tar: file changed as we read it` during backups

This seems to occur when new files are being created as we're archiving
the files on a site. Doesn't make sense to fail the entire backup
because of that.

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
(cherry picked from commit 803b45b)

* fix: add `errors=replace` to decode() call

Signed-off-by: Akhil Narang <me@akhilnarang.dev>
(cherry picked from commit e3e6834)

---------

Co-authored-by: Akhil Narang <me@akhilnarang.dev>
  • Loading branch information
mergify[bot] and akhilnarang committed May 22, 2024
1 parent 22e278f commit 30f767e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
7 changes: 7 additions & 0 deletions frappe/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,10 @@ class InvalidKeyError(ValidationError):
http_status_code = 401
title = "Invalid Key"
message = "The document key is invalid"


class CommandFailedError(Exception):
def __init__(self, message: str, out: str, err: str):
super().__init__(message)
self.out = out
self.err = err
4 changes: 3 additions & 1 deletion frappe/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,9 @@ def execute_in_shell(cmd, verbose=False, low_priority=False, check_exit_code=Fal
print(out)

if failed:
raise Exception("Command failed")
raise frappe.CommandFailedError(
"Command failed", out.decode(errors="replace"), err.decode(errors="replace")
)

return err, out

Expand Down
21 changes: 15 additions & 6 deletions frappe/utils/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,21 @@ def backup_files(self):
else:
cmd_string = "tar -cf {0} {1}"

frappe.utils.execute_in_shell(
cmd_string.format(backup_path, files_path),
verbose=self.verbose,
low_priority=True,
check_exit_code=True,
)
try:
frappe.utils.execute_in_shell(
cmd_string.format(backup_path, files_path),
verbose=self.verbose,
low_priority=True,
check_exit_code=True,
)
except frappe.CommandFailedError as e:
if e.err and "file changed as we read it" in e.err:
click.secho(
"Ignoring `tar: file changed as we read it` to prevent backup failure",
fg="red",
)
else:
raise e

def copy_site_config(self):
site_config_backup_path = self.backup_path_conf
Expand Down

0 comments on commit 30f767e

Please sign in to comment.