mirror of
https://github.com/ZettaIO/restic-compose-backup.git
synced 2025-09-28 06:35:24 +00:00
Rename project
This commit is contained in:
140
restic_compose_backup/containers_db.py
Normal file
140
restic_compose_backup/containers_db.py
Normal file
@@ -0,0 +1,140 @@
|
||||
from restic_compose_backup.containers import Container
|
||||
from restic_compose_backup.config import Config
|
||||
from restic_compose_backup import (
|
||||
commands,
|
||||
restic,
|
||||
)
|
||||
from restic_compose_backup import utils
|
||||
|
||||
|
||||
class MariadbContainer(Container):
|
||||
container_type = 'mariadb'
|
||||
|
||||
def get_credentials(self) -> dict:
|
||||
"""dict: get credentials for the service"""
|
||||
return {
|
||||
'host': self.hostname,
|
||||
'username': self.get_config_env('MYSQL_USER'),
|
||||
'password': self.get_config_env('MYSQL_PASSWORD'),
|
||||
'port': "3306",
|
||||
}
|
||||
|
||||
def ping(self) -> bool:
|
||||
"""Check the availability of the service"""
|
||||
creds = self.get_credentials()
|
||||
return commands.ping_mysql(
|
||||
creds['host'],
|
||||
creds['port'],
|
||||
creds['username'],
|
||||
creds['password'],
|
||||
)
|
||||
|
||||
def dump_command(self) -> list:
|
||||
"""list: create a dump command restic and use to send data through stdin"""
|
||||
creds = self.get_credentials()
|
||||
return [
|
||||
"mysqldump",
|
||||
f"--host={creds['host']}",
|
||||
f"--port={creds['port']}",
|
||||
f"--user={creds['username']}",
|
||||
f"--password={creds['password']}",
|
||||
"--all-databases",
|
||||
]
|
||||
|
||||
def backup(self):
|
||||
config = Config()
|
||||
return restic.backup_from_stdin(
|
||||
config.repository,
|
||||
f'/backup/{self.service_name}/all_databases.sql',
|
||||
self.dump_command(),
|
||||
)
|
||||
|
||||
|
||||
class MysqlContainer(Container):
|
||||
container_type = 'mysql'
|
||||
|
||||
def get_credentials(self) -> dict:
|
||||
"""dict: get credentials for the service"""
|
||||
return {
|
||||
'host': self.hostname,
|
||||
'username': self.get_config_env('MYSQL_USER'),
|
||||
'password': self.get_config_env('MYSQL_PASSWORD'),
|
||||
'port': "3306",
|
||||
}
|
||||
|
||||
def ping(self) -> bool:
|
||||
"""Check the availability of the service"""
|
||||
creds = self.get_credentials()
|
||||
return commands.ping_mysql(
|
||||
creds['host'],
|
||||
creds['port'],
|
||||
creds['username'],
|
||||
creds['password'],
|
||||
)
|
||||
|
||||
def dump_command(self) -> list:
|
||||
"""list: create a dump command restic and use to send data through stdin"""
|
||||
creds = self.get_credentials()
|
||||
return [
|
||||
"mysqldump",
|
||||
f"--host={creds['host']}",
|
||||
f"--port={creds['port']}",
|
||||
f"--user={creds['username']}",
|
||||
f"--password={creds['password']}",
|
||||
"--all-databases",
|
||||
]
|
||||
|
||||
def backup(self):
|
||||
config = Config()
|
||||
return restic.backup_from_stdin(
|
||||
config.repository,
|
||||
f'/backup/{self.service_name}/all_databases.sql',
|
||||
self.dump_command(),
|
||||
)
|
||||
|
||||
|
||||
class PostgresContainer(Container):
|
||||
container_type = 'postgres'
|
||||
|
||||
def get_credentials(self) -> dict:
|
||||
"""dict: get credentials for the service"""
|
||||
return {
|
||||
'host': self.hostname,
|
||||
'username': self.get_config_env('POSTGRES_USER'),
|
||||
'password': self.get_config_env('POSTGRES_PASSWORD'),
|
||||
'port': "5432",
|
||||
'database': self.get_config_env('POSTGRES_DB'),
|
||||
}
|
||||
|
||||
def ping(self) -> bool:
|
||||
"""Check the availability of the service"""
|
||||
creds = self.get_credentials()
|
||||
return commands.ping_postgres(
|
||||
creds['host'],
|
||||
creds['port'],
|
||||
creds['username'],
|
||||
creds['password'],
|
||||
)
|
||||
|
||||
def dump_command(self) -> list:
|
||||
"""list: create a dump command restic and use to send data through stdin"""
|
||||
# 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):
|
||||
config = Config()
|
||||
creds = self.get_credentials()
|
||||
|
||||
with utils.environment('PGPASSWORD', creds['password']):
|
||||
return restic.backup_from_stdin(
|
||||
config.repository,
|
||||
f"/backup/{self.service_name}/{creds['database']}.sql",
|
||||
self.dump_command(),
|
||||
)
|
Reference in New Issue
Block a user