Exclude folders by regex (?) from time machine backup

I use a Time Machine server for my backups. Meanwhile I am developing lots of web applications where the project usually consist of a folder with the application files I want to backup and node_modules folder (external libraries) I don't want to backup. In other words the folder structure e.g. is:

projects
-- app1
---- src
---- node_modules -- I want to ignore only this folder
---- package.json
-- app2
---- whatever-else
---- node_modules -- I want to ignore only this folder
---- package.json
---- elephant.jpg

etc.

The amount of applications is big and growing (currently more than 20), the size of node_modules folder is usually thousands times bigger than the rest of the project: the size is huge and the amount of files too.

How to exclude all the node_modules folders within the whole system (like .gitignore but for time machine)? Is there some way to do it by mask? I simply don't want to add the folder to excluded folders manually all the time.

I am also fine with a cron script that searches for the new folders every N minutes and makes them to be ignored.

Are there solutions for that?


You can find and exclude all node_modules folders from the current path, e.g. in your home directory (~), like this:

$ find `pwd` -maxdepth 3 -type d -name 'node_modules' | xargs -n 1 tmutil addexclusion

Kudos to @peterdemartini: https://gist.github.com/peterdemartini/4c918635208943e7a042ff5ffa789fc1

Note that the excluded folders will not appear in the Time Machine System Preferences or when running this command:

$ defaults read /Library/Preferences/com.apple.TimeMachine.plist SkipPaths

When you use this command (tmutil addexclusion), the item you exclude remains in the Time Machine exclusion list even if you move it, which is not the case when you exclude items from the Time Machine preference pane. If you use the above command with the -p flag, then it will not be sticky and will be the same as an exclusion you add from the Time Machine preference pane.

https://www.macworld.com/article/2033804/control-time-machine-from-the-command-line.html


Reviving an old post, but while looking for the same thing I ended up trying this tool called Asimov, and it worked great for me.

Asimov will scan directories looking for node_modules (nodeJs) or vendor (php composer), and exclude then automatically from next TimeCapsule backup. It will also add a cron job to update those exclusions.

Hope this helps someone.