captaincore/lib/remote-scripts/restic-cache-check
2026-03-11 21:09:19 -10:00

57 lines
1.5 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Checks for restic cache on the remote server.
# Only produces output if a cache directory exists.
#
# Usage: captaincore ssh <site> --script=restic-cache-check
# captaincore ssh @all --script=restic-cache-check
# captaincore ssh @all --script=restic-cache-check --more-than=100MB
#
# Parse flags
for arg in "$@"; do
if [[ $arg == --more-than=* || $arg == --more_than=* ]]; then
more_than="${arg#*=}"
fi
done
cache_dir="$HOME/.cache/restic"
if [[ ! -d "$cache_dir" ]]; then
exit 0
fi
size_bytes=$(du -sb "$cache_dir" 2>/dev/null | cut -f1)
if [[ -z "$size_bytes" ]]; then
exit 0
fi
# Apply --more-than filter
if [[ -n "$more_than" ]]; then
value=$(echo "$more_than" | grep -oE '[0-9]+')
unit=$(echo "$more_than" | grep -oEi '[A-Za-z]+')
case "${unit^^}" in
KB) threshold=$((value * 1024)) ;;
MB) threshold=$((value * 1024 * 1024)) ;;
GB) threshold=$((value * 1024 * 1024 * 1024)) ;;
*) threshold="$value" ;;
esac
if [[ "$size_bytes" -lt "$threshold" ]]; then
exit 0
fi
fi
# Format size as human readable
if [[ "$size_bytes" -ge $((1024 * 1024 * 1024)) ]]; then
size_display="$((size_bytes / 1024 / 1024 / 1024)) GB"
elif [[ "$size_bytes" -ge $((1024 * 1024)) ]]; then
size_display="$((size_bytes / 1024 / 1024)) MB"
elif [[ "$size_bytes" -ge 1024 ]]; then
size_display="$((size_bytes / 1024)) KB"
else
size_display="${size_bytes} B"
fi
echo "${size_display} ${cache_dir}"