Skip to content

Commit

Permalink
godoo backup dump to Dump DB and Filestore of local instance into…
Browse files Browse the repository at this point in the history
… Folder
  • Loading branch information
joshkreud committed Dec 5, 2023
1 parent 1c33d31 commit 00d74a1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/godoo_cli/commands/backup/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import typer

from .dump import dump_instance
from .load import load_instance_data
from .pull import InstancePuller

Expand All @@ -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
78 changes: 78 additions & 0 deletions src/godoo_cli/commands/backup/dump.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 00d74a1

Please sign in to comment.