Exclude a directory except some subdirectories

I would like to exclude ~\.* directories and files, but include the subdirectory ~\.subfolder

I use

restic backup /home/bob —exclude-file EXCLUDE

File EXCLUDE contains ~\.*

How to include ~\.subfolder ?

One solution is to run restic several times for each directory.

However, new snapshots are created for each run of restic. Pruning is then applied to all snapshots.

To fix this, use tags. Tag different backups with different names and copy the prune command with different tags (otherwise, if you keep say last 10 snapshots, it will be total snapshots not per group).

I just tried to find a better solution to this other than having two backup commands.
But as far as I can tell that’s the only way to go.
The exclude is stronger than the include and thus the file won’t be included.

Related discussion and probably the closest you can get currently: Backup public ssh keys but do not backup private ssh keys

I tried negative patterns. I don’t get the logic and can’t get it to work (restic 0.12.1). It seems an experimental feature?

You have to apparently exclude and include several times, which gets confusing.

Perhaps I do not understand the problem you wish to solve but restic has the ability to read a file which lists the files/folders to backup.
restic backup --files-from filewhichliststhethingstobackup
Of course the rest of the backup command needs to specify the repository location, password etc.
The documentation is at –files-from

It seems restic 1.13 has added negative exclusion, exactly what is needed for this.

Hi, I cannot seem to find this solution in the documentation:

https://restic.readthedocs.io/en/stable/040_backup.html?highlight=exclude#excluding-files

The most relevant I can find there is to use an exclude file with exclamation marks, however it states “with the same limitation: once a directory is excluded, it is not possible to include files inside the directory” which, to me, sounds like the solution does not apply here?
Can files/directories within an excluded directory be individually included?

No, if you exclude /test/abc you can’t include /test/abc/def, so you can’t include something if the parent folder is missing. But the example should show how it works for the requested option (exclude all subdirectories, except one).
(You can also check the gitignore documentation: Git - gitignore Documentation)

Excluding all subdirectories of test/abc except one subdirectory test/abc/def is same as excluding test/abc and including test/abc/def.

The end result is the same: backing up test/abc/def, and nothing else in test/abc/.

So what does the provided option do? Does it do the above example?

No you can:

exclude /test/*
include /test/abc

because it’s on the same level.

But the following should not work:

exculde /test/*
include /test/abc/def

You would need to (as I understand):

exclude /test/*
include /test/abc
include /test/abc/def

You mean the depth is one? Ok!

The second case might be

exclude /test/*
include /test/abc
exclude  /test/abc/*
include /test/abc/def

Maybe exclude include order matters too.