From 1a3efadea29b99ab9522c1937dba202cc90fae28 Mon Sep 17 00:00:00 2001 From: Jiri Podivin Date: Thu, 31 Oct 2024 13:48:54 +0100 Subject: [PATCH] Newly downloaded archive is compared against existing files to ensure uniqueness Signed-off-by: Jiri Podivin --- .../templates/backup.sh.j2 | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/roles/log-detective-backup/templates/backup.sh.j2 b/roles/log-detective-backup/templates/backup.sh.j2 index d023ee3f5d..0f1e04b64e 100644 --- a/roles/log-detective-backup/templates/backup.sh.j2 +++ b/roles/log-detective-backup/templates/backup.sh.j2 @@ -1,5 +1,22 @@ -#! /bin/sh +#!/bin/bash + +set -e basename=$(date +%s).tar.gz +ld_backup_path="{{ ld_backup_path }}" +backup_path=$ld_backup_path/$basename -wget -O "{{ ld_backup_path }}/$basename" "{{ ld_dump_url }}" +# Find last modified +recent=$(find "$ld_backup_path" -type f -name "*tar.gz" -mtime -1 | sort | tail -n 1) + +wget -O "$backup_path" "{{ ld_dump_url }}" + +# Extract both and get hashes +new=$(tar -xzf "$backup_path" -O | sha256sum) +old=$(tar -xzf "$recent" -O | sha256sum) + +# Compare hashes +if [ "$new" = "$old" ]; then + echo No changes since last backup, removing downloaded archive + rm "$recent" +fi