diff --git a/src/godoo_cli/commands/backup/cli.py b/src/godoo_cli/commands/backup/cli.py index ef6cae7..819e3a0 100644 --- a/src/godoo_cli/commands/backup/cli.py +++ b/src/godoo_cli/commands/backup/cli.py @@ -1,5 +1,6 @@ import typer +from .dump import dump_instance from .load import load_instance_data from .pull import InstancePuller @@ -13,5 +14,6 @@ def backup_cli_app(): app.command("pull")(puller.pull_instance_data) app.command("load")(load_instance_data) + app.command("dump")(dump_instance) return app diff --git a/src/godoo_cli/commands/backup/dump.py b/src/godoo_cli/commands/backup/dump.py new file mode 100644 index 0000000..756259a --- /dev/null +++ b/src/godoo_cli/commands/backup/dump.py @@ -0,0 +1,78 @@ +import logging +import shutil +from configparser import ConfigParser +from datetime import datetime +from pathlib import Path + +import typer + +from ...cli_common import CommonCLI +from ..db.connection import DBConnection + +LOGGER = logging.getLogger(__name__) +CLI = CommonCLI() + + +@CLI.arg_annotator +def dump_instance( + dump_path: Path = typer.Argument( + ..., + help="Path to dump to", + file_okay=False, + dir_okay=True, + writable=True, + resolve_path=True, + ), + db_name=CLI.database.db_name, + db_host=CLI.database.db_host, + db_port=CLI.database.db_port, + db_user=CLI.database.db_user, + db_password=CLI.database.db_password, + conf_path=CLI.odoo_paths.conf_path, +): + """Dump DB and Filestore into Folder""" + db_connection = DBConnection( + db_name=db_name, + hostname=db_host, + username=db_user, + password=db_password, + port=db_port, + ) + dump_path.mkdir(parents=True, exist_ok=True) + + if not conf_path: + raise typer.Exit("No Odoo Conf Path provided. Cannot dump Filestore") + + # Read .conf value [options] datad_dir + conf_path = Path(conf_path) + parser = ConfigParser() + parser.read(conf_path) + data_dir = parser["options"]["data_dir"] + data_dir = Path(data_dir) + + # Copy Filestore + LOGGER.info("Dumping Filestore") + filestore_target = dump_path / "odoo_filestore" + shutil.rmtree(filestore_target, ignore_errors=True) + shutil.copytree(data_dir, filestore_target) + + # Dump DB using pg_dump + LOGGER.info("Dumping DB") + db_dump_target = dump_path / "odoo.dump" + db_dump_target.unlink(missing_ok=True) + db_connection.run_psql_shell_command("pg_dump --format c {} > %s" % db_dump_target) + + readme_path = Path(dump_path) / "README.md" + readme_path.unlink(missing_ok=True) + readme_content = f""" +# gOdoo Dump + +SQL Dump and Filestore of gOdoo Instance. + +## Metadata + +- Filestore: [{ datetime.fromtimestamp(filestore_target.stat().st_mtime)}]({filestore_target.relative_to(dump_path)}) +- SQL Dump: [{ datetime.fromtimestamp(db_dump_target.stat().st_mtime)}]({db_dump_target.relative_to(dump_path)}) + +""" + readme_path.write_text(readme_content)