refactor: DB dump executable logic (#22799)

This commit is contained in:
Ankush Menat 2023-10-18 14:24:40 +05:30 committed by GitHub
parent 8df5402b1f
commit d1111fb189
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -362,22 +362,38 @@ class BackupGenerator:
with open(site_config_backup_path, "w") as n, open(site_config_path) as c:
n.write(c.read())
def get_db_dump_exeuctable(self) -> str:
db_exc, exists = None, False
if self.db_type == "mariadb":
if mariadb_dump_path := which("mariadb-dump"):
exists = bool(mariadb_dump_path)
db_exc = "mariadb-dump"
else:
# Fallback to mysqldump if mariadb-dump is not available.
db_exc = "mysqldump"
exists = bool(which(db_exc))
elif self.db_type == "postgres":
db_exc = "pg_dump"
exists = bool(which(db_exc))
if not exists:
frappe.throw(
f"{db_exc} not found in PATH! This is required to take a backup.",
exc=frappe.ExecutableNotFound,
)
return db_exc
def take_dump(self):
import frappe.utils
from frappe.utils.change_log import get_app_branch
db_exc = {
"mariadb": ("mariadb-dump", which("mariadb-dump") or which("mysqldump")),
"postgres": ("pg_dump", which("pg_dump")),
}[self.db_type]
db_exc = self.get_db_dump_exeuctable()
gzip_exc = which("gzip")
if not (gzip_exc and db_exc[1]):
_exc = "gzip" if not gzip_exc else db_exc[0]
if not gzip_exc:
frappe.throw(
f"{_exc} not found in PATH! This is required to take a backup.", exc=frappe.ExecutableNotFound
"`gzip` not found in PATH! This is required to take a backup.", exc=frappe.ExecutableNotFound
)
db_exc = db_exc[0]
database_header_content = [
f"Backup generated by Frappe {frappe.__version__} on branch {get_app_branch('frappe') or 'N/A'}",