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()
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