8000 fix: throw exception if backup failed (backport #18230) by mergify[bot] · Pull Request #18254 · frappe/frappe · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: throw exception if backup failed (backport #18230) #18254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion frappe/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import frappe.recorder
from frappe.installer import add_to_installed_apps, remove_app
from frappe.utils import add_to_date, get_bench_relative_path, now
from frappe.utils.backups import fetch_latest_backups
from frappe.utils.backups import BackupGenerator, fetch_latest_backups


# TODO: check frappe.cli.coloured_output to set coloured output!
Expand Down Expand Up @@ -317,6 +317,19 @@ def test_partial_restore(self):
self.assertEquals(self.returncode, 0)
self.assertEquals(frappe.db.count("ToDo"), todo_count)

def test_backup_fails_with_exit_code(self):
"""Provide incorrect options to check if exit code is 1"""
odb = BackupGenerator(
frappe.conf.db_name,
frappe.conf.db_name,
frappe.conf.db_password + "INCORRECT PASSWORD",
db_host=frappe.db.host,
db_port=frappe.db.port,
db_type=frappe.conf.db_type,
)
with self.assertRaises(Exception):
odb.take_dump()

def test_recorder(self):
frappe.recorder.stop()

Expand Down
11 changes: 8 additions & 3 deletions frappe/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def unesc(s, esc_chars):
return s


def execute_in_shell(cmd, verbose=0, low_priority=False):
def execute_in_shell(cmd, verbose=False, low_priority=False, check_exit_code=False):
# using Popen instead of os.system - as recommended by python docs
import tempfile
from subprocess import Popen
Expand All @@ -404,20 +404,25 @@ def execute_in_shell(cmd, verbose=0, low_priority=False):
kwargs["preexec_fn"] = lambda: os.nice(10)

p = Popen(cmd, **kwargs)
p.wait()
exit_code = p.wait()

stdout.seek(0)
out = stdout.read()

stderr.seek(0)
err = stderr.read()

if verbose:
failed = check_exit_code and exit_code

if verbose or failed:
if err:
print(err)
if out:
print(out)

if failed:
raise Exception("Command failed")

return err, out


Expand Down
13 changes: 8 additions & 5 deletions frappe/utils/backups.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,9 @@ def take_dump(self):
)

cmd_string = (
"{db_exc} postgres://{user}:{password}@{db_host}:{db_port}/{db_name}"
" {include} {exclude} | {gzip} >> {backup_path_db}"
"self=$$; "
"( {db_exc} postgres://{user}:{password}@{db_host}:{db_port}/{db_name}"
" {include} {exclude} || kill $self ) | {gzip} >> {backup_path_db}"
)

else:
Expand All @@ -405,8 +406,10 @@ def take_dump(self):
)

cmd_string = (
"{db_exc} --single-transaction --quick --lock-tables=false -u {user}"
" -p{password} {db_name} -h {db_host} -P {db_port} {include} {exclude}"
# Remember process of this shell and kill it if mysqldump exits w/ non-zero code
"self=$$; "
" ( {db_exc} --single-transaction --quick --lock-tables=false -u {user}"
" -p{password} {db_name} -h {db_host} -P {db_port} {include} {exclude} || kill $self ) "
" | {gzip} >> {backup_path_db}"
)

Expand All @@ -426,7 +429,7 @@ def take_dump(self):
if self.verbose:
print(command.replace(args.password, "*" * 10) + "\n")

frappe.utils.execute_in_shell(command, low_priority=True)
frappe.utils.execute_in_shell(command, low_priority=True, check_exit_code=True)

def send_email(self):
"""
Expand Down
0