Skip to content

Instantly share code, notes, and snippets.

@appler1009
Created June 21, 2020 00:58
Show Gist options
  • Select an option

  • Save appler1009/adf230249d1f1e983986b95756d37a77 to your computer and use it in GitHub Desktop.

Select an option

Save appler1009/adf230249d1f1e983986b95756d37a77 to your computer and use it in GitHub Desktop.
Cleanup oldest Time Machine backup
#!/bin/sh
COMPUTER_NAME=$(/usr/sbin/scutil --get ComputerName)
NBACKUPS=$(/usr/bin/tmutil listbackups |
/usr/bin/grep "$COMPUTER_NAME" |
/usr/bin/wc -l)
OLDEST_BACKUP=$(/usr/bin/tmutil listbackups |
/usr/bin/grep "$COMPUTER_NAME" |
/usr/bin/head -n1)
LATEST_BACKUP=$(/usr/bin/tmutil latestbackup)
echo Latest backup: $LATEST_BACKUP
if [[ -n "$LATEST_BACKUP" && "$LATEST_BACKUP" != "$OLDEST_BACKUP" ]]
then
echo -n "$NBACKUPS backups. Delete oldest: ${OLDEST_BACKUP##*/} [y/N]? "
read answer
case $answer in
y*)
echo Running: /usr/bin/sudo /usr/bin/tmutil delete "$OLDEST_BACKUP"
/usr/bin/sudo time /usr/bin/tmutil delete "$OLDEST_BACKUP"
;;
*)
echo No change
;;
esac
else
echo "No backup available for deletion"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment