restic-compose-backup/src/restic_compose_backup/containers_db.py

241 lines
6.5 KiB
Python
Raw Normal View History

2021-03-09 12:05:49 +01:00
import logging
from pathlib import Path
2019-12-03 09:40:02 +01:00
from restic_compose_backup.containers import Container
from restic_compose_backup.config import config, Config
2019-12-03 09:40:02 +01:00
from restic_compose_backup import (
commands,
restic,
)
2019-12-03 09:40:02 +01:00
from restic_compose_backup import utils
2019-12-03 01:29:41 +01:00
2021-03-09 12:05:49 +01:00
logger = logging.getLogger(__name__)
2019-12-03 01:29:41 +01:00
class MariadbContainer(Container):
container_type = 'mariadb'
def get_credentials(self) -> dict:
"""dict: get credentials for the service"""
return {
'host': self.hostname,
'username': 'root',
'password': self.get_config_env('MYSQL_ROOT_PASSWORD'),
2019-12-03 01:29:41 +01:00
'port': "3306",
}
def ping(self) -> bool:
"""Check the availability of the service"""
2019-12-03 01:47:15 +01:00
creds = self.get_credentials()
2021-03-08 19:29:42 +01:00
return commands.ping_mariadb(
creds['host'],
creds['port'],
creds['username'],
creds['password']
)
2019-12-03 01:29:41 +01:00
def dump_command(self) -> list:
"""list: create a dump command restic and use to send data through stdin"""
creds = self.get_credentials()
destination = self.backup_destination_path()
2021-03-09 07:05:37 +01:00
2021-03-09 11:58:34 +01:00
logger.debug('log_level: %s', self.get_config('log_level'))
verbosity = 3
if self.get_config('log_level') == 'debug':
2021-03-09 07:05:37 +01:00
verbosity = 3
return [
"mydumper",
"--user",
creds['username'],
"--password",
creds['password'],
"--host",
creds['host'],
"--port",
creds['port'],
"--outputdir",
2021-03-08 14:50:48 +01:00
f"{destination}",
"--no-views",
"--compress",
2021-03-08 16:38:05 +01:00
"--regex",
2021-03-09 07:05:37 +01:00
"'^(?!(mysql\.))'",
"--verbose",
f"{verbosity}"
]
def backup(self):
2019-12-03 05:00:47 +01:00
config = Config()
2021-03-08 14:57:01 +01:00
destination = self.backup_destination_path()
2019-12-05 00:27:56 +01:00
2021-03-08 14:57:01 +01:00
commands.run([
"mkdir",
"-p",
f"{destination}"
])
commands.run(self.dump_command())
return restic.backup_files(
config.repository,
2021-03-08 14:58:27 +01:00
f"{destination}",
tags=self.tags
)
2019-12-03 01:29:41 +01:00
def backup_destination_path(self) -> str:
destination = Path("/databases")
if utils.is_true(config.include_project_name):
project_name = self.project_name
if project_name != "":
destination /= project_name
destination /= self.service_name
return destination
2019-12-03 01:29:41 +01:00
class MysqlContainer(Container):
container_type = 'mysql'
def get_credentials(self) -> dict:
"""dict: get credentials for the service"""
return {
'host': self.hostname,
'username': 'root',
'password': self.get_config_env('MYSQL_ROOT_PASSWORD'),
2019-12-03 01:29:41 +01:00
'port': "3306",
}
def ping(self) -> bool:
"""Check the availability of the service"""
2019-12-03 01:47:15 +01:00
creds = self.get_credentials()
2021-03-08 19:29:42 +01:00
return commands.ping_mysql(
creds['host'],
creds['port'],
creds['username'],
creds['password']
)
2019-12-03 01:29:41 +01:00
def dump_command(self) -> list:
"""list: create a dump command restic and use to send data through stdin"""
2019-12-03 05:11:24 +01:00
creds = self.get_credentials()
destination = self.backup_destination_path()
2021-03-09 11:58:34 +01:00
logger.debug('log_level: %s', self.get_config('log_level'))
2021-03-09 07:05:37 +01:00
2021-03-09 11:58:34 +01:00
verbosity = 3
if self.get_config('log_level') == 'debug':
2021-03-09 07:05:37 +01:00
verbosity = 3
2019-12-03 05:11:24 +01:00
return [
"mydumper",
"--user",
creds['username'],
"--password",
creds['password'],
"--host",
creds['host'],
"--port",
creds['port'],
"--outputdir",
2021-03-08 14:50:48 +01:00
f"{destination}",
"--no-views",
"--compress",
"--regex",
2021-03-09 07:05:37 +01:00
"'^(?!(mysql\.))'",
"--verbose",
f"{verbosity}"
2019-12-03 05:11:24 +01:00
]
2019-12-03 01:29:41 +01:00
def backup(self):
2019-12-03 05:11:24 +01:00
config = Config()
2021-03-08 14:57:01 +01:00
destination = self.backup_destination_path()
2019-12-05 00:27:56 +01:00
2021-03-08 14:57:01 +01:00
commands.run([
"mkdir",
"-p",
f"{destination}"
])
commands.run(self.dump_command())
return restic.backup_files(
config.repository,
2021-03-08 14:58:27 +01:00
f"{destination}",
tags=self.tags
)
def backup_destination_path(self) -> str:
destination = Path("/databases")
if utils.is_true(config.include_project_name):
project_name = self.project_name
if project_name != "":
destination /= project_name
destination /= self.service_name
return destination
2019-12-03 01:29:41 +01:00
class PostgresContainer(Container):
container_type = 'postgres'
def get_credentials(self) -> dict:
"""dict: get credentials for the service"""
return {
'host': self.hostname,
2021-03-08 16:32:52 +01:00
'username': self.get_config_env('POSTGRES_USER'),
'password': self.get_config_env('POSTGRES_PASSWORD'),
2019-12-03 01:29:41 +01:00
'port': "5432",
2021-03-08 16:32:52 +01:00
'database': self.get_config_env('POSTGRES_DB'),
2019-12-03 01:29:41 +01:00
}
def ping(self) -> bool:
"""Check the availability of the service"""
2019-12-03 03:00:17 +01:00
creds = self.get_credentials()
return commands.ping_postgres(
creds['host'],
creds['port'],
creds['username'],
creds['password'],
)
2019-12-03 01:29:41 +01:00
def dump_command(self) -> list:
"""list: create a dump command restic and use to send data through stdin"""
2019-12-03 06:24:05 +01:00
# NOTE: Backs up a single database from POSTGRES_DB env var
creds = self.get_credentials()
return [
"pg_dump",
f"--host={creds['host']}",
f"--port={creds['port']}",
f"--username={creds['username']}",
creds['database'],
]
def backup(self):
2019-12-03 06:24:05 +01:00
config = Config()
creds = self.get_credentials()
with utils.environment('PGPASSWORD', creds['password']):
return restic.backup_from_stdin(
config.repository,
self.backup_destination_path(),
2019-12-03 06:24:05 +01:00
self.dump_command(),
tags=self.tags
2019-12-03 06:24:05 +01:00
)
def backup_destination_path(self) -> str:
destination = Path("/databases")
if utils.is_true(config.include_project_name):
project_name = self.project_name
if project_name != "":
destination /= project_name
destination /= self.service_name
destination /= f"{self.get_credentials()['database']}.sql"
return destination