From 64908c8e2f0710a704070196c362c5ad0039d99d Mon Sep 17 00:00:00 2001
From: Einar Forselv <eforselv@gmail.com>
Date: Fri, 19 Apr 2019 02:16:00 +0200
Subject: [PATCH] Generate volumes for containers with volumes

---
 restic_volume_backup/containers.py | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/restic_volume_backup/containers.py b/restic_volume_backup/containers.py
index 13319ad..20c70c6 100644
--- a/restic_volume_backup/containers.py
+++ b/restic_volume_backup/containers.py
@@ -43,15 +43,18 @@ class Container:
         """All configured env vars for the container"""
         return self.get_config('Env', default=[])
 
-    @property
-    def volumes(self, mode='rw'):
+    property
+    def volumes(self):
         """
         Return volumes for the container in the following format:
             {'/home/user1/': {'bind': '/mnt/vol2', 'mode': 'rw'},}
         """
         volumes = {}
         for mount in self._mounts:
-            volumes[mount.source] = {'bind': mount.destination, 'mode': mode}
+            volumes[mount.source] = {
+                'bind': mount.destination,
+                'mode': 'rw',
+            }
 
         return volumes
 
@@ -118,6 +121,17 @@ class Container:
 
         return filtered
 
+    def volumes_for_backup(self, source_prefix='/backup', mode='ro'):
+        mounts = self.filter_mounts()
+        volumes = {}
+        for mount in mounts:
+            volumes[mount.source] = {
+                'bind': '{}{}'.format(source_prefix, mount.source),
+                'mode': mode,
+            }
+
+        return volumes
+
     def _parse_pattern(self, value):
         if not value:
             return None
@@ -227,6 +241,13 @@ class RunningContainers:
         """Obtain all containers with backup enabled"""
         return [container for container in self.containers if container.backup_enabled]
 
+    def generate_backup_mounts(self, dest_prefix):
+        mounts = {}
+        for container in self.containers_for_backup():
+            mounts.update(container.volumes_for_backup(source_prefix=dest_prefix, mode='ro'))
+
+        return mounts
+
     def get_service(self, name) -> Container:
         for container in self.containers:
             if container.service_name == name: