Files
backups/check.sh

57 lines
1.7 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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
else
checkNotPassed "$targetName is not present on $repoName"
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"