restic-compose-backup/restic_volume_backup/cli.py

103 lines
3.1 KiB
Python
Raw Normal View History

2019-04-15 16:31:08 +00:00
import argparse
import sys
2019-04-15 15:25:42 +00:00
from restic_volume_backup.config import Config
from restic_volume_backup.containers import RunningContainers
2019-04-17 01:45:51 +00:00
from restic_volume_backup import backup_runner
from restic_volume_backup import restic
2019-04-13 17:04:54 +00:00
def main():
2019-04-15 16:31:08 +00:00
args = parse_args()
config = Config()
2019-04-13 17:04:54 +00:00
containers = RunningContainers()
2019-04-15 16:31:08 +00:00
if args.action == 'status':
2019-04-18 03:01:02 +00:00
status(config, containers)
2019-04-16 22:08:24 +00:00
2019-04-18 03:01:02 +00:00
elif args.action == 'backup':
backup(config, containers)
2019-04-16 22:08:24 +00:00
2019-04-18 03:01:02 +00:00
# Separate command to avoid spawning infinite containers :)
elif args.action == 'start-backup-process':
start_backup_process(config, containers)
2019-04-13 17:04:54 +00:00
2019-04-17 02:38:15 +00:00
2019-04-18 03:01:02 +00:00
def status(config, containers):
"""Outputs the backup config for the compse 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)
2019-04-18 03:01:02 +00:00
print()
backup_containers = containers.containers_for_backup()
for container in backup_containers:
if container.backup_enabled:
print('service: {}'.format(container.service_name))
for mount in container.filter_mounts():
print(' - {}'.format(mount.source))
if len(backup_containers) == 0:
print("No containers in the project has 'restic-volume-backup.enabled' label")
2019-04-18 03:01:02 +00:00
print()
def backup(config, containers):
"""Start backup"""
# Make sure we don't spawn multiple backup processes
if containers.backup_process_running:
raise ValueError("Backup process already running")
print("Initializing repository")
# TODO: Errors when repo already exists
restic.init_repo(config.repository)
print("Starting backup container..")
backup_runner.run(
image=containers.this_container.image,
command='restic-volume-backup start-backup-process',
volumes=containers.this_container.volumes,
enviroment=containers.this_container.environment,
labels={
"restic-volume-backup.backup_process": 'True',
"com.docker.compose.project": containers.this_container.project_name,
},
2019-04-18 03:01:02 +00:00
)
2019-04-13 23:35:14 +00:00
def start_backup_process(config, containers):
"""Start the backup process container"""
if not containers.backup_process_container or containers.this_container == containers.backup_process_container is False:
print(
"Cannot run backup process in this container. Use backup command instead. "
"This will spawn a new container with the necessary mounts."
)
return
print("start-backup-process")
status(config, containers)
2019-04-18 05:22:07 +00:00
import time
for i in range(5):
time.sleep(1)
print(i)
2019-04-18 05:22:07 +00:00
exit(1)
2019-04-15 16:31:08 +00:00
def parse_args():
parser = argparse.ArgumentParser(prog='restic_volume_backup')
parser.add_argument(
'action',
choices=['status', 'backup', 'start-backup-process'],
2019-04-15 16:31:08 +00:00
)
return parser.parse_args()
2019-04-13 17:04:54 +00:00
if __name__ == '__main__':
main()