date --iso-8601=seconds
}
+get_date()
+{
+ date --iso-8601=date
+}
+
get_backup_type()
{
local config="$1"
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" ] \
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
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"
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--))
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"
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"
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