|
| 1 | +# flake8: noqa F501 |
| 2 | +import pathlib |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +from unittest.mock import patch |
| 6 | +from masonite.configuration import config |
| 7 | +from shutil import make_archive |
| 8 | +from datetime import datetime |
| 9 | +from masonite.utils.location import base_path |
| 10 | +import subprocess |
| 11 | +import gzip |
| 12 | + |
| 13 | + |
| 14 | +class Backup: |
| 15 | + def __init__(self, application) -> None: |
| 16 | + self.app = application |
| 17 | + self.backup_config = config("backup") |
| 18 | + |
| 19 | + def accept(self, path): |
| 20 | + for pattern in self.backup_config.get("source").get("excludes"): |
| 21 | + if pattern in path: |
| 22 | + return False |
| 23 | + return True |
| 24 | + |
| 25 | + def database(self): |
| 26 | + """ |
| 27 | + Backup the database. |
| 28 | + """ |
| 29 | + |
| 30 | + db_config = config("database.databases") |
| 31 | + default = db_config.get("default") |
| 32 | + connection = db_config.get(default) |
| 33 | + driver = connection.get("driver") |
| 34 | + |
| 35 | + db_file_path = base_path( |
| 36 | + "{}.gz".format("database-" + str(datetime.timestamp(datetime.now()))) |
| 37 | + ) |
| 38 | + |
| 39 | + if driver == "sqlite": |
| 40 | + pass |
| 41 | + elif driver == "mysql": |
| 42 | + command_str = f"mysqldump -u {connection.get('user')} -p{connection.get('password')} {connection.get('database')}" |
| 43 | + |
| 44 | + elif driver == "postgres": |
| 45 | + command_str = f"pg_dump -U{connection.get('user')} -h{connection.get('host')} -p{connection.get('port')} -d{connection.get('database')}" |
| 46 | + |
| 47 | + elif driver == "mssql": |
| 48 | + command_str = f"sqlcmd -S{connection.get('host')} -U{connection.get('user')} -P{connection.get('port')} -d{connection.get('database')}" |
| 49 | + |
| 50 | + elif driver == "sqlserver": |
| 51 | + command_str = f"sqlcmd -S{connection.get('host')} -U{connection.get('user')} -P{connection.get('port')} -d{connection.get('database')}" |
| 52 | + |
| 53 | + elif driver == "oracle": |
| 54 | + command_str = f"sqlplus -S{connection.get('user')}/{connection.get('password')}@{connection.get('host')}:{connection.get('port')}/{connection.get('database')}" |
| 55 | + |
| 56 | + if command_str: |
| 57 | + with gzip.open(db_file_path, "wb") as f: |
| 58 | + popen = subprocess.Popen( |
| 59 | + [command_str], |
| 60 | + stdout=subprocess.PIPE, |
| 61 | + shell=True, |
| 62 | + universal_newlines=True, |
| 63 | + ) |
| 64 | + for stdout_line in iter(popen.stdout.readline, ""): |
| 65 | + f.write(stdout_line.encode("utf-8")) |
| 66 | + popen.stdout.close() |
| 67 | + popen.wait() |
| 68 | + return db_file_path |
| 69 | + |
| 70 | + def files(self): |
| 71 | + """ |
| 72 | + Backup the files. |
| 73 | + """ |
| 74 | + filename = ( |
| 75 | + self.backup_config.get("filename", "backup") |
| 76 | + + "-" |
| 77 | + + str(datetime.timestamp(datetime.now())) |
| 78 | + ) |
| 79 | + |
| 80 | + output_dir = base_path("storage/backup") |
| 81 | + |
| 82 | + if not pathlib.Path(output_dir).exists(): |
| 83 | + pathlib.Path(output_dir).mkdir(parents=True, exist_ok=True) |
| 84 | + |
| 85 | + path_to_archive = pathlib.Path(output_dir).joinpath(filename) |
| 86 | + |
| 87 | + with tempfile.TemporaryDirectory() as tmp: |
| 88 | + shutil.copytree( |
| 89 | + self.backup_config.get("source").get("root"), |
| 90 | + pathlib.Path(tmp).joinpath("backup"), |
| 91 | + ignore=shutil.ignore_patterns(*self.backup_config.get("source").get("excludes")), |
| 92 | + ) |
| 93 | + |
| 94 | + with patch("os.path.isfile", side_effect=self.accept): |
| 95 | + make_archive(path_to_archive, "zip", tmp) |
| 96 | + return path_to_archive |
0 commit comments