From bcd3a0b8fa7f2f814a96b62b9aa526ae74c9a835 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Mon, 10 Aug 2026 19:24:42 +0200 Subject: [PATCH] change none per type --- src/backup | 36 ++++++++++++++++++++++++++++++++++-- tests/backup_tests.bats | 6 +++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/backup b/src/backup index fd31cc0..4603f55 100755 --- a/src/backup +++ b/src/backup @@ -349,16 +349,48 @@ cleanup_old_backups() done } +is_backup_old() +{ + local type="$1" + local date="$2" + local backup_month= + local now_month= + local diff= + [ "$date" == none ] || return 1 + backup_month=$(date --date="$date" '+%Y-%m') + now_month=$(date '+%Y-%m') + diff=$(( ($(date --date="$date" +%s) - $(date +%s)) / 86400 )) + + case "$type" in + "day") [ $diff -ge 0 ] || return 1 ;; + "week") [ $diff -ge -6 ] || return 1 ;; + "month") [ "$backup_month" == "$now_month" ] || return 1 ;; + *) return 0 ;; + esac +} + check_backup_date() { local config="$1" local LIGHTRED="\e[1;31m" local LIGHTGREEN="\e[1;32m" local RESET="\e[0m" - local stat_file=$(echo "$config" | jq --raw-output '.backup_stat_file') + local color="$LIGHTGREEN" + local stat= + local type= + local line=1 + local stat_file= + stat_file=$(echo "$config" | jq --raw-output '.backup_stat_file') + [ -n "$NOCOLOR" ] && LIGHTRED= && LIGHTGREEN= && RESET= && color= [ "$stat_file" != "null" ] || return 0 [ -f "$stat_file" ] || printf "none%-.s\n" {1..3} > "$stat_file" - cat "$stat_file" + + for type in day week month; do + stat=$(awk "NR==$line {print; exit}" "$stat_file") + is_backup_old "$type" "$stat" || color="$LIGHTRED" + printf "${color}%s %s${RESET}\n" "$type" "$stat" + ((line++)) + done } main() diff --git a/tests/backup_tests.bats b/tests/backup_tests.bats index f555770..21631e3 100755 --- a/tests/backup_tests.bats +++ b/tests/backup_tests.bats @@ -977,7 +977,7 @@ teardown() { assert_success [ -f "build/test/stats" ] - assert_line --index 0 "none" - assert_line --index 1 "none" - assert_line --index 2 "none" + assert_line --index 0 "day none" + assert_line --index 1 "week none" + assert_line --index 2 "month none" } \ No newline at end of file -- 2.47.3