Skip to content

Instantly share code, notes, and snippets.

@ahgraber
Created December 30, 2023 16:50
Show Gist options
  • Save ahgraber/4479a05218a67565e11fc9a64038a012 to your computer and use it in GitHub Desktop.
Save ahgraber/4479a05218a67565e11fc9a64038a012 to your computer and use it in GitHub Desktop.
Editing Customize TimeMachine Exclusions

Customize TimeMachine Exclusions

Add exclusions by dirname

find $(pwd) -type d -name <NAME> -maxdepth 3 -exec tmutil addexclusion {} \;

Automate exclusions

We can automate finding and adding exclusions by running tm_exclude.sh script as a cronjob:

  1. Save tm_exclude.sh as executable

    nano ~/tm_exclude.sh
    # paste file contents below, save, and exit
    chmod +x ~/tm_exclude.sh
  2. Open crontab: crontab -e

  3. Schedule tm_exclude.sh:

    # crawl files to add time machine exclusions @1am
    0 1 * * * /bin/bash ~/tm_exclude.sh
  4. Save and exit crontab (:wq if vi; ctrl-o ctrl-x if nano)

See what is currently excluded with

sudo mdfind "com_apple_backup_excludeItem = 'com.apple.backupd'"

Undo exclusions

tmutil removeexclusion /path/to/directory

References

asimov stackexchange peterdemartini/command.sh

#!/usr/bin/env bash
# Credit: modified from asimov/asimov
set -Eeu -o pipefail
# use 'fd' instead of 'find'
if [[ -z "$(command -v fd)" ]]; then
echo "Error: Cannot find 'fd'"
exit 1
fi
# Exclude the given paths from Time Machine backups.
# Reads the newline-separated list of paths from stdin.
exclude_path() {
local path
while IFS=$'\n' read -r path; do
if tmutil isexcluded "${path}" | grep -Fq '[Excluded]'; then
echo "- ${path} is already excluded, skipping."
continue
fi
tmutil addexclusion "${path}"
sizeondisk=$(du -hs "${path}" | cut -f1)
echo "- ${path} has been excluded from Time Machine backups (${sizeondisk})."
done
}
exclusions=(".git" ".venv" "site_packages" "node_modules")
for xln in "${exclusions[@]}"; do
fd \
--hidden \
--no-ignore \
--type directory \
--maxdepth 8 \
--exclude '*/.*' \
--exclude '*/Library' \
--full-path \
--glob "**/${xln}" \
/Users \
| exclude_path;
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment