From 1b9f7f22f117b2abe2c70bc48ace389bf712cb36 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Radoslav=20Bod=C3=B3?= <bodik@cesnet.cz>
Date: Fri, 19 Apr 2024 10:11:07 +0200
Subject: [PATCH] rwm: add autotags (add backup config name as tag for its
 backup snapshot), add backup_extras (global options for restic backup)

---
 examples/rwm-backups.conf |  7 ++++---
 rwm.py                    | 35 ++++++++++++++++++++++++++++-------
 tests/test_rwm.py         |  2 ++
 3 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/examples/rwm-backups.conf b/examples/rwm-backups.conf
index 8452345..07464d2 100644
--- a/examples/rwm-backups.conf
+++ b/examples/rwm-backups.conf
@@ -18,8 +18,6 @@ backups:
       - "/var/lib/mysql/*"
       - "/var/lib/postgresql/*"
       - "/var/run/*"
-    tags:
-      - "simplefs"
     extras:
       - "--one-file-system"
 
@@ -27,7 +25,7 @@ backups:
     filesdirs:
       - /var/lib/rwm/mysql.tar.gz
     tags:
-      - "mysql"
+      - "database"
     prerun:
       - "/opt/rwm/scripts/backup_mysql.py create"
     postrun:
@@ -37,3 +35,6 @@ retention:
   keep-daily: "60"
   keep-within: "60d"
   keep-tag: "donotforget"
+
+autotags: true
+backup_extras: ["--pack-size", "64"]
diff --git a/rwm.py b/rwm.py
index d08c851..028b4a6 100755
--- a/rwm.py
+++ b/rwm.py
@@ -133,10 +133,20 @@ class RWMConfig(BaseModel):
 
         retention:
             Dictionary containing retention policies for Restic repository.
-            Keys and values corresponds to a `restic forget` command `--keep*` options without leading dashes.
+            Keys and values corresponds to a `restic forget` command `--keep*`
+            options without leading dashes.
 
         lock_path:
-            Path for parallel execution exclusion lock. Defaults to `/var/run/rwm.lock`.
+            Path for parallel execution exclusion lock. Defaults to
+            `/var/run/rwm.lock`.
+
+        autotags:
+            Automatically add a tag to each backup snapshot, using the value
+            of the backup configuration name.
+
+        backup_extras:
+            Additional options for any the `restic backup` commmand (eg. default `pack-size` setting).
+            Defaults to an empty list.
     """
 
     model_config = ConfigDict(extra='forbid')
@@ -149,6 +159,8 @@ class RWMConfig(BaseModel):
     backups: Dict[str, BackupConfig] = {}
     retention: Dict[str, str] = {}
     lock_path: str = "/var/run/rwm.lock"
+    autotags: bool = False
+    backup_extras: List[str] = []
 
 
 class RwmJSONEncoder(json.JSONEncoder):
@@ -592,15 +604,24 @@ class RWM:
         logger.info(f"_restic_backup {name}")
         conf = self.config.backups[name]
 
-        excludes = []
-        for item in conf.excludes:
-            excludes += ["--exclude", item]
-
         tags = []
+        if self.config.autotags:
+            tags += ["--tag", name]
         for item in conf.tags:
             tags += ["--tag", item]
 
-        cmd_args = ["backup"] + conf.extras + tags + excludes + conf.filesdirs
+        excludes = []
+        for item in conf.excludes:
+            excludes += ["--exclude", item]
+
+        cmd_args = [
+            "backup",
+            *self.config.backup_extras,
+            *conf.extras,
+            *tags,
+            *excludes,
+            *conf.filesdirs
+        ]
 
         wrap_output(backup_proc := self.restic_cmd(cmd_args))
         return backup_proc.returncode
diff --git a/tests/test_rwm.py b/tests/test_rwm.py
index 3a89a5b..d7c1829 100644
--- a/tests/test_rwm.py
+++ b/tests/test_rwm.py
@@ -136,6 +136,8 @@ def test_backup(tmpworkdir: str, motoserver: str):  # pylint: disable=unused-arg
             "keep-daily": "1"
         },
         "lock_path": f"{tmpworkdir}/rwm.lock",
+        "autotags": True,
+        "backup_extras": ["--quiet", "false"]  # dummy here
     })
 
     Path("testdatadir").mkdir()
-- 
GitLab