diff --git a/.dockerignore b/.dockerignore index 21d0b89..706039a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1 +1,3 @@ .venv/ +.gitignore +Dockerfile \ No newline at end of file diff --git a/restic-backup/crontab b/crontab similarity index 100% rename from restic-backup/crontab rename to crontab diff --git a/restic-backup/entrypoint.sh b/entrypoint.sh similarity index 100% rename from restic-backup/entrypoint.sh rename to entrypoint.sh diff --git a/restic-backup/backup.py b/restic_volume_backup/backup.py similarity index 67% rename from restic-backup/backup.py rename to restic_volume_backup/backup.py index aab34fc..ad5a89c 100644 --- a/restic-backup/backup.py +++ b/restic_volume_backup/backup.py @@ -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) diff --git a/restic-backup/containers.py b/restic_volume_backup/containers.py similarity index 61% rename from restic-backup/containers.py rename to restic_volume_backup/containers.py index f5844c9..c9258d0 100644 --- a/restic-backup/containers.py +++ b/restic_volume_backup/containers.py @@ -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() \ No newline at end of file diff --git a/restic-backup/restic.py b/restic_volume_backup/restic.py similarity index 100% rename from restic-backup/restic.py rename to restic_volume_backup/restic.py diff --git a/restic-backup/runner.py b/restic_volume_backup/runner.py similarity index 100% rename from restic-backup/runner.py rename to restic_volume_backup/runner.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..9afe901 --- /dev/null +++ b/setup.py @@ -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', + ], +)