From 268080569a5cfbe0c38eebc52b396277693fe381 Mon Sep 17 00:00:00 2001
From: Evangelos Foutras <evangelos@foutras.com>
Date: Sat, 30 Mar 2024 18:25:34 +0200
Subject: [PATCH] borg_client: stop ignoring errors during db dumps

Due to the "systemctl is-active foo && backup-foo || true" shorthand,
errors during database dumping were being ignored. Change the MariaDB
section to also be wrapped in a proper if statement. Finally, get rid
of "|| true" silencing statements + enable errexit in helper scripts.
---
 roles/borg_client/templates/backup-mysql.sh.j2    | 2 ++
 roles/borg_client/templates/backup-postgres.sh.j2 | 3 ++-
 roles/borg_client/templates/borg-backup.sh.j2     | 8 +++++---
 3 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/roles/borg_client/templates/backup-mysql.sh.j2 b/roles/borg_client/templates/backup-mysql.sh.j2
index 9aee38dc7..84cd957d8 100644
--- a/roles/borg_client/templates/backup-mysql.sh.j2
+++ b/roles/borg_client/templates/backup-mysql.sh.j2
@@ -1,5 +1,7 @@
 #!/bin/bash
 
+set -e
+
 mysql_opts="--defaults-file={{mysql_backup_defaults}}"
 backupdir="{{mysql_backup_dir}}"
 
diff --git a/roles/borg_client/templates/backup-postgres.sh.j2 b/roles/borg_client/templates/backup-postgres.sh.j2
index ccb34fa99..a87e2ad2d 100644
--- a/roles/borg_client/templates/backup-postgres.sh.j2
+++ b/roles/borg_client/templates/backup-postgres.sh.j2
@@ -3,7 +3,8 @@
 # Script to backup all postgres databases individually
 #
 # Requires local login with `postgres` user and either trusted or peer auth.
-#
+
+set -e
 
 DBLIST=($(sudo -u postgres psql -d postgres -qt -c 'SELECT datname from pg_database'))
 for db in "${DBLIST[@]}"; do
diff --git a/roles/borg_client/templates/borg-backup.sh.j2 b/roles/borg_client/templates/borg-backup.sh.j2
index c0d55a773..bfddba08c 100644
--- a/roles/borg_client/templates/borg-backup.sh.j2
+++ b/roles/borg_client/templates/borg-backup.sh.j2
@@ -40,9 +40,11 @@ cleanup() {
 trap cleanup EXIT
 
 # Dump databases to /root/backup-{postgres,mysql} before taking a btrfs snapshot
-systemctl is-active postgresql && /usr/local/bin/backup-postgres.sh || true
-if systemctl is-active mysqld || systemctl is-active mariadb; then
-    /usr/local/bin/backup-mysql.sh || true
+if systemctl is-active postgresql; then
+    /usr/local/bin/backup-postgres.sh
+fi
+if systemctl is-active mariadb; then
+    /usr/local/bin/backup-mysql.sh
 fi
 
 if is_btrfs "$src"; then
-- 
GitLab