borg-patterns.1 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. .\" Man page generated from reStructuredText.
  2. .
  3. .TH BORG-PATTERNS 1 "2020-12-06" "" "borg backup tool"
  4. .SH NAME
  5. borg-patterns \- Details regarding patterns
  6. .
  7. .nr rst2man-indent-level 0
  8. .
  9. .de1 rstReportMargin
  10. \\$1 \\n[an-margin]
  11. level \\n[rst2man-indent-level]
  12. level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
  13. -
  14. \\n[rst2man-indent0]
  15. \\n[rst2man-indent1]
  16. \\n[rst2man-indent2]
  17. ..
  18. .de1 INDENT
  19. .\" .rstReportMargin pre:
  20. . RS \\$1
  21. . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
  22. . nr rst2man-indent-level +1
  23. .\" .rstReportMargin post:
  24. ..
  25. .de UNINDENT
  26. . RE
  27. .\" indent \\n[an-margin]
  28. .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
  29. .nr rst2man-indent-level -1
  30. .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
  31. .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
  32. ..
  33. .SH DESCRIPTION
  34. .sp
  35. The path/filenames used as input for the pattern matching start from the
  36. currently active recursion root. You usually give the recursion root(s)
  37. when invoking borg and these can be either relative or absolute paths.
  38. .sp
  39. So, when you give \fIrelative/\fP as root, the paths going into the matcher
  40. will look like \fIrelative/.../file.ext\fP\&. When you give \fI/absolute/\fP as
  41. root, they will look like \fI/absolute/.../file.ext\fP\&.
  42. .sp
  43. File paths in Borg archives are always stored normalized and relative.
  44. This means that e.g. \fBborg create /path/to/repo ../some/path\fP will
  45. store all files as \fIsome/path/.../file.ext\fP and \fBborg create
  46. /path/to/repo /home/user\fP will store all files as
  47. \fIhome/user/.../file.ext\fP\&.
  48. .sp
  49. File patterns support these styles: fnmatch, shell, regular expressions,
  50. path prefixes and path full\-matches. By default, fnmatch is used for
  51. \fB\-\-exclude\fP patterns and shell\-style is used for the experimental
  52. \fB\-\-pattern\fP option.
  53. .sp
  54. Starting with Borg 1.2, for all but regular expression pattern matching
  55. styles, all paths are treated as relative, meaning that a leading path
  56. separator is removed after normalizing and before matching. This allows
  57. you to use absolute or relative patterns arbitrarily.
  58. .sp
  59. If followed by a colon (\(aq:\(aq) the first two characters of a pattern are
  60. used as a style selector. Explicit style selection is necessary when a
  61. non\-default style is desired or when the desired pattern starts with
  62. two alphanumeric characters followed by a colon (i.e. \fIaa:something/*\fP).
  63. .INDENT 0.0
  64. .TP
  65. .B \fI\%Fnmatch\fP, selector \fIfm:\fP
  66. This is the default style for \fB\-\-exclude\fP and \fB\-\-exclude\-from\fP\&.
  67. These patterns use a variant of shell pattern syntax, with \(aq*\(aq matching
  68. any number of characters, \(aq?\(aq matching any single character, \(aq[...]\(aq
  69. matching any single character specified, including ranges, and \(aq[!...]\(aq
  70. matching any character not specified. For the purpose of these patterns,
  71. the path separator (backslash for Windows and \(aq/\(aq on other systems) is not
  72. treated specially. Wrap meta\-characters in brackets for a literal
  73. match (i.e. \fI[?]\fP to match the literal character \fI?\fP). For a path
  74. to match a pattern, the full path must match, or it must match
  75. from the start of the full path to just before a path separator. Except
  76. for the root path, paths will never end in the path separator when
  77. matching is attempted. Thus, if a given pattern ends in a path
  78. separator, a \(aq*\(aq is appended before matching is attempted. A leading
  79. path separator is always removed.
  80. .TP
  81. .B Shell\-style patterns, selector \fIsh:\fP
  82. This is the default style for \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP\&.
  83. Like fnmatch patterns these are similar to shell patterns. The difference
  84. is that the pattern may include \fI**/\fP for matching zero or more directory
  85. levels, \fI*\fP for matching zero or more arbitrary characters with the
  86. exception of any path separator. A leading path separator is always removed.
  87. .TP
  88. .B Regular expressions, selector \fIre:\fP
  89. Regular expressions similar to those found in Perl are supported. Unlike
  90. shell patterns regular expressions are not required to match the full
  91. path and any substring match is sufficient. It is strongly recommended to
  92. anchor patterns to the start (\(aq^\(aq), to the end (\(aq$\(aq) or both. Path
  93. separators (backslash for Windows and \(aq/\(aq on other systems) in paths are
  94. always normalized to a forward slash (\(aq/\(aq) before applying a pattern. The
  95. regular expression syntax is described in the \fI\%Python documentation for
  96. the re module\fP\&.
  97. .TP
  98. .B Path prefix, selector \fIpp:\fP
  99. This pattern style is useful to match whole sub\-directories. The pattern
  100. \fIpp:root/somedir\fP matches \fIroot/somedir\fP and everything therein. A leading
  101. path separator is always removed.
  102. .TP
  103. .B Path full\-match, selector \fIpf:\fP
  104. This pattern style is (only) useful to match full paths.
  105. This is kind of a pseudo pattern as it can not have any variable or
  106. unspecified parts \- the full path must be given. \fIpf:root/file.ext\fP matches
  107. \fIroot/file.ext\fP only. A leading path separator is always removed.
  108. .sp
  109. Implementation note: this is implemented via very time\-efficient O(1)
  110. hashtable lookups (this means you can have huge amounts of such patterns
  111. without impacting performance much).
  112. Due to that, this kind of pattern does not respect any context or order.
  113. If you use such a pattern to include a file, it will always be included
  114. (if the directory recursion encounters it).
  115. Other include/exclude patterns that would normally match will be ignored.
  116. Same logic applies for exclude.
  117. .UNINDENT
  118. .sp
  119. \fBNOTE:\fP
  120. .INDENT 0.0
  121. .INDENT 3.5
  122. \fIre:\fP, \fIsh:\fP and \fIfm:\fP patterns are all implemented on top of the Python SRE
  123. engine. It is very easy to formulate patterns for each of these types which
  124. requires an inordinate amount of time to match paths. If untrusted users
  125. are able to supply patterns, ensure they cannot supply \fIre:\fP patterns.
  126. Further, ensure that \fIsh:\fP and \fIfm:\fP patterns only contain a handful of
  127. wildcards at most.
  128. .UNINDENT
  129. .UNINDENT
  130. .sp
  131. Exclusions can be passed via the command line option \fB\-\-exclude\fP\&. When used
  132. from within a shell the patterns should be quoted to protect them from
  133. expansion.
  134. .sp
  135. The \fB\-\-exclude\-from\fP option permits loading exclusion patterns from a text
  136. file with one pattern per line. Lines empty or starting with the number sign
  137. (\(aq#\(aq) after removing whitespace on both ends are ignored. The optional style
  138. selector prefix is also supported for patterns loaded from a file. Due to
  139. whitespace removal paths with whitespace at the beginning or end can only be
  140. excluded using regular expressions.
  141. .sp
  142. To test your exclusion patterns without performing an actual backup you can
  143. run \fBborg create \-\-list \-\-dry\-run ...\fP\&.
  144. .sp
  145. Examples:
  146. .INDENT 0.0
  147. .INDENT 3.5
  148. .sp
  149. .nf
  150. .ft C
  151. # Exclude \(aq/home/user/file.o\(aq but not \(aq/home/user/file.odt\(aq:
  152. $ borg create \-e \(aq*.o\(aq backup /
  153. # Exclude \(aq/home/user/junk\(aq and \(aq/home/user/subdir/junk\(aq but
  154. # not \(aq/home/user/importantjunk\(aq or \(aq/etc/junk\(aq:
  155. $ borg create \-e \(aq/home/*/junk\(aq backup /
  156. # Exclude the contents of \(aq/home/user/cache\(aq but not the directory itself:
  157. $ borg create \-e home/user/cache/ backup /
  158. # The file \(aq/home/user/cache/important\(aq is *not* backed up:
  159. $ borg create \-e /home/user/cache/ backup / /home/user/cache/important
  160. # The contents of directories in \(aq/home\(aq are not backed up when their name
  161. # ends in \(aq.tmp\(aq
  162. $ borg create \-\-exclude \(aqre:^/home/[^/]+\e.tmp/\(aq backup /
  163. # Load exclusions from file
  164. $ cat >exclude.txt <<EOF
  165. # Comment line
  166. /home/*/junk
  167. *.tmp
  168. fm:aa:something/*
  169. re:^home/[^/]\e.tmp/
  170. sh:home/*/.thumbnails
  171. # Example with spaces, no need to escape as it is processed by borg
  172. some file with spaces.txt
  173. EOF
  174. $ borg create \-\-exclude\-from exclude.txt backup /
  175. .ft P
  176. .fi
  177. .UNINDENT
  178. .UNINDENT
  179. A more general and easier to use way to define filename matching patterns exists
  180. with the experimental \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP options. Using these, you
  181. may specify the backup roots (starting points) and patterns for inclusion/exclusion.
  182. A root path starts with the prefix \fIR\fP, followed by a path (a plain path, not a
  183. file pattern). An include rule starts with the prefix +, an exclude rule starts
  184. with the prefix \-, an exclude\-norecurse rule starts with !, all followed by a pattern.
  185. .sp
  186. \fBNOTE:\fP
  187. .INDENT 0.0
  188. .INDENT 3.5
  189. Via \fB\-\-pattern\fP or \fB\-\-patterns\-from\fP you can define BOTH inclusion and exclusion
  190. of files using pattern prefixes \fB+\fP and \fB\-\fP\&. With \fB\-\-exclude\fP and
  191. \fB\-\-exlude\-from\fP ONLY excludes are defined.
  192. .UNINDENT
  193. .UNINDENT
  194. .sp
  195. Inclusion patterns are useful to include paths that are contained in an excluded
  196. path. The first matching pattern is used so if an include pattern matches before
  197. an exclude pattern, the file is backed up. If an exclude\-norecurse pattern matches
  198. a directory, it won\(aqt recurse into it and won\(aqt discover any potential matches for
  199. include rules below that directory.
  200. .sp
  201. Note that the default pattern style for \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP is
  202. shell style (\fIsh:\fP), so those patterns behave similar to rsync include/exclude
  203. patterns. The pattern style can be set via the \fIP\fP prefix.
  204. .sp
  205. Patterns (\fB\-\-pattern\fP) and excludes (\fB\-\-exclude\fP) from the command line are
  206. considered first (in the order of appearance). Then patterns from \fB\-\-patterns\-from\fP
  207. are added. Exclusion patterns from \fB\-\-exclude\-from\fP files are appended last.
  208. .sp
  209. Examples:
  210. .INDENT 0.0
  211. .INDENT 3.5
  212. .sp
  213. .nf
  214. .ft C
  215. # backup pics, but not the ones from 2018, except the good ones:
  216. # note: using = is essential to avoid cmdline argument parsing issues.
  217. borg create \-\-pattern=+pics/2018/good \-\-pattern=\-pics/2018 repo::arch pics
  218. # use a file with patterns:
  219. borg create \-\-patterns\-from patterns.lst repo::arch
  220. .ft P
  221. .fi
  222. .UNINDENT
  223. .UNINDENT
  224. .sp
  225. The patterns.lst file could look like that:
  226. .INDENT 0.0
  227. .INDENT 3.5
  228. .sp
  229. .nf
  230. .ft C
  231. # "sh:" pattern style is the default, so the following line is not needed:
  232. P sh
  233. R /
  234. # can be rebuild
  235. \- /home/*/.cache
  236. # they\(aqre downloads for a reason
  237. \- /home/*/Downloads
  238. # susan is a nice person
  239. # include susans home
  240. + /home/susan
  241. # don\(aqt backup the other home directories
  242. \- /home/*
  243. # don\(aqt even look in /proc
  244. ! /proc
  245. .ft P
  246. .fi
  247. .UNINDENT
  248. .UNINDENT
  249. .SH AUTHOR
  250. The Borg Collective
  251. .\" Generated by docutils manpage writer.
  252. .