From 0fc620bb1fab989a84ec4d69b1f31f014d0023e5 Mon Sep 17 00:00:00 2001
From: Jannik <jannikw@users.noreply.github.com>
Date: Tue, 26 May 2020 15:30:54 +0200
Subject: [PATCH] Allow excluding all bind mounts from backups

---
 restic_compose_backup.env               |  1 +
 src/restic_compose_backup/cli.py        |  1 +
 src/restic_compose_backup/config.py     |  1 +
 src/restic_compose_backup/containers.py | 10 +++++++---
 4 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/restic_compose_backup.env b/restic_compose_backup.env
index 728bed5..8f07544 100644
--- a/restic_compose_backup.env
+++ b/restic_compose_backup.env
@@ -6,6 +6,7 @@
 
 SWARM_MODE=true
 INCLUDE_PROJECT_NAME=false
+EXCLUDE_BIND_MOUNTS=false
 
 RESTIC_REPOSITORY=/restic_data
 RESTIC_PASSWORD=password
diff --git a/src/restic_compose_backup/cli.py b/src/restic_compose_backup/cli.py
index 661603f..c558c2e 100644
--- a/src/restic_compose_backup/cli.py
+++ b/src/restic_compose_backup/cli.py
@@ -67,6 +67,7 @@ def status(config, containers):
     logger.info("Repository: '%s'", config.repository)
     logger.info("Backup currently running?: %s", containers.backup_process_running)
     logger.info("Include project name in backup path?: %s", utils.is_true(config.include_project_name))
+    logger.debug("Exclude bind mounts from backups?: %s", utils.is_true(config.exclude_bind_mounts))
     logger.info("Checking docker availability")
 
     utils.list_containers()
diff --git a/src/restic_compose_backup/config.py b/src/restic_compose_backup/config.py
index 264bfcc..8326a9b 100644
--- a/src/restic_compose_backup/config.py
+++ b/src/restic_compose_backup/config.py
@@ -14,6 +14,7 @@ class Config:
         self.cron_command = os.environ.get('CRON_COMMAND') or self.default_backup_command
         self.swarm_mode = os.environ.get('SWARM_MODE') or False
         self.include_project_name = os.environ.get('INCLUDE_PROJECT_NAME') or False
+        self.exclude_bind_mounts = os.environ.get('EXCLUDE_BIND_MOUNTS') or False
 
         # Log
         self.log_level = os.environ.get('LOG_LEVEL')
diff --git a/src/restic_compose_backup/containers.py b/src/restic_compose_backup/containers.py
index 863677d..acf12db 100644
--- a/src/restic_compose_backup/containers.py
+++ b/src/restic_compose_backup/containers.py
@@ -193,11 +193,15 @@ class Container:
         """Get all mounts for this container matching include/exclude filters"""
         filtered = []
 
+        # If exclude_bind_mounts is true, only volume mounts are kept in the list of mounts
+        exclude_bind_mounts = utils.is_true(config.exclude_bind_mounts)
+        mounts = list(filter(lambda m: not exclude_bind_mounts or m.type == "volume", self._mounts))
+
         if not self.volume_backup_enabled:
             return filtered
 
         if self._include:
-            for mount in self._mounts:
+            for mount in mounts:
                 for pattern in self._include:
                     if pattern in mount.source:
                         break
@@ -207,14 +211,14 @@ class Container:
                 filtered.append(mount)
 
         elif self._exclude:
-            for mount in self._mounts:
+            for mount in mounts:
                 for pattern in self._exclude:
                     if pattern in mount.source:
                         break
                 else:
                     filtered.append(mount)
         else:
-            return self._mounts
+            return mounts
 
         return filtered