From 5c953c00fceda39f4dea6c3a51cfc4648a70b0aa Mon Sep 17 00:00:00 2001
From: Evangelos Foutras <evangelos@foutrelis.com>
Date: Fri, 9 Jul 2021 04:28:23 +0300
Subject: [PATCH] Avoid running backup-gitlab twice; reuse tarballs

The official backup tool for GitLab takes many hours to run because it
puts everything inside tarballs and then gzips each one. It seems safe
and much more efficient to skip this step for the offsite backup while
reusing the tarballs generated by the first backup to the Storage Box.

Should save ~5 hours from the borg-backup-offsite.service execution.
---
 roles/borg_client/templates/borg-backup.service.j2 | 5 +++++
 roles/borg_client/templates/borg-backup.sh.j2      | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/roles/borg_client/templates/borg-backup.service.j2 b/roles/borg_client/templates/borg-backup.service.j2
index 001eaaa45..bc26b541c 100644
--- a/roles/borg_client/templates/borg-backup.service.j2
+++ b/roles/borg_client/templates/borg-backup.service.j2
@@ -1,5 +1,10 @@
 [Unit]
 Description=Borg backup
+
+{% if inventory_hostname == "gitlab.archlinux.org" %}
+# The ordering relation defined below is important for the GitLab backups
+# because the offsite backup reuses the tarballs from this service's run.
+{% endif %}
 Wants=borg-backup-offsite.service
 Before=borg-backup-offsite.service
 
diff --git a/roles/borg_client/templates/borg-backup.sh.j2 b/roles/borg_client/templates/borg-backup.sh.j2
index ade832c00..271281448 100644
--- a/roles/borg_client/templates/borg-backup.sh.j2
+++ b/roles/borg_client/templates/borg-backup.sh.j2
@@ -45,7 +45,8 @@ if systemctl is-active mysqld || systemctl is-active mariadb; then
     /usr/local/bin/backup-mysql.sh || true
 fi
 
-{% if inventory_hostname == "gitlab.archlinux.org" %}
+{# When backing up to offsite, reuse the existing tarballs from the previous backup #}
+{% if inventory_hostname == "gitlab.archlinux.org" and item['suffix'] != '-offsite' %}
 # Create tarball backups of various GitLab directories using the official backup tool
 systemctl is-active docker && /usr/local/bin/backup-gitlab.sh
 {% endif %}
-- 
GitLab