Major repo reorganizaion
This commit is contained in:
parent
f76c91e549
commit
fc6ecd951d
|
@ -1 +1,3 @@
|
||||||
.venv/
|
.venv/
|
||||||
|
.gitignore
|
||||||
|
Dockerfile
|
|
@ -3,7 +3,7 @@ import sys
|
||||||
from containers import RunningContainers
|
from containers import RunningContainers
|
||||||
import restic
|
import restic
|
||||||
|
|
||||||
cmds = ['volumes', 'backup', 'snapshots', 'check']
|
cmds = ['status', 'backup', 'snapshots', 'check']
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
|
@ -31,20 +31,22 @@ def main():
|
||||||
|
|
||||||
containers = RunningContainers()
|
containers = RunningContainers()
|
||||||
|
|
||||||
if mode == 'volumes':
|
if mode == 'status':
|
||||||
volumes = containers.volume_mounts()
|
containers.print_services()
|
||||||
for vol in volumes:
|
# volumes = containers.volume_mounts()
|
||||||
print(vol)
|
# for vol in volumes:
|
||||||
print(vol.mount_string())
|
# print(vol)
|
||||||
|
# print(vol.mount_string())
|
||||||
|
|
||||||
binds = containers.bind_mounts()
|
# binds = containers.bind_mounts()
|
||||||
for vol in binds:
|
# for vol in binds:
|
||||||
print(binds)
|
# print(binds)
|
||||||
print(vol.mount_string())
|
# print(vol.mount_string())
|
||||||
|
|
||||||
if mode == 'backup':
|
if mode == 'backup':
|
||||||
|
print("Starting backup ..")
|
||||||
# TODO: Errors when repo already exists
|
# TODO: Errors when repo already exists
|
||||||
restic.init_repo(Config.repository)
|
# restic.init_repo(Config.repository)
|
||||||
|
|
||||||
# for vol in containers.backup_volumes():
|
# for vol in containers.backup_volumes():
|
||||||
# restic.backup_volume(Config.repository, vol)
|
# restic.backup_volume(Config.repository, vol)
|
|
@ -1,6 +1,7 @@
|
||||||
import os
|
import os
|
||||||
import docker
|
import docker
|
||||||
import json
|
import json
|
||||||
|
import pprint
|
||||||
|
|
||||||
DOCKER_BASE_URL = os.environ.get('DOCKER_BASE_URL') or "unix://tmp/docker.sock"
|
DOCKER_BASE_URL = os.environ.get('DOCKER_BASE_URL') or "unix://tmp/docker.sock"
|
||||||
VOLUME_TYPE_BIND = "bind"
|
VOLUME_TYPE_BIND = "bind"
|
||||||
|
@ -125,54 +126,64 @@ class RunningContainers:
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
self.containers = []
|
self.containers = []
|
||||||
self.all_containers = []
|
self.this_container = None
|
||||||
self.backup_container = None
|
|
||||||
|
|
||||||
# Read all containers and find this container
|
# Find the container we are running in.
|
||||||
for entry in all_containers:
|
# If we don't have this information we cannot continue
|
||||||
container = Container(entry)
|
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']):
|
if not self.this_container:
|
||||||
self.backup_container = container
|
|
||||||
else:
|
|
||||||
self.all_containers.append(container)
|
|
||||||
|
|
||||||
if not self.backup_container:
|
|
||||||
raise ValueError("Cannot find metadata for backup container")
|
raise ValueError("Cannot find metadata for backup container")
|
||||||
|
|
||||||
for container in self.all_containers:
|
# Gather all containers in the current compose setup
|
||||||
# Weed out containers not beloging to this project.
|
for container_data in all_containers:
|
||||||
if container.project_name != self.backup_container.project_name:
|
# pprint.pprint(container_data, indent=2)
|
||||||
continue
|
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
|
# for container in self.all_containers:
|
||||||
if not container.backup_enabled:
|
# # Weed out containers not beloging to this project.
|
||||||
continue
|
# if container.project_name != self.backup_container.project_name:
|
||||||
|
# continue
|
||||||
|
|
||||||
# and not oneoffs (started manually with run or similar)
|
# # Keep only containers with backup enabled
|
||||||
if container.is_oneoff:
|
# if not container.backup_enabled:
|
||||||
continue
|
# 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):
|
# self.containers.append(container)
|
||||||
"""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 volume_mounts(self):
|
# def gen_volumes(self, volume_type):
|
||||||
"""Docker volumes"""
|
# """Generator yielding volumes of a specific type"""
|
||||||
return set(mnt for mnt in self.gen_volumes(VOLUME_TYPE_VOLUME))
|
# for cont in self.containers:
|
||||||
|
# for mnt in cont.mounts:
|
||||||
|
# if mnt.type == volume_type:
|
||||||
|
# yield mnt
|
||||||
|
|
||||||
def bind_mounts(self):
|
# def volume_mounts(self):
|
||||||
"""Host mapped volumes"""
|
# """Docker volumes"""
|
||||||
return set(mnt for mnt in self.gen_volumes(VOLUME_TYPE_BIND))
|
# return set(mnt for mnt in self.gen_volumes(VOLUME_TYPE_VOLUME))
|
||||||
|
|
||||||
def print_all(self):
|
# def bind_mounts(self):
|
||||||
print("Backup container:")
|
# """Host mapped volumes"""
|
||||||
print(json.dumps(self.backup_container.to_dict(), indent=2))
|
# return set(mnt for mnt in self.gen_volumes(VOLUME_TYPE_BIND))
|
||||||
|
|
||||||
print("All containers:")
|
def print_services(self):
|
||||||
print(json.dumps([cnt.to_dict() for cnt in self.containers], indent=2))
|
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()
|
|
@ -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',
|
||||||
|
],
|
||||||
|
)
|
Loading…
Reference in New Issue