Using mdls for tags and rysc to backup data? (OSX)

56 Views Asked by At

I'm using rsync to make some regularly backups. Now I want to exclude all files and folders with a red an orange tag. As I learned using tags in OSX isn't available as a criteria to rsync. But with an extension to mdls it would be possible. But I don't have any glue how? Any hints or examples? Thanks in advance Dirk

Summary: I already run rsync successfully. But mdls I didn't understand as well, as the result of mdls would be a filter to rsync.

1

There are 1 best solutions below

5
Mark Setchell On

I am not in a position to test this, but you should be able to get something working if you use this as a starting point.

The general idea is to use rsync's --exclude-from "excluded.txt" option to specify the things you don't want included in your backup, like this:

rsync -avz --exclude-from "excluded.txt" source/ destination/

where "excluded.txt" contains one line per file or directory you wish to exclude. Try making a simple two-line excluded.txt by hand to see if you can get the hang of it first.


One the macOS side, you need to use mdfind to find your red-tagged or orange-tagged files. The basic starting point is:

mdfind -onlyin SOMEDIRECTORY 'tag:red OR tag:orange'

Maybe you actually mean 'tag:red AND tag:orange'? This will produce a list of files (and directories) tagged that way, so you will then want to redirect that to a file when it looks right using:

mdfind -onlyin SOMEDIRECTORY 'tag:red OR tag:orange' > excluded.txt

And then you can join the two commands:

mdfind -onlyin SOMEDIRECTORY 'tag:red OR tag:orange' > excluded.txt
rsync -avz --exclude-from "excluded.txt" source/ destination/

There are ways of doing that without creating a temporary file, but I'm not sure if you want anything that complicated:

rsync -avz --exclude-from <(mdfind -onlyin SOMEDIRECTORY 'tag:red OR tag:orange')  source/ destination/

Note also that I believe rsync uses only relative paths for the --exclude-from parameter, so you may need to convert absolute to relative paths.


Note there's an excellent intro to mdfind by Michael Klement that I commend to you for reading and upvoting here.