Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/backup #198

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions derex/runner/cli/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,27 @@ def reset_mongodb_password_cmd(current_password: Optional[str], force: bool):

reset_mongodb_password(current_password)
return 0


@mongodb.command(name="dump")
@click.pass_obj
@click.argument("db_name", type=str)
def dump_database_cmd(project: Optional[Project], db_name: str):
"""Dump a mongodb database"""

from derex.runner.mongodb import dump_database

dump_database(db_name)
return 0


@mongodb.command(name="restore")
@click.argument("db_name", type=str, nargs=1)
@click.argument("dump_file", type=click.Path(exists=True), nargs=1)
def restore_database_cmd(db_name: str, dump_file: str):
"""Restore a mysql database from a file"""

from derex.runner.mongodb import restore_database

restore_database(db_name, dump_file)
return 0
24 changes: 24 additions & 0 deletions derex/runner/cli/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,27 @@ def reset_mysql_password_cmd(current_password: str, force: bool):

reset_mysql_password(current_password)
return 0


@mysql.command(name="dump")
@click.pass_obj
@click.argument("db_name", type=str)
def dump_database_cmd(project: Optional[Project], db_name: str):
"""Dump a mysql database"""

from derex.runner.mysql import dump_database

dump_database(db_name)
return 0


@mysql.command(name="restore")
@click.argument("db_name", type=str, nargs=1)
@click.argument("dump_file", type=click.Path(exists=True), nargs=1)
def restore_database_cmd(db_name: str, dump_file: str):
"""Restore a mysql database from a file"""

from derex.runner.mysql import restore_database

restore_database(db_name, dump_file)
return 0
27 changes: 27 additions & 0 deletions derex/runner/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Optional

import logging
import os
import urllib.parse


Expand Down Expand Up @@ -135,3 +136,29 @@ def reset_mongodb_password(current_password: str = None):

run_ddc_services(compose_args, exit_afterwards=True)
return 0


@ensure_mongodb
def dump_database(database_name: str):
"""Export the database"""
logger.info(f'Dumping the database "{database_name}"...')
os.system(
f"docker exec -i mongodb mongodump --authenticationDatabase=admin -u {MONGODB_ROOT_USER} -p{MONGODB_ROOT_PASSWORD} -d {database_name} --gzip --archive={database_name}.gz && docker cp mongodb:{database_name}.gz . && docker exec mongodb rm {database_name}.gz"
)
logger.info(
f"The database {database_name} was successfully dumped on {database_name}"
)


@ensure_mongodb
def restore_database(database_name: str, dump_file: str):
"""Resore a database from a dump file"""
logger.info(
f'Restoring the database "{database_name}" from the the directory {dump_file}'
)
os.system(
f"docker cp {dump_file} mongodb:/ && docker exec mongodb mongorestore --authenticationDatabase=admin -u {MONGODB_ROOT_USER} -p{MONGODB_ROOT_PASSWORD} --db {database_name} --drop --gzip --archive={dump_file} && docker exec mongodb rm {dump_file}"
)
logger.info(
f"The database {database_name} was successfully retored from {dump_file}"
)
29 changes: 29 additions & 0 deletions derex/runner/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import Tuple

import logging
import os
import pymysql


Expand Down Expand Up @@ -187,6 +188,34 @@ def copy_database(source_db_name: str, destination_db_name: str):
)


def dump_database(database_name: str):
""" "Export the database"""
logger.info(f'Dumping the database "{database_name}"...')
# run_ddc_services(
# [
# "exec",
# "-T",
# "mysql",
# f"mysqldump -u root -p{MYSQL_ROOT_PASSWORD} --databases {database_name} > dump.sql"
# ]
# )
os.system(
f"docker exec mysql mysqldump -u {MYSQL_ROOT_USER} -p{MYSQL_ROOT_PASSWORD} {database_name} > {database_name}.sql"
)
logger.info(f"The database {database_name} was successfully dumped")


def restore_database(database_name: str, dump_file: str):
"""Resore a database from a dump file"""
logger.info(f'Restoring the database "{database_name}" from {dump_file}')
os.system(
f"docker exec mysql mysqldump -u {MYSQL_ROOT_USER} -p{MYSQL_ROOT_PASSWORD} {database_name} < {dump_file}"
)
logger.info(
f"The database {database_name} was successfully retored from {dump_file}"
)


@ensure_mysql
def reset_mysql_openedx(project: Project, dry_run: bool = False):
"""Run script from derex/openedx image to reset the mysql db."""
Expand Down