Tarsnap / tarsnap

Command-line client code for Tarsnap.
https://tarsnap.com
Other
864 stars 60 forks source link

Anchored excludes #604

Open riastradh opened 8 months ago

riastradh commented 8 months ago

Suppose I have the following directory tree:

Suppose I want to exclude ./foo, but include everything else -- including ./a/foo.

This comment suggests the possibility of anchored matches for --exclude was considered incompletely and was meant to be re-considered:

https://github.com/Tarsnap/tarsnap/blob/dfcc22d1e19e6813841aa6bd731be3bb357b252f/tar/matching.c#L70-L73

I can use some elaborate system of find(1) patterns piped to tarsnap to achieve the same result, but it would be nice if I could simply specify anchored path patterns in the exclusion file. I think the syntax of --exclude /foo is not currently useful so it could probably be used for this purpose, like in .gitignore files.

I realized this today when I discovered that the exclusion was broader than I intended, resulting in some files not being backed up when I thought they would be for years. (Fortunately, in my case, the files are not important.)

gperciva commented 8 months ago

Right. The logic (and rationale) of --include and --exclude in BSD tar(1) is rather opaque, as we can see in all the questions on stackoverflow and related sites. That said, we wouldn't want to change the current behaviour, since that would break compatibility with tar (and with previous versions of tarsnap).

I'll look into current libarchive to see if/how they've addressed this, although an initial grep shows that comment is unchanged. I see that GNU tar(1) has a ton of --exclude options -- 12 of them! -- and that's not even counting the 8 options which changes the behaviour of exclude patterns.

As you mentioned, you could use some find(1) magic.

My personal strategy would be to use find with grep:

$ find . -type f | grep -v "^./foo$"
./bar
./a/foo
./a/bar

since I'm already familiar with grep, and I can never remember which find patterns are POSIX or not.

(I'm glad that you noticed the un-backed-up files!)