Skip to content

Instantly share code, notes, and snippets.

@popmonkey
Last active October 22, 2019 22:44
Show Gist options
  • Save popmonkey/dcd6446105dd75f4602d0a092894a83b to your computer and use it in GitHub Desktop.
Save popmonkey/dcd6446105dd75f4602d0a092894a83b to your computer and use it in GitHub Desktop.
dramatically speed up Time Machine backups on OS X
#!/bin/sh
# Turns off low priorioty throttling while Time Machine backups are in progress.
# Also prevents sleeping/idling while TM is running
#
# by https://github.com/popmonkey
#
# to truly automate this, add the following to /etc/sudoers (sudo visudo)
# (remove one set of # and change 'user' to your username)
# # allow speedy_tm to set throttle
# user ALL=(root) NOPASSWD: /usr/sbin/sysctl debug.lowpri_throttle_enabled=1
# user ALL=(root) NOPASSWD: /usr/sbin/sysctl debug.lowpri_throttle_enabled=0
# and maybe even run it on boot
LAST_PHASE=
log() {
echo "[$(date)] $@"
}
is_tm_running() {
CURRENT_PHASE=$(tmutil currentphase)
if [[ $LAST_PHASE != $CURRENT_PHASE ]]; then
log Phase: $CURRENT_PHASE
LAST_PHASE=$CURRENT_PHASE
fi
if [[ $CURRENT_PHASE == "BackupNotRunning" ]]; then
return 1
else
return 0
fi
}
set_throttle() {
CURRENT_VAL=$(sysctl debug.lowpri_throttle_enabled | awk '{ print $2 }')
if [[ $1 != $CURRENT_VAL ]]; then
log changing lowpri throttle to $1
sudo sysctl debug.lowpri_throttle_enabled=$1
fi
}
ctrl_c() {
log "CTRL-C trapped"
set_throttle 1
exit 0
}
trap ctrl_c INT
while : ; do
if is_tm_running; then
set_throttle 0
# prevent system from sleeping
caffeinate -i sleep 29 &
else
set_throttle 1
fi
sleep 30
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment