nixos-config/modules/borg_check.sh

73 lines
2.4 KiB
Bash
Raw Permalink Normal View History

2022-02-19 11:47:17 +01:00
#!/usr/bin/env bash
export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
function statistics {
journalctl _SYSTEMD_INVOCATION_ID=$(systemctl show --value -p InvocationID borgbackup-job-default) | grep -Eio "$2 archive[s]?.*" | awk '$'$(($1 + 1))'~/^TB/{printf "%.f", $'$1'*1000*1000*1000*1000};$'$(($1 + 1))'~/^GB/{printf "%.f", $'$1'*1000*1000*1000}; $'$(($1 + 1))'~/^MB/{printf "%u", $'$1'*1000*1000}; $'$(($1 + 1))'~/^kB/{printf "%u", $'$1'*1000}; $'$(($1 + 1))'~/^B/{print $'$1'};'
}
function discover {
dirname $(grep -silm1 "This is a Borg Backup repository" /mnt/sdf1/borg/*/README) | awk 'BEGIN{printf "{\"data\":["}; {i=split($1,path,"/"); printf c"{\"{#PATH}\":\""$1"\", \"{#HOST}\":\"" path[i-1] "\", \"{#DIR}\":\"" path[i] "\"}";c="," }; END{print "]}"}'
}
function check {
borg check $1 2> /dev/null | wc -l
}
function _time {
date -d "$(borg info --bypass-lock $1::$(borg list $1 --bypass-lock --short | tail -n1) | grep -i "time ($2)" | sed 's/^.*): //;s/$//')" $3
}
fail=1
while [[ $fail -ne 0 ]]; do
case $1 in
discover)
discover && fail=0
;;
check)
path="$1"
check "$path" && fail=0
;;
time)
path="$3"
case $2 in
start_time)
_time "$path" "start" "" && fail=0
;;
start_timestamp)
_time "$path" "start" "+%s" && fail=0
;;
end_time)
_time "$path" "end" "" && fail=0
;;
end_timestamp)
_time "$path" "end" "+%s" && fail=0
;;
esac
;;
statistics)
path="$4"
case $2 in
original)
scope=3
;;
compressed)
scope=5
;;
dedup)
scope=7
;;
esac
_type=$3
statistics $scope $_type && fail=0
fail=0
;;
debug)
echo "12414"
tmpfile="$(mktemp)"
echo "whyyyy" > "$tmpfile"
echo "asedfasdf" > "/zabbix"
fail=0
;;
*)
exit 1
;;
esac
done