#!/usr/bin/env bash set -eu -o pipefail verbose='' # we test for (non)emptiness quiet='' # we test for (non)emptiness source lib.sh exitCode=0 checkNotPassed() { echo "WARN: $1" if [ "$exitCode" -eq 0 ]; then exitCode=2 fi } isRecentInRepo() { local repoName="$1" targetName="$2" maxAge="$3" checkPathComponent "$repoName" checkPathComponent "$targetName" local targetFile="targets/$targetName" backupsDir="backups/$repoName" local hostname="$(< "$targetFile" jq --raw-output .hostname)" backedUpPath="$(< "$targetFile" jq --raw-output .path)" local dates="" while IFS='' read -r -d '' backupFile; do verbose "Processing $backupFile" local date if date="$(< "$backupFile" jq --exit-status --raw-output --arg hostname "$hostname" --arg path "$backedUpPath" 'if $hostname == .hostname and any(.paths[]; . == $path) then .time | sub("\\.\\d+"; "") | strptime("%Y-%m-%dT%H:%M:%S%z") | mktime else false end')"; then verbose "The snapshot is relevant and was created at $date" dates="$dates"$'\n'"$date" fi done < <(find "$backupsDir" -maxdepth 1 -type f -print0) local max="$(echo "$dates" | sort --numeric-sort --reverse | head -n 1)" if [ -n "$max" ]; then local age="$(("$now"-"$max"))" verbose "Last backup was at $max, now it's $now, the age is $age" if [ "$age" -gt "$maxAge" ]; then checkNotPassed "$targetName on $repoName is too old" else info "$targetName on $repoName is fresh enough" fi fi } isRecent() { abort "isRecent is not implemented yet" } now="$(date +%s)" ##### End of definitions, user defined checks go below this line ##### End of user defined checks, the rest of this file is a predefined trailer exit "$exitCode"