]> gitweb.hhaalo.de Git - simple-backup.git/commitdiff
change none per type
authorBastian Dehn <hhaalo@arcor.de>
Mon, 10 Aug 2026 17:24:42 +0000 (19:24 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Mon, 10 Aug 2026 17:24:42 +0000 (19:24 +0200)
src/backup
tests/backup_tests.bats

index fd31cc0901e791b616aae081385e6d91702738b2..4603f55eb38bd1f518d123a6a3be5d4d94402a80 100755 (executable)
@@ -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()
index f555770431bc8c56492cd4d9abfcf4fadb17a71c..21631e3ae74066e39ed10529057516eae1226e8d 100755 (executable)
@@ -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