Exclude node_modules folders from MacOS Time Machine

What is the problem?

MacOS includes a very handy backup system called Time Machine which does a snapshot of all changes every hour.

Existing Solutions

Searching for a solution if found this tools:

  • tmignore — excludes files and directories matched by .gitignore files, can run as cron job
  • tmutil — official Time Machine utility, examine and manipulate Time Machine backups, restore data from backups, add or remove exclusions, and compare backups
  • npm wrapper — bash wrapper for npm to run on specific events

Experiance

asimov

good:

  • can be installed as a service with brew to run once a day
  • no options to define the foldes which should be excluded / included
  • no command to list the files which have been excluded
  • no command to remove the excluded directories
  • no dryrun

tmignore

good:

  • can be installed as a service with brew to run once a day
  • has a configuration to define the search path and pattern to include files which are ignored by .gitignore but need to be backuped - e.g. secret keys
  • commands to list and reset
  • to danger to miss something on whitelist and break emergency recover

npm wrapper

This is not working if you use any kind of viual client.

What API is used by this tools?

All this tools use the command line tool tmutil with the command tmutil addexclusion <path to file>.

mkdir "not_backuped"xattr "not_backuped"
tmutil addexclusion "$(pwd)/not_backuped"tmutil isexcluded "$(pwd)/not_backuped"xattr "not_backuped"

Is this item excluded?

tmutil isexcluded "$(pwd)/not_backuped/readme.txt"

Find excluded Folders

Now with the knowledge that excluded items have a specific extended attribute it is very easy to use the find command to find all items with the attribute:

find ~/my-projects -xattrname "com.apple.metadata:com_apple_backup_excludeItem" -type d -prune
  • -xattrname "com.apple.metadata:com_apple_backup_excludeItem" - this is the specific attribute
  • -type d - here we only search for Folders
  • -prune - we are not interested to search inside folders we found

Remove Items from being excluded

  1. use tmutil with absolute path to the item
    tmutil removeexclusion "$(pwd)/not_backuped"
  2. use xattr (not recommended)
    xattr -d "com.apple.metadata:com_apple_backup_excludeItem" not_backuped
  3. use find for multiple items
    find ~/my-projects -xattrname "com.apple.metadata:com_apple_backup_excludeItem" -type d -prune -exec tmutil removeexclusion "{}" \;
    this will search in folder my-projects for folders which are excluded from backup and will remove them from exclusion.

Simple Solution

Create your own simple script which includes your rules with this draft:

Originally published at https://www.heissenberger.at.

Fast-track professional successful in the design, development and deployment of technology strategies and policy. Experienced leading Internet and IS operations

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store