Skip to content

Instantly share code, notes, and snippets.

@ChrisHeerschap
Last active April 26, 2016 12:03
Show Gist options
  • Save ChrisHeerschap/d5c361490fc5e76cbe92c21a6e9f8078 to your computer and use it in GitHub Desktop.
Save ChrisHeerschap/d5c361490fc5e76cbe92c21a6e9f8078 to your computer and use it in GitHub Desktop.
fuckin' waaaaah...
#!/bin/bash
# Debugging
exec 2> /mnt/sto/tmp/debug-check-backups
set -x
# Check timemachine backups
# Vars
TMDIR=/mnt/sto/backup/timemachine
RESULTS=com.apple.TimeMachine.Results.plist
# Number of days without backups to trigger a warning
WARN=7
# Options processing
while getopts ":w:c:h" opt
do
case $opt in
w) WARN=$OPTARG;;
c) echo "It's not really that critical, is it?"; exit 1;;
*) echo "usage: $0 [-w WARN]"; exit 1;;
esac
done
# Define the base find command
findcmd="/usr/bin/find $TMDIR -maxdepth 2 -type f -name $RESULTS"
# Check the timemachine directory and make sure we find the results file
out="$($findcmd 2>&1)"
# If we get no output from the base find command, something's gone wrong.
if [[ -z $out ]]
then
echo "WARNING: Unable to find any files matching $RESULTS in $TMDIR."
exit 1
fi
# Run the same find command and limit it to the warn timeframe
out="$($findcmd -mtime +$WARN 2>&1)"
# Process the output
if [[ -z $out ]]
then
echo "OK: All backups are within the past $WARN days"
exit 0
else
# Kinda an ugly, big-hammer approach to sending the output, but it works.
echo "WARNING: The following backups are older than $WARN days: $(echo "$out" | /usr/bin/awk -F '[/.]' '{print $6}' | /usr/bin/tr '\n' ', ' | /usr/bin/sed 's/,$//')"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment