From: Bastian Dehn Date: Mon, 10 Aug 2026 18:19:55 +0000 (+0200) Subject: add check day old and ok X-Git-Tag: v1.0.6^2~1^2~10 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=8b66cf94ae9f8c7d0125a9c17d67a98897be53a1;p=simple-backup.git add check day old and ok --- diff --git a/src/backup b/src/backup index c99b2ca..76b56ad 100755 --- a/src/backup +++ b/src/backup @@ -14,6 +14,11 @@ get_date_time() date --iso-8601=seconds } +get_date() +{ + date --iso-8601=date +} + get_backup_type() { local config="$1" @@ -149,7 +154,7 @@ get_backup_dir() local date= local last_backup_number="1" local last_backup_dir="${last_backup_dir_path##*/}" - date=$(date --iso-8601=date) + date=$(get_date) backup_path=$(echo "$config" | jq --raw-output '.backup_path') backup_name=$(echo "$config" | jq --raw-output '.backup_name') [ "${last_backup_dir:0:10}" == "$date" ] \ @@ -349,21 +354,33 @@ cleanup_old_backups() done } +get_unix_date() +{ + date --date="$1" +%s +} + +get_unix_time() +{ + date +%s +} + 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 )) + + local DAY=86400 + local diff= + local unix_date= + local unix_current_date= + unix_date=$(get_unix_date "$date") + unix_current_date=$(get_unix_time) + diff=$(( ("$unix_date" - "$unix_current_date") / "$DAY" )) case "$type" in - "day") [ $diff -ge 0 ] || return 1 ;; - "week") [ $diff -ge -6 ] || return 1 ;; + "day") [ "$diff" -ge "0" ] || return 1 ;; + "week") [ "$diff" -ge "-6" ] || return 1 ;; "month") [ "$backup_month" == "$now_month" ] || return 1 ;; *) return 0 ;; esac @@ -390,6 +407,7 @@ check_backup_date() for type in day week month; do status="ok" stat=$(awk "NR==$line {print; exit}" "$stat_file") + is_backup_old "$type" "$stat" || status="old" [ "$status" != "ok" ] || color="$LIGHTRED" diff --git a/tests/backup_tests.bats b/tests/backup_tests.bats index f566acc..97dcf43 100755 --- a/tests/backup_tests.bats +++ b/tests/backup_tests.bats @@ -9,9 +9,12 @@ setup() { export RSYNC_LOG="build/test/rsync.log" export RSYNC_LOG_RUNS="0" source src/backup - date() { + get_date() { echo "2000-01-01" } + get_date_time() { + echo "2000-01-01T23:30:45+02:00" + } rsync() { [ "$RSYNC_LOG_RUNS" -eq "0" ] && return 0 ((RSYNC_LOG_RUNS--)) @@ -398,7 +401,7 @@ teardown() { assert_line --index 7 "ℹ️ INFO: create directory build/test/2000-01-01_01_backup/testpc1" assert_line --index 8 "ℹ️ INFO: create directory build/test/2000-01-01_01_backup/testpc2" assert_line --index 9 "ℹ️ INFO: create directory build/test/2000-01-01_01_backup/onlylocal" - assert_line --index 10 "ℹ️ INFO: 2000-01-01 [none] created 2000-01-01_01_backup -> build/test/backup.log" + assert_line --index 10 "ℹ️ INFO: 2000-01-01T23:30:45+02:00 [none] created 2000-01-01_01_backup -> build/test/backup.log" assert_line --index 11 "ℹ️ INFO: backup count: 1" assert_line --index 12 "🏁 FINISH: backup run" assert_line --index 13 "✅ SUCCESS: backup" @@ -440,7 +443,7 @@ teardown() { assert_line --index 19 "################################################################################" assert_line --index 20 "ℹ️ INFO: machine ${LIGHTPURPLE}onlylocal${RESET}; path ${YELLOW}/home/local${RESET}" assert_line --index 21 "################################################################################" - assert_line --index 22 "ℹ️ INFO: 2000-01-01 [none] created 2000-01-01_01_backup -> build/test/backup.log" + assert_line --index 22 "ℹ️ INFO: 2000-01-01T23:30:45+02:00 [none] created 2000-01-01_01_backup -> build/test/backup.log" assert_line --index 23 "ℹ️ INFO: backup count: 1" assert_line --index 24 "🏁 FINISH: backup run" assert_line --index 25 "✅ SUCCESS: backup" @@ -980,4 +983,44 @@ teardown() { assert_line --index 0 "day none old" assert_line --index 1 "week none old" assert_line --index 2 "month none old" +} + +# bats test_tags=backup:check +@test "success check backup date day ok" { + mkdir --parents "build/test" + printf "2000-01-01\nnone\nnone" > "build/test/stats" + get_unix_date() { + date --date="2000-01-01" +%s + } + get_unix_time() { + date --date="2000-01-01" +%s + } + + run main "check" + + assert_success + [ -f "build/test/stats" ] + assert_line --index 0 "day 2000-01-01 ok" + assert_line --index 1 "week none old" + assert_line --index 2 "month none old" +} + +# bats test_tags=backup:check +@test "success check backup date day old" { + mkdir --parents "build/test" + printf "2000-01-01\nnone\nnone\n" > "build/test/stats" + get_unix_date() { + date --date="2000-01-01" +%s + } + get_unix_time() { + date --date="2000-01-02" +%s + } + + run main "check" + + assert_success + [ -f "build/test/stats" ] + assert_line --index 0 "day 2000-01-01 old" + assert_line --index 1 "week none old" + assert_line --index 2 "month none old" } \ No newline at end of file