Back up to /volumes and /databases

This commit is contained in:
Einar Forselv 2019-12-04 22:17:42 +01:00
parent 7f6b140a00
commit d3933f8913
6 changed files with 13 additions and 12 deletions

View File

@ -16,8 +16,8 @@ This includes both host mapped volumes and actual docker volumes.
* When backup starts a new instance of the container is created * When backup starts a new instance of the container is created
mapping in all the needed volumes. It will copy networks etc mapping in all the needed volumes. It will copy networks etc
to ensure databases can be reached to ensure databases can be reached
* Volumes are mounted to `/backup/<service_name>/<path>` * Volumes are mounted to `/volumes/<service_name>/<path>`
in the backup process container. `/backup` is pushed into restic in the backup process container. `/volumes` is pushed into restic
* Databases are backed up from stdin / dumps * Databases are backed up from stdin / dumps
* Cron triggers backup * Cron triggers backup
@ -117,7 +117,7 @@ volumes:
Will dump databases directly into restic through stdin. Will dump databases directly into restic through stdin.
They will appear in restic as a separate snapshot with They will appear in restic as a separate snapshot with
path `/backup/<service_name>/dump.sql` or similar. path `/databases/<service_name>/dump.sql` or similar.
```yaml ```yaml
mariadb: mariadb:

View File

@ -82,7 +82,7 @@ def backup(config, containers):
volumes = containers.this_container.volumes volumes = containers.this_container.volumes
# Map volumes from other containers we are backing up # Map volumes from other containers we are backing up
mounts = containers.generate_backup_mounts('/backup') mounts = containers.generate_backup_mounts('/volumes')
volumes.update(mounts) volumes.update(mounts)
result = backup_runner.run( result = backup_runner.run(
@ -123,7 +123,7 @@ def start_backup_process(config, containers):
# Back up volumes # Back up volumes
try: try:
logger.info('Backing up volumes') logger.info('Backing up volumes')
vol_result = restic.backup_files(config.repository, source='/backup') vol_result = restic.backup_files(config.repository, source='/volumes')
logger.debug('Volume backup exit code: %s', vol_result) logger.debug('Volume backup exit code: %s', vol_result)
if vol_result != 0: if vol_result != 0:
logger.error('Backup command exited with non-zero code: %s', vol_result) logger.error('Backup command exited with non-zero code: %s', vol_result)

View File

@ -6,7 +6,7 @@ logger = logging.getLogger(__name__)
def test(): def test():
return run_command(['ls', '/backup']) return run_command(['ls', '/volumes'])
def ping_mysql(host, port, username, password) -> int: def ping_mysql(host, port, username, password) -> int:

View File

@ -163,6 +163,7 @@ class Container:
if self._include: if self._include:
for mount in self._mounts: for mount in self._mounts:
for pattern in self._include: for pattern in self._include:
print(pattern, self._include)
if pattern in mount.source: if pattern in mount.source:
break break
else: else:
@ -182,7 +183,7 @@ class Container:
return filtered return filtered
def volumes_for_backup(self, source_prefix='/backup', mode='ro'): def volumes_for_backup(self, source_prefix='/volumes', mode='ro'):
"""Get volumes configured for backup""" """Get volumes configured for backup"""
mounts = self.filter_mounts() mounts = self.filter_mounts()
volumes = {} volumes = {}
@ -333,7 +334,7 @@ class RunningContainers:
"""Obtain all containers with backup enabled""" """Obtain all containers with backup enabled"""
return [container for container in self.containers if container.backup_enabled] return [container for container in self.containers if container.backup_enabled]
def generate_backup_mounts(self, dest_prefix='/backup') -> dict: def generate_backup_mounts(self, dest_prefix='/volumes') -> dict:
"""Generate mounts for backup for the entire compose setup""" """Generate mounts for backup for the entire compose setup"""
mounts = {} mounts = {}
for container in self.containers_for_backup(): for container in self.containers_for_backup():

View File

@ -45,7 +45,7 @@ class MariadbContainer(Container):
config = Config() config = Config()
return restic.backup_from_stdin( return restic.backup_from_stdin(
config.repository, config.repository,
f'/backup/{self.service_name}/all_databases.sql', f'/databases/{self.service_name}/all_databases.sql',
self.dump_command(), self.dump_command(),
) )
@ -88,7 +88,7 @@ class MysqlContainer(Container):
config = Config() config = Config()
return restic.backup_from_stdin( return restic.backup_from_stdin(
config.repository, config.repository,
f'/backup/{self.service_name}/all_databases.sql', f'/databases/{self.service_name}/all_databases.sql',
self.dump_command(), self.dump_command(),
) )
@ -135,6 +135,6 @@ class PostgresContainer(Container):
with utils.environment('PGPASSWORD', creds['password']): with utils.environment('PGPASSWORD', creds['password']):
return restic.backup_from_stdin( return restic.backup_from_stdin(
config.repository, config.repository,
f"/backup/{self.service_name}/{creds['database']}.sql", f"/databases/{self.service_name}/{creds['database']}.sql",
self.dump_command(), self.dump_command(),
) )

View File

@ -19,7 +19,7 @@ def init_repo(repository: str):
])) ]))
def backup_files(repository: str, source='/backup'): def backup_files(repository: str, source='/volumes'):
return commands.run(restic(repository, [ return commands.run(restic(repository, [
"--verbose", "--verbose",
"backup", "backup",