jeff
November 16, 2022, 8:38am
1
Hi,
I cannot find out how to set exclude options such that all but one special subdirectory is excluded and furthermore all (arbitrary) files in the first level are kept. Something like
--exclude 'path/to/exclude/**/*' --exclude '!path/to/exclude/keep_nonetheless'
does the first thing, simple enough.
Appending another
--exclude '!path/to/exclude/*
for keeping all files unfortunately draws back the whole contents of ‘path/to/exclude’.
Any idea? Thanks!
1 Like
akrabu
November 17, 2022, 3:13am
2
Check this PR:
restic:master
← vincentbernat:feature/negative-pattern
opened 09:25PM - 14 Jun 19 UTC
This is quite similar to gitignore. If a pattern is prefixed by an
exclamation … mark and match a file that was previously matched by a
regular pattern, the match is cancelled. Notably, this can be used
with `--exclude-file` to cancel the exclusion of some files.
Like for gitignore, once a directory is excluded, it is not possible
to include files inside the directory. For example, a user wanting to
only keep `*.c` in some directory should not use:
~/work
!~/work/*.c
But:
~/work/*
!~/work/*.c
I didn't write documentation or changelog entry. I would like to get
feedback if this is the right approach for excluding/including files
at will for backups. I use something like this as an exclude file to
backup my home:
$HOME/**/*
!$HOME/Documents
!$HOME/code
!$HOME/.emacs.d
!$HOME/games
# [...]
node_modules
*~
*.o
*.lo
*.pyc
# [...]
$HOME/code/linux/*
!$HOME/code/linux/.git
# [...]
There are some limitations for this change:
- Patterns are not mixed accross methods: patterns from file are
handled first and if a file is excluded with this method, it's not
possible to reinclude it with `--exclude !something`.
- Patterns starting with `!` are now interpreted as a negative
pattern. I don't think anyone was relying on that.
- The whole list of patterns is walked for each match. We may
optimize later by exiting early if we know no pattern is starting
with `!`.
Fix #233
Checklist
---------
- [x] I have read the [Contribution Guidelines](https://github.com/restic/restic/blob/master/CONTRIBUTING.md#providing-patches)
- [x] I have added tests for all changes in this PR
- [x] I have added documentation for the changes (in the manual)
- [x] There's a new file in `changelog/unreleased/` that describes the changes for our users (template [here](https://github.com/restic/restic/blob/master/changelog/TEMPLATE))
- [x] I have run `gofmt` on the code in all commits
- [x] All commit messages are formatted in the same style as [the other commits in the repo](https://github.com/restic/restic/blob/master/CONTRIBUTING.md#git-commits)
- [x] I'm done, this Pull Request is ready for review
I think it’s merged, so it should be working. The last post may be of interest to you.
!/Users
/Users/*
!/Users/daniel
So perhaps using --exclude “!path/to/exclude” --exclude “path/to/exclude/*” --exclude “!path/to/exclude/keep_nonetheless” would work?
flomp
November 17, 2022, 8:57am
3
Oh interesting and very cool! I did not know that this functionality has been implemented.
I haven’t tried, but the docs say, that the following should work, too:
/Users/**/*
!/Users/daniel
(see Backing up — restic 0.14.0 documentation )
Does anybody know, if the behavior is different between /Users/*
and /Users/**/*
?
The first one only explicitly excludes everything directly within the Users
directory, which means that you can easily cancel its effect. The latter one excludes everything inside Users
and in every of its subfolders. The latter is somewhat inefficient, as matching **
takes more time.
But the negative pattern should have priority, so both should work, although the **/
variant is less efficient.
2 Likes
flomp
June 22, 2024, 9:09pm
6
Just for documention: Here is a more complicated example:
AppData/Roaming/Code/*
!AppData/Roaming/Code/User
AppData/Roaming/Code/User/*
!AppData/Roaming/Code/User/*.json
!AppData/Roaming/Code/User/snippets
It is for Visual Studio Code under Windows and does the following:
Exclude everything of the VSCode-folder
…but do descend into the User
-folder
…but still exclude everything there
…except all .json
-files in that folder
…and except the snippets
sub-folder
Hope this helps.