Allow excluding files with $ via --exclude-file

Previously it wasn't possbile to exclude files with a literal dollar
sign (`$`) via exclude files, now users can write `$$` for that.
This commit is contained in:
Alexander Neumann 2018-06-22 20:46:04 +02:00
parent bb2ad76833
commit 025ec9dff5
2 changed files with 15 additions and 3 deletions

View File

@ -234,8 +234,18 @@ func collectRejectFuncs(opts BackupOptions, repo *repository.Repository, targets
}
// readExcludePatternsFromFiles reads all exclude files and returns the list of
// exclude patterns.
// exclude patterns. For each line, leading and trailing white space is removed
// and comment lines are ignored. For each remaining pattern, environment
// variables are resolved. For adding a literal dollar sign ($), write $$ to
// the file.
func readExcludePatternsFromFiles(excludeFiles []string) []string {
getenvOrDollar := func(s string) string {
if s == "$" {
return "$"
}
return os.Getenv(s)
}
var excludes []string
for _, filename := range excludeFiles {
err := func() (err error) {
@ -258,7 +268,7 @@ func readExcludePatternsFromFiles(excludeFiles []string) []string {
continue
}
line = os.ExpandEnv(line)
line = os.Expand(line, getenvOrDollar)
excludes = append(excludes, line)
}
return scanner.Err()

View File

@ -158,7 +158,9 @@ Patterns use `filepath.Glob <https://golang.org/pkg/path/filepath/#Glob>`__ inte
see `filepath.Match <https://golang.org/pkg/path/filepath/#Match>`__ for
syntax. Patterns are tested against the full path of a file/dir to be saved,
even if restic is passed a relative path to save. Environment-variables in
exclude-files are expanded with `os.ExpandEnv <https://golang.org/pkg/os/#ExpandEnv>`__.
exclude-files are expanded with `os.ExpandEnv <https://golang.org/pkg/os/#ExpandEnv>`__,
so `/home/$USER/foo` will be expanded to `/home/bob/foo` for the user `bob`. To
get a literal dollar sign, write `$$` to the file.
Patterns need to match on complete path components. For example, the pattern ``foo``: