|
NAMEborg-patterns - Details regarding patterns DESCRIPTIONThe path/filenames used as input for the pattern matching start with the currently active recursion root. You usually give the recursion root(s) when invoking borg and these can be either relative or absolute paths. Be careful, your patterns must match the archived paths:
A directory exclusion pattern can end either with or without a slash ('/'). If it ends with a slash, such as some/path/, the directory will be included but not its content. If it does not end with a slash, such as some/path, both the directory and content will be excluded. Borg supports different pattern styles. To define a non-default style for a specific pattern, prefix it with two characters followed by a colon ':' (i.e. fm:path/*, sh:path/**). The default pattern style for --exclude differs from --pattern, see below.
NOTE: re:, sh: and fm: patterns are all
implemented on top of the Python SRE engine. It is very easy to formulate
patterns for each of these types which requires an inordinate amount of time
to match paths. If untrusted users are able to supply patterns, ensure they
cannot supply re: patterns. Further, ensure that sh: and
fm: patterns only contain a handful of wildcards at most.
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. Patterns matching special characters, e.g. white space, within a shell may require adjustments, such as putting quotation marks around the arguments. Example: Using bash, the following command line option would match and exclude "item name": --pattern='-path/item name' Note that when patterns are used within a pattern file directly read by borg, e.g. when using --exclude-from or --patterns-from, there is no shell involved and thus no quotation marks are required. The --exclude-from option permits loading exclusion patterns from a text file with one pattern per line. Lines empty or starting with the number 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 a directory anywhere in the tree named ``steamapps/common`` # (and everything below it), regardless of where it appears: $ borg create -e 'sh:**/steamapps/common/**' backup / # Exclude the contents of ``/home/user/.cache``: $ borg create -e 'sh:home/user/.cache/**' backup /home/user $ borg create -e home/user/.cache/ backup /home/user # The file '/home/user/.cache/important' is *not* backed up: $ borg create -e home/user/.cache/ backup / /home/user/.cache/important # Exclude '/home/user/file.o' but not '/home/user/file.odt': $ borg create -e '*.o' backup / # Exclude '/home/user/junk' and '/home/user/subdir/junk' but # not '/home/user/importantjunk' or '/etc/junk': $ borg create -e 'home/*/junk' backup / # The contents of directories in '/home' are not backed up when their name # ends in '.tmp' $ borg create --exclude 're:^home/[^/]+\.tmp/' backup / # 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 backup / 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.
NOTE: Via --pattern or --patterns-from you can
define BOTH inclusion and exclusion of files using pattern prefixes +
and -. With --exclude and --exclude-from ONLY excludes
are defined.
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. Example: # Define the recursion root R / # Exclude all iso files in any directory - **/*.iso # Explicitly include all inside etc and root + etc/** + root/** # Exclude a specific directory under each user's home directories - home/*/.cache # Explicitly include everything in /home + home/** # Explicitly exclude some directories without recursing into them ! re:^(dev|proc|run|sys|tmp) # Exclude all other files and directories # that are not specifically included earlier. - ** NOTE: It's possible that a sub-directory/file is matched while
parent directories are not. In that case, parent directories are not backed up
thus their user, group, permission, etc. can not be restored.
Note that the default pattern style for --pattern and --patterns-from is shell style (sh:), so those patterns behave similar to rsync include/exclude patterns. The pattern style can be set via the P prefix. 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: # backup 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 repo::arch pics # use a file with patterns: borg create --patterns-from patterns.lst repo::arch The patterns.lst file could look like that: # "sh:" pattern style is the default, so the following line is not needed: P sh R / # can be rebuild - home/*/.cache # they're downloads for a reason - home/*/Downloads # susan is a nice person # include susans home + home/susan # also back up this exact file + pf:home/bobby/specialfile.txt # don't backup the other home directories - home/* # don't even look in /proc ! proc 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 repo::arch /home/bobby /home/susan borg create --patterns-from patternfile.lst repo::arch The patternfile: # 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. AUTHORThe Borg Collective
|