Major repo reorganizaion

This commit is contained in:
Einar Forselv 2019-04-15 16:16:06 +02:00
parent f76c91e549
commit fc6ecd951d
8 changed files with 78 additions and 50 deletions

View File

@ -1 +1,3 @@
.venv/
.gitignore
Dockerfile

View File

@ -3,7 +3,7 @@ import sys
from containers import RunningContainers
import restic
cmds = ['volumes', 'backup', 'snapshots', 'check']
cmds = ['status', 'backup', 'snapshots', 'check']
class Config:
@ -31,20 +31,22 @@ def main():
containers = RunningContainers()
if mode == 'volumes':
volumes = containers.volume_mounts()
for vol in volumes:
print(vol)
print(vol.mount_string())
if mode == 'status':
containers.print_services()
# volumes = containers.volume_mounts()
# for vol in volumes:
# print(vol)
# print(vol.mount_string())
binds = containers.bind_mounts()
for vol in binds:
print(binds)
print(vol.mount_string())
# binds = containers.bind_mounts()
# for vol in binds:
# print(binds)
# print(vol.mount_string())
if mode == 'backup':
print("Starting backup ..")
# TODO: Errors when repo already exists
restic.init_repo(Config.repository)
# restic.init_repo(Config.repository)
# for vol in containers.backup_volumes():
# restic.backup_volume(Config.repository, vol)

View File

@ -1,6 +1,7 @@
import os
import docker
import json
import pprint
DOCKER_BASE_URL = os.environ.get('DOCKER_BASE_URL') or "unix://tmp/docker.sock"
VOLUME_TYPE_BIND = "bind"
@ -125,54 +126,64 @@ class RunningContainers:
client.close()
self.containers = []
self.all_containers = []
self.backup_container = None
self.this_container = None
# Read all containers and find this container
for entry in all_containers:
container = Container(entry)
# Find the container we are running in.
# If we don't have this information we cannot continue
for container_data in all_containers:
if container_data.get('Id').startswith(os.environ['HOSTNAME']):
self.this_container = Container(container_data)
if container.id.startswith(os.environ['HOSTNAME']):
self.backup_container = container
else:
self.all_containers.append(container)
if not self.backup_container:
if not self.this_container:
raise ValueError("Cannot find metadata for backup container")
for container in self.all_containers:
# Weed out containers not beloging to this project.
if container.project_name != self.backup_container.project_name:
continue
# Gather all containers in the current compose setup
for container_data in all_containers:
# pprint.pprint(container_data, indent=2)
container = Container(container_data)
if container.project_name == self.this_container.project_name:
if container.id != self.this_container.id:
self.containers.append(container)
# Keep only containers with backup enabled
if not container.backup_enabled:
continue
# for container in self.all_containers:
# # Weed out containers not beloging to this project.
# if container.project_name != self.backup_container.project_name:
# continue
# and not oneoffs (started manually with run or similar)
if container.is_oneoff:
continue
# # Keep only containers with backup enabled
# if not container.backup_enabled:
# continue
self.containers.append(container)
# # and not oneoffs (started manually with run or similar)
# if container.is_oneoff:
# continue
def gen_volumes(self, volume_type):
"""Generator yielding volumes of a specific type"""
for cont in self.containers:
for mnt in cont.mounts:
if mnt.type == volume_type:
yield mnt
# self.containers.append(container)
def volume_mounts(self):
"""Docker volumes"""
return set(mnt for mnt in self.gen_volumes(VOLUME_TYPE_VOLUME))
# def gen_volumes(self, volume_type):
# """Generator yielding volumes of a specific type"""
# for cont in self.containers:
# for mnt in cont.mounts:
# if mnt.type == volume_type:
# yield mnt
def bind_mounts(self):
"""Host mapped volumes"""
return set(mnt for mnt in self.gen_volumes(VOLUME_TYPE_BIND))
# def volume_mounts(self):
# """Docker volumes"""
# return set(mnt for mnt in self.gen_volumes(VOLUME_TYPE_VOLUME))
def print_all(self):
print("Backup container:")
print(json.dumps(self.backup_container.to_dict(), indent=2))
# def bind_mounts(self):
# """Host mapped volumes"""
# return set(mnt for mnt in self.gen_volumes(VOLUME_TYPE_BIND))
print("All containers:")
print(json.dumps([cnt.to_dict() for cnt in self.containers], indent=2))
def print_services(self):
print()
print("Backup config for compose project '{}'".format(self.this_container.project_name))
print()
for container in self.containers:
print('service: {}'.format(container.service_name))
mounts = container.filter_mounts()
for mount in mounts:
print(' - {}'.format(mount.source))
print()

13
setup.py Normal file
View File

@ -0,0 +1,13 @@
from setuptools import setup, find_namespace_packages
setup(
name="restic-volume-backup",
url="https://github.com/ZettaIO/restic-volume-backup",
version="1.0.0",
author="Einar Forselv",
author_email="eforselv@gmail.com",
packages=find_namespace_packages(include=['restic_volume_backup']),
install_requires=[
'docker-py==1.10.6',
],
)