Skip to content

Commit

Permalink
fix: backport restore command to work around backup restore isse with…
Browse files Browse the repository at this point in the history
  • Loading branch information
WebsiteDeveloper committed Oct 28, 2024
1 parent 43338f3 commit 75a9fd5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
41 changes: 23 additions & 18 deletions frappe/database/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,35 +65,40 @@ def get_database_list(self):

@staticmethod
def restore_database(target, source, user, password):
from frappe.utils import make_esc
from shutil import which

from frappe import _
from frappe.utils import execute_in_shell, make_esc

esc = make_esc("$ ")

from distutils.spawn import find_executable
# Ensure that the entire process fails if any part of the pipeline fails
command = ["set -o pipefail;"]

pv = find_executable("pv")
if pv:
pipe = "{pv} {source} |".format(pv=pv, source=source)
source = ""
# Handle gzipped backups
if source.endswith(".gz"):
if gzip := which("gzip"):
command.extend([gzip, "-cd", source, "|"])
else:
raise Exception("`gzip` not installed")
else:
pipe = ""
source = "< {source}".format(source=source)
command.extend(["cat", source, "|"])

if pipe:
print("Restoring Database file...")
# Newer versions of MariaDB add in a line that'll break on older versions, so remove it
command.extend(["sed", r"'/\/\*M\{0,1\}!999999\\- enable the sandbox mode \*\//d'", "|"])

command = (
"{pipe} mysql -u {user} -p{password} -h{host} "
+ ("-P{port}" if frappe.db.port else "")
+ " {target} {source}"
# Generate the restore command
bin = (
"mysql -u {user} -p{password} -h{host} " + ("-P{port}" if frappe.db.port else "") + " {target}"
)
command = command.format(
pipe=pipe,
bin = bin.format(
user=esc(user),
password=esc(password),
host=esc(frappe.db.host),
target=esc(target),
source=source,
port=frappe.db.port,
)
os.system(command)

command.append(bin)

execute_in_shell(" ".join(command), check_exit_code=True, verbose=False)
8 changes: 7 additions & 1 deletion frappe/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,17 @@ def unesc(s, esc_chars):
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 shutil import which
from subprocess import Popen

with tempfile.TemporaryFile() as stdout:
with tempfile.TemporaryFile() as stderr:
kwargs = {"shell": True, "stdout": stdout, "stderr": stderr}
kwargs = {
"shell": True,
"stdout": stdout,
"stderr": stderr,
"executable": which("bash") or "/bin/bash",
}

if low_priority:
kwargs["preexec_fn"] = lambda: os.nice(10)
Expand Down

0 comments on commit 75a9fd5

Please sign in to comment.