Skip to content

Instantly share code, notes, and snippets.

@deftsp
Forked from blanboom/tmcleanup.md
Created October 13, 2020 01:13
Show Gist options
  • Save deftsp/2f724d97cb4a0c2f313125a38d647233 to your computer and use it in GitHub Desktop.
Save deftsp/2f724d97cb4a0c2f313125a38d647233 to your computer and use it in GitHub Desktop.
Script to automatically remote old Mac OSX Time Machine Backups older than a specified number of days

Prerequisites

  • gfind (via brew install findutils)
  • Time Machine drive mounted on your computer (or you can change path from standard /Volume/Time\ Machine\ Backups/Backups.backup.db/
  • Admin/sudo access in the OSX terminal

Script

  • Be sure to set the correct machine name. You can check the actual folder things are going into by looking in the backup location.
  • By default, it erases all backups older than 30 days. Adjust as desired.

Example

Clean all backups older than 30 days:

sudo ./tmcleanup.sh /Volumes/Time\ Machine\ Backups/Backups.backupdb my_computer_name 30

Clean 50% of backups older than 30 days:

sudo ./tmcleanup_50percent.sh /Volumes/Time\ Machine\ Backups/Backups.backupdb my_computer_name 30
#!/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)
#
# USEAGE:
# ./tmcleanup.sh BACKUP_LOCATION MACHINE_NAME DAY_AGE
#
# EXAMPLE:
# ./tmcleanup.sh /Volumes/Time\ Machine\ Backups/Backups.backupdb my_computer_name 30
#
if [ "$#" -ne 3 ] ; then
echo "USEAGE:"
echo " $0 BACKUP_LOCATION MACHINE_NAME DAY_AGE"
echo "EXAMPLE:"
echo " $0 /Volumes/Time\ Machine\ Backups/Backups.backupdb my_computer_name 30"
exit
fi
BACKUP_LOCATION=$1
MACHINE_NAME=$2
DAY_AGE=$3
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
result=$(gfind "${BACKUP_LOCATION}"/"${MACHINE_NAME}"/ -daystart -maxdepth 1 -mindepth 1 -mmin +$((60*24*${DAY_AGE})))
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 "Run hdiutil compact /path/to/timemachine/sparsebundle if necessary"
echo "Finished!"
else
echo "Did not find any Time Machine Backups older than ${DAY_AGE} days old."
fi
#!/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)
#
# USEAGE:
# ./tmcleanup_50percent.sh BACKUP_LOCATION MACHINE_NAME DAY_AGE
#
# EXAMPLE:
# ./tmcleanup_50percent.sh /Volumes/Time\ Machine\ Backups/Backups.backupdb my_computer_name 30
#
if [ "$#" -ne 3 ] ; then
echo "USEAGE:"
echo " $0 BACKUP_LOCATION MACHINE_NAME DAY_AGE"
echo "EXAMPLE:"
echo " $0 /Volumes/Time\ Machine\ Backups/Backups.backupdb my_computer_name 30"
exit
fi
BACKUP_LOCATION=$1
MACHINE_NAME=$2
DAY_AGE=$3
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
result=$(gfind "${BACKUP_LOCATION}"/"${MACHINE_NAME}"/ -daystart -maxdepth 1 -mindepth 1 -mmin +$((60*24*${DAY_AGE})))
echo "$result"
if [ -n "$result" ]; then
while read -r line1 && read -r line2; do
START_TIME=$(date +%s)
tmutil delete "$line1"
END_TIME=$(date +%s)
echo "Elapsed time: $(($END_TIME - $START_TIME)) secs."
done <<< "$result"
echo "Run hdiutil compact /path/to/timemachine/sparsebundle if necessary"
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