Skip to content

Instantly share code, notes, and snippets.

@michaljuris
Created October 9, 2020 07:36
Show Gist options
  • Save michaljuris/80646512fec543b5609f38e58381bb8b to your computer and use it in GitHub Desktop.
Save michaljuris/80646512fec543b5609f38e58381bb8b to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Pre-requesite is that GNU linux findutils package is installed (brew install findutils) for gfind
# Should be possible to just use find but I have not tested it (should just omit -daystart)
#
BACKUP_LOCATION=$(tmutil machinedirectory)
#MACHINE_NAME=$(scutil --get ComputerName)
DAY_AGE=30
if [ ! -d "${BACKUP_LOCATION}" ]; then
echo "${BACKUP_LOCATION} is currently not accessible. Please connect your Time Machine disk or change the path."
exit
fi
echo "*-* Removing old Time Machine Backups for ${MACHINE_NAME} older than ${DAY_AGE} days *-*"
# Get a list of all the backups in reverse order
result=$(gfind "${BACKUP_LOCATION}"/ -daystart -maxdepth 1 -mindepth 1 -mmin +$((60*24*${DAY_AGE})) | tail -r)
echo "$result"
if [ -n "$result" ]; then
while read -r line; do
START_TIME=$(date +%s)
tmutil delete "$line"
END_TIME=$(date +%s)
echo "Elapsed time: $(($END_TIME - $START_TIME)) secs."
done <<< "$result"
echo "Finished!"
else
echo "Did not find any Time Machine Backups older than ${DAY_AGE} days old."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment