Skip to content

Instantly share code, notes, and snippets.

@poma
Last active May 4, 2022 01:29
Show Gist options
  • Save poma/0e0ccbfca2dd5ba62f18d3b45a8bfc77 to your computer and use it in GitHub Desktop.
Save poma/0e0ccbfca2dd5ba62f18d3b45a8bfc77 to your computer and use it in GitHub Desktop.
Exclude dependency dirs from Time Machine backups
tmutil_exclude() {
# todo: recurse to parent dirs to support commands that execute in project subdirs
DIR=$1
DEP_FILE=$2
if [ -d "$DIR" ] && [ -f "$DEP_FILE" ] && ! tmutil isexcluded "$DIR" | grep -q '\[Excluded\]'; then
tmutil addexclusion "$DIR"
echo "tmutil: ${DIR} has been excluded from Time Machine backups"
fi
}
__npm_wrapper () {
command npm "$@"
EXIT_CODE=$?
tmutil_exclude "node_modules" "package.json"
return $EXIT_CODE
}
__cargo_wrapper () {
command cargo "$@"
EXIT_CODE=$?
tmutil_exclude "target" "cargo.toml"
return $EXIT_CODE
}
alias npm=__npm_wrapper
alias cargo=__cargo_wrapper
@GBrachetta
Copy link

Hi! Will this run on MacOS Monterey?

I'm currently looking to implement something like this, but while this seems to work perfect on Catalina I'm not sure it does on Monterey (the dry run doesn't yield a list of excluded paths).

I really would love to find a (semi) automated way to exclude node_modules and .venv folders from my Time Machine backups.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment