mirror of
https://github.com/ZettaIO/restic-compose-backup.git
synced 2025-09-29 07:05:24 +00:00
Cleanup
This commit is contained in:
@@ -1,13 +1,28 @@
|
||||
import argparse
|
||||
import pprint
|
||||
import logging
|
||||
|
||||
from restic_volume_backup.config import Config
|
||||
from restic_volume_backup.containers import RunningContainers
|
||||
from restic_volume_backup import backup_runner
|
||||
from restic_volume_backup import restic
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_logger(level=logging.INFO):
|
||||
logger.setLevel(level)
|
||||
ch = logging.StreamHandler()
|
||||
ch.setLevel(logging.DEBUG)
|
||||
ch.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
|
||||
logger.addHandler(ch)
|
||||
|
||||
|
||||
setup_logger()
|
||||
|
||||
|
||||
def main():
|
||||
"""CLI entrypoint"""
|
||||
args = parse_args()
|
||||
config = Config()
|
||||
containers = RunningContainers()
|
||||
@@ -18,50 +33,45 @@ def main():
|
||||
elif args.action == 'backup':
|
||||
backup(config, containers)
|
||||
|
||||
# Separate command to avoid spawning infinite containers :)
|
||||
elif args.action == 'start-backup-process':
|
||||
start_backup_process(config, containers)
|
||||
|
||||
|
||||
def status(config, containers):
|
||||
"""Outputs the backup config for the compose setup"""
|
||||
print()
|
||||
print("Backup config for compose project '{}'".format(containers.this_container.project_name))
|
||||
print("Current service:", containers.this_container.name)
|
||||
print("Backup process :", containers.backup_process_container.name
|
||||
if containers.backup_process_container else 'Not Running')
|
||||
print("Backup running :", containers.backup_process_running)
|
||||
|
||||
print()
|
||||
logger.info("Backup config for compose project '%s'", containers.this_container.project_name)
|
||||
logger.info("Current service: %s", containers.this_container.name)
|
||||
logger.info("Backup process: %s", containers.backup_process_container.name
|
||||
if containers.backup_process_container else 'Not Running')
|
||||
logger.info("Backup running: %s", containers.backup_process_running)
|
||||
|
||||
backup_containers = containers.containers_for_backup()
|
||||
for container in backup_containers:
|
||||
if container.backup_enabled:
|
||||
print('service: {}'.format(container.service_name))
|
||||
logger.info('service: %s', container.service_name)
|
||||
for mount in container.filter_mounts():
|
||||
print(' - {}'.format(mount.source))
|
||||
logger.info(' - %s', mount.source)
|
||||
|
||||
if len(backup_containers) == 0:
|
||||
print("No containers in the project has 'restic-volume-backup.enabled' label")
|
||||
|
||||
print()
|
||||
logger.info("No containers in the project has 'restic-volume-backup.enabled' label")
|
||||
|
||||
|
||||
def backup(config, containers):
|
||||
"""Start backup"""
|
||||
"""Request a backup to start"""
|
||||
# Make sure we don't spawn multiple backup processes
|
||||
if containers.backup_process_running:
|
||||
raise ValueError("Backup process already running")
|
||||
|
||||
print("Initializing repository")
|
||||
logger.info("Initializing repository")
|
||||
|
||||
# TODO: Errors when repo already exists
|
||||
restic.init_repo(config.repository)
|
||||
|
||||
print("Starting backup container..")
|
||||
logger.info("Starting backup container..")
|
||||
|
||||
# Map all volumes from the backup container into the backup process container
|
||||
volumes = containers.this_container.volumes()
|
||||
volumes = containers.this_container.volumes
|
||||
|
||||
# Map volumes from other containers we are backing up
|
||||
mounts = containers.generate_backup_mounts('/backup')
|
||||
@@ -90,14 +100,17 @@ def start_backup_process(config, containers):
|
||||
)
|
||||
return
|
||||
|
||||
print("start-backup-process")
|
||||
logger.info("start-backup-process")
|
||||
status(config, containers)
|
||||
|
||||
# Waste a few seconds faking a backup
|
||||
print("Fake backup running")
|
||||
import time
|
||||
for i in range(5):
|
||||
time.sleep(1)
|
||||
print(i)
|
||||
|
||||
exit(1)
|
||||
exit(0)
|
||||
|
||||
|
||||
def parse_args():
|
||||
|
Reference in New Issue
Block a user