diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index dc84ed280..1e0c3c46b 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -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() diff --git a/doc/040_backup.rst b/doc/040_backup.rst index ec3e957b7..b1e92369b 100644 --- a/doc/040_backup.rst +++ b/doc/040_backup.rst @@ -158,7 +158,9 @@ Patterns use `filepath.Glob `__ inte see `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 `__. +exclude-files are expanded with `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``: