BORG-PATTERNS(1) | borg backup tool | BORG-PATTERNS(1) |
borg-patterns - Details regarding patterns
When specifying one or more file paths in a Borg command that supports patterns for the respective option or argument, you can apply the patterns described here to include only desired files and/or exclude unwanted ones. Patterns can be used
Borg always stores all file paths normalized and relative to the current recursion root. The recursion root is also named PATH in Borg commands like borg create that do a file discovery, so do not confuse the root with the PATH argument of e.g. borg extract.
Starting with Borg 1.2, paths that are matched against patterns always appear relative. If you give /absolute/ as root, the paths going into the matcher will look relative like absolute/.../file.ext. If you give ../some/path as root, the paths will look like some/path/.../file.ext.
File patterns support five different styles. If followed by a colon ':', the first two characters of a pattern are used as a style selector. Explicit style selection is necessary if a non-default style is desired or when the desired pattern starts with two alphanumeric characters followed by a colon (i.e. aa:something/*).
Implementation note: this is implemented via very time-efficient O(1) hashtable lookups (this means you can have huge amounts of such patterns without impacting performance much). Due to that, this kind of pattern does not respect any context or order. If you use such a pattern to include a file, it will always be included (if the directory recursion encounters it). Other include/exclude patterns that would normally match will be ignored. Same logic applies for exclude.
NOTE:
Exclusions can be passed via the command line option --exclude. When used from within a shell, the patterns should be quoted to protect them from expansion.
The --exclude-from option permits loading exclusion patterns from a text file with one pattern per line. Lines empty or starting with the hash sign '#' after removing whitespace on both ends are ignored. The optional style selector prefix is also supported for patterns loaded from a file. Due to whitespace removal, paths with whitespace at the beginning or end can only be excluded using regular expressions.
To test your exclusion patterns without performing an actual backup you can run borg create --list --dry-run ....
Examples:
# Exclude '/home/user/file.o' but not '/home/user/file.odt': $ borg create -e '*.o' archive / # Exclude '/home/user/junk' and '/home/user/subdir/junk' but # not '/home/user/importantjunk' or '/etc/junk': $ borg create -e 'home/*/junk' archive / # Exclude the contents of '/home/user/cache' but not the directory itself: $ borg create -e home/user/cache/ archive / # The file '/home/user/cache/important' is *not* backed up: $ borg create -e home/user/cache/ archive / /home/user/cache/important # The contents of directories in '/home' are not backed up when their name # ends in '.tmp' $ borg create --exclude 're:^home/[^/]+\.tmp/' archive / # Load exclusions from file $ cat >exclude.txt <<EOF # Comment line home/*/junk *.tmp fm:aa:something/* re:^home/[^/]+\.tmp/ sh:home/*/.thumbnails # Example with spaces, no need to escape as it is processed by borg some file with spaces.txt EOF $ borg create --exclude-from exclude.txt archive /
A more general and easier to use way to define filename matching patterns exists with the --pattern and --patterns-from options. Using these, you may specify the backup roots, default pattern styles and patterns for inclusion and exclusion.
The first matching pattern is used, so if an include pattern matches before an exclude pattern, the file is backed up. Note that a no-recurse exclude stops examination of subdirectories so that potential includes will not match - use normal excludes for such use cases.
Tip: You can easily test your patterns with --dry-run and --list:
$ borg create --dry-run --list --patterns-from patterns.txt archive
This will list the considered files one per line, prefixed with a character that indicates the action (e.g. 'x' for excluding, see Item flags in borg create usage docs).
NOTE:
Patterns (--pattern) and excludes (--exclude) from the command line are considered first (in the order of appearance). Then patterns from --patterns-from are added. Exclusion patterns from --exclude-from files are appended last.
Examples:
# back up pics, but not the ones from 2018, except the good ones: # note: using = is essential to avoid cmdline argument parsing issues. borg create --pattern=+pics/2018/good --pattern=-pics/2018 archive pics # back up only JPG/JPEG files (case insensitive) in all home directories: borg create --pattern '+ re:\.jpe?g(?i)$' archive /home # back up homes, but exclude big downloads (like .ISO files) or hidden files: borg create --exclude 're:\.iso(?i)$' --exclude 'sh:home/**/.*' archive /home # use a file with patterns (recursion root '/' via command line): borg create --patterns-from patterns.lst archive /
The patterns.lst file could look like that:
# "sh:" pattern style is the default # exclude caches - home/*/.cache # include susans home + home/susan # also back up this exact file + pf:home/bobby/specialfile.txt # don't back up the other home directories - home/* # don't even look in /dev, /proc, /run, /sys, /tmp (note: would exclude files like /device, too) ! re:^(dev|proc|run|sys|tmp)
You can specify recursion roots either on the command line or in a patternfile:
# these two commands do the same thing borg create --exclude home/bobby/junk archive /home/bobby /home/susan borg create --patterns-from patternfile.lst archive
patternfile.lst:
# note that excludes use fm: by default and patternfiles use sh: by default. # therefore, we need to specify fm: to have the same exact behavior. P fm R /home/bobby R /home/susan - home/bobby/junk
This allows you to share the same patterns between multiple repositories without needing to specify them on the command line.
The Borg Collective
2023-03-01 |