How to stop Time Machine backing up log files inside application containers?

Time Machine does not backup log files in ~/Library/Logs

But it does backup logs files within application containers. For example, log files in the folder ~/Library/Containers/com.apple.mail/Data/Library/Logs/Mail.

My Mail and OneDrive logs are currently adding about 2 GB per backup.

I could manually exclude each of ~/Library/Containers/*/Data/Library/Logs but that is a lot of exclusions and would be a list which needs addition whenever I install a new app.

The ~/Library/Containers/* folders were introduced to support sandboxing applications, but Time Machine seems to have not kept up with these changes.

Is there a better way of telling Time Machine to not backup the /Log folders within Containers?

I am using OS X Yosemite 10.10.1.


Solution 1:

I had a similar need and using a combination of this answer and this answer I came up with the following solution.

Create a property list (e.g. ~/Library/LaunchAgents/excludeLogFiles.plist) which will be loaded with launchctl load ~/Library/LaunchAgents/excludeLogFiles.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>excludeLogFiles</string>
  <key>ProgramArguments</key>
  <array>
    <string>/bin/zsh</string>
    <string>-c</string>
    <string>tmutil addexclusion /Users/user/Library/Containers/**/Data/Library/Logs</string>
  </array>
  <key>WatchPaths</key>
  <array>
    <string>/Users/user/Library/Containers</string>
  </array>
</dict>
</plist>

Instead of using EnableGlobbing, which hasn't been supported since OS X 10.10 (ref), the path gets expanded by the program calling /bin/zsh <args>.

A couple of things I found while experimenting:

  1. The ** glob works with zsh but not bash
  2. Using ~ should work in the path for ProgramArguments but I don't know if the same trick can be used for WatchPath. Hard-coding the user path might be necessary.