Skip to content

Instantly share code, notes, and snippets.

@henri
Last active February 14, 2024 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henri/aa14ecfaeee03a3a44d68b3c708e11c5 to your computer and use it in GitHub Desktop.
Save henri/aa14ecfaeee03a3a44d68b3c708e11c5 to your computer and use it in GitHub Desktop.
macOS TimeMachine cheatsheet
# the man page for tmutil
man tmutil
# calculate drift on backups (cpu / io intensive)
tmutil calculatedrift backup_folder
# list backups
tmutil listbackups
# set backup destination
tmutil setdestination <volume_name>
# remove backup destination
tmutil removedestination [volume_name]
# information on destination volumes configured
tmutil destinationinfo
# exclude directory
tmutil addexclusion "<path_to_exclude>"
# check if some path / file is excluded
tmutil isexcluded "<path_to_check>"
# list local snapshots (by volume) and date
tmutil listlocalsnapshots /
tmutil listlocalsnapshotdates /
# delete local snapshots (by volume and date)
tmutil deletelocalsnapshots date
# thin the snapshots for a mount point
tmutil thinlocalsnapshots mount_point [purge_amount]
# when purge_amount is specified, tmutil will attempt to reclaim purge_amount
# in bytes by thinning snapshots.
# local backups on / off- depends on system version
sudo tmutil enablelocal
sudo tmutil disablelocal
# stop start backups
tmutil stopbackup
tmutil startbackup
# time machine on or off
sudo tmutil enable
sudo tmutil disable
delete backups (one by one)
tmutil delete /Volumes/drive_name/Backups.backupdb/mac_name/YYYY-MM-DD-hhmmss
delete all snapshots (warning danger will robbinson)
for delsnap in $(tmutil listlocalsnapshotdates | grep "-") ; do sudo tmutil deletelocalsnapshots $delsnap ; done
# find the machine's backup direcoty
sudo tmutil machinedirectory
# compact a sparce time machine backup (if it is on a network volume)
hdiutil compact /Volumes/TimeMachineMountPoint/YourBackupName.sparsebundle
# limiting the size of timemachine backup
# from : https://www.defaults-write.com/time-machine-setup-a-size-limit-for-backup-volumes/
sudo defaults write /Library/Preferences/com.apple.TimeMachine MaxSize -integer XX
Replace XX by the number of megabytes that you want to use, 1 GB = 1024 MB
Example 200GB:
sudo defaults write /Library/Preferences/com.apple.TimeMachine MaxSize -integer 204800
To remove the limit:
sudo defaults delete /Library/Preferences/com.apple.TimeMachine MaxSize
# alterative (best) approach is to partition your volume up into a dedicated time machine backup volume
# start backup after setting limits
tmutil startbackup --auto --rotation
# start backup options from man page
# --auto Run the backup in a mode similar to system-scheduled backups.
# --block Wait (block) until the backup is finished before exiting.
# --rotation Allow automatic destination rotation during the backup.
# --destination Perform the backup to the destination corresponding to the specified ID.
# if you are dealing with snapshots on a local volume, then the bottom of the man page will
# provide lots of options fo deleting and thining local snapshots.
#!/bin/bash
# This script will automatically find the oldest TM backup for your computer,
# tell you which is the oldest and newest backup and provide you with a prompt to
# delete the oldest backup. You must enter Y and enter your administrator password
# to delete it. Stolen from StackExchange (thanks nohillside) :
# https://apple.stackexchange.com/questions/39287/how-can-i-manually-delete-old-backups-to-free-space-for-time-machine
#
# potentially helpful for pruning (less work) : https://github.com/oPromessa/tm_del_old_baks
#
COMPUTER_NAME=$(scutil --get ComputerName)
NBACKUPS=$(tmutil listbackups | grep "$COMPUTER_NAME" | wc -l)
OLDEST_BACKUP=$(tmutil listbackups | grep "$COMPUTER_NAME" | head -n1)
LATEST_BACKUP=$(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: sudo tmutil delete "$OLDEST_BACKUP"
sudo time 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