2020-05-26 14:30:59 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
2019-12-03 09:40:02 +01:00
|
|
|
from restic_compose_backup.containers import Container
|
2020-05-26 14:30:59 +02:00
|
|
|
from restic_compose_backup.config import config, Config
|
2019-12-03 09:40:02 +01:00
|
|
|
from restic_compose_backup import (
|
2019-12-03 04:23:06 +01:00
|
|
|
commands,
|
|
|
|
restic,
|
|
|
|
)
|
2019-12-03 09:40:02 +01:00
|
|
|
from restic_compose_backup import utils
|
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,
|
2021-03-08 16:41:23 +01:00
|
|
|
'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()
|
2019-12-05 00:38:58 +01:00
|
|
|
|
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"""
|
2019-12-03 04:23:06 +01:00
|
|
|
creds = self.get_credentials()
|
2021-03-08 10:43:34 +01:00
|
|
|
destination = self.backup_destination_path()
|
2021-03-09 07:05:37 +01:00
|
|
|
|
|
|
|
verbosity = 2
|
2021-03-09 11:37:18 +01:00
|
|
|
if self.get_config('log_level') == 'debug':
|
2021-03-09 07:05:37 +01:00
|
|
|
verbosity = 3
|
|
|
|
|
2019-12-03 04:23:06 +01:00
|
|
|
return [
|
2021-03-08 10:43:34 +01:00
|
|
|
"mydumper",
|
2021-03-08 14:43:29 +01:00
|
|
|
"--user",
|
|
|
|
creds['username'],
|
|
|
|
"--password",
|
|
|
|
creds['password'],
|
|
|
|
"--host",
|
|
|
|
creds['host'],
|
|
|
|
"--port",
|
|
|
|
creds['port'],
|
|
|
|
"--outputdir",
|
2021-03-08 14:50:48 +01:00
|
|
|
f"{destination}",
|
2021-03-08 10:43:34 +01:00
|
|
|
"--no-views",
|
|
|
|
"--compress",
|
2021-03-08 16:38:05 +01:00
|
|
|
"--regex",
|
2021-03-09 07:05:37 +01:00
|
|
|
"'^(?!(mysql\.))'",
|
|
|
|
"--verbose",
|
|
|
|
f"{verbosity}"
|
2019-12-03 04:23:06 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
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}"
|
|
|
|
])
|
2021-03-08 10:43:34 +01:00
|
|
|
commands.run(self.dump_command())
|
|
|
|
|
|
|
|
return restic.backup_files(
|
|
|
|
config.repository,
|
2021-03-08 14:58:27 +01:00
|
|
|
f"{destination}",
|
2021-03-08 10:43:34 +01:00
|
|
|
tags=self.tags
|
|
|
|
)
|
2019-12-03 01:29:41 +01:00
|
|
|
|
2020-05-26 10:42:29 +02:00
|
|
|
def backup_destination_path(self) -> str:
|
2020-05-26 14:30:59 +02:00
|
|
|
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
|
2020-05-26 10:42:29 +02:00
|
|
|
|
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,
|
2021-03-08 16:41:23 +01:00
|
|
|
'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()
|
2019-12-05 00:38:58 +01:00
|
|
|
|
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()
|
2021-03-08 10:43:34 +01:00
|
|
|
destination = self.backup_destination_path()
|
2021-03-09 07:05:37 +01:00
|
|
|
|
|
|
|
verbosity = 2
|
2021-03-09 11:37:18 +01:00
|
|
|
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 [
|
2021-03-08 10:43:34 +01:00
|
|
|
"mydumper",
|
2021-03-08 14:43:29 +01:00
|
|
|
"--user",
|
|
|
|
creds['username'],
|
|
|
|
"--password",
|
|
|
|
creds['password'],
|
|
|
|
"--host",
|
|
|
|
creds['host'],
|
|
|
|
"--port",
|
|
|
|
creds['port'],
|
|
|
|
"--outputdir",
|
2021-03-08 14:50:48 +01:00
|
|
|
f"{destination}",
|
2021-03-08 10:43:34 +01:00
|
|
|
"--no-views",
|
|
|
|
"--compress",
|
2021-03-08 14:52:36 +01:00
|
|
|
"--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
|
|
|
|
2019-12-03 04:23:06 +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}"
|
|
|
|
])
|
2021-03-08 10:43:34 +01:00
|
|
|
commands.run(self.dump_command())
|
|
|
|
|
|
|
|
return restic.backup_files(
|
|
|
|
config.repository,
|
2021-03-08 14:58:27 +01:00
|
|
|
f"{destination}",
|
2021-03-08 10:43:34 +01:00
|
|
|
tags=self.tags
|
|
|
|
)
|
2019-12-03 04:23:06 +01:00
|
|
|
|
2020-05-26 10:42:29 +02:00
|
|
|
def backup_destination_path(self) -> str:
|
2020-05-26 14:30:59 +02:00
|
|
|
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
|
2020-05-26 10:42:29 +02:00
|
|
|
|
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'],
|
|
|
|
]
|
2019-12-03 04:23:06 +01:00
|
|
|
|
|
|
|
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,
|
2020-05-26 10:42:29 +02:00
|
|
|
self.backup_destination_path(),
|
2019-12-03 06:24:05 +01:00
|
|
|
self.dump_command(),
|
2020-11-25 19:32:32 +01:00
|
|
|
tags=self.tags
|
2019-12-03 06:24:05 +01:00
|
|
|
)
|
2020-05-26 10:42:29 +02:00
|
|
|
|
|
|
|
def backup_destination_path(self) -> str:
|
2020-05-26 14:30:59 +02:00
|
|
|
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
|