borg-patterns.1 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. .\" Man page generated from reStructuredText.
  2. .
  3. .
  4. .nr rst2man-indent-level 0
  5. .
  6. .de1 rstReportMargin
  7. \\$1 \\n[an-margin]
  8. level \\n[rst2man-indent-level]
  9. level margin: \\n[rst2man-indent\\n[rst2man-indent-level]]
  10. -
  11. \\n[rst2man-indent0]
  12. \\n[rst2man-indent1]
  13. \\n[rst2man-indent2]
  14. ..
  15. .de1 INDENT
  16. .\" .rstReportMargin pre:
  17. . RS \\$1
  18. . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin]
  19. . nr rst2man-indent-level +1
  20. .\" .rstReportMargin post:
  21. ..
  22. .de UNINDENT
  23. . RE
  24. .\" indent \\n[an-margin]
  25. .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]]
  26. .nr rst2man-indent-level -1
  27. .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]]
  28. .in \\n[rst2man-indent\\n[rst2man-indent-level]]u
  29. ..
  30. .TH "BORG-PATTERNS" 1 "2022-06-25" "" "borg backup tool"
  31. .SH NAME
  32. borg-patterns \- Details regarding patterns
  33. .SH DESCRIPTION
  34. .sp
  35. When specifying one or more file paths in a Borg command that supports
  36. patterns for the respective option or argument, you can apply the
  37. patterns described here to include only desired files and/or exclude
  38. unwanted ones. Patterns can be used
  39. .INDENT 0.0
  40. .IP \(bu 2
  41. for \fB\-\-exclude\fP option,
  42. .IP \(bu 2
  43. in the file given with \fB\-\-exclude\-from\fP option,
  44. .IP \(bu 2
  45. for \fB\-\-pattern\fP option,
  46. .IP \(bu 2
  47. in the file given with \fB\-\-patterns\-from\fP option and
  48. .IP \(bu 2
  49. for \fBPATH\fP arguments that explicitly support them.
  50. .UNINDENT
  51. .sp
  52. Borg always stores all file paths normalized and relative to the
  53. current recursion root. The recursion root is also named \fBPATH\fP in
  54. Borg commands like \fIborg create\fP that do a file discovery, so do not
  55. confuse the root with the \fBPATH\fP argument of e.g. \fIborg extract\fP\&.
  56. .sp
  57. Starting with Borg 1.2, paths that are matched against patterns always
  58. appear relative. If you give \fB/absolute/\fP as root, the paths going
  59. into the matcher will look relative like \fBabsolute/.../file.ext\fP\&.
  60. If you give \fB\&../some/path\fP as root, the paths will look like
  61. \fBsome/path/.../file.ext\fP\&.
  62. .sp
  63. File patterns support five different styles. If followed by a colon \(aq:\(aq,
  64. the first two characters of a pattern are used as a style selector.
  65. Explicit style selection is necessary if a non\-default style is desired
  66. or when the desired pattern starts with two alphanumeric characters
  67. followed by a colon (i.e. \fBaa:something/*\fP).
  68. .INDENT 0.0
  69. .TP
  70. .B \fI\%Fnmatch\fP, selector \fBfm:\fP
  71. This is the default style for \fB\-\-exclude\fP and \fB\-\-exclude\-from\fP\&.
  72. These patterns use a variant of shell pattern syntax, with \(aq*\(aq matching
  73. any number of characters, \(aq?\(aq matching any single character, \(aq[...]\(aq
  74. matching any single character specified, including ranges, and \(aq[!...]\(aq
  75. matching any character not specified. For the purpose of these patterns,
  76. the path separator (backslash for Windows and \(aq/\(aq on other systems) is not
  77. treated specially. Wrap meta\-characters in brackets for a literal
  78. match (i.e. \fB[?]\fP to match the literal character \(aq?\(aq). For a path
  79. to match a pattern, the full path must match, or it must match
  80. from the start of the full path to just before a path separator. Except
  81. for the root path, paths will never end in the path separator when
  82. matching is attempted. Thus, if a given pattern ends in a path
  83. separator, a \(aq*\(aq is appended before matching is attempted. A leading
  84. path separator is always removed.
  85. .TP
  86. .B Shell\-style patterns, selector \fBsh:\fP
  87. This is the default style for \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP\&.
  88. Like fnmatch patterns these are similar to shell patterns. The difference
  89. is that the pattern may include \fB**/\fP for matching zero or more directory
  90. levels, \fB*\fP for matching zero or more arbitrary characters with the
  91. exception of any path separator. A leading path separator is always removed.
  92. .TP
  93. .B \fI\%Regular expressions\fP, selector \fBre:\fP
  94. Unlike shell patterns, regular expressions are not required to match the full
  95. path and any substring match is sufficient. It is strongly recommended to
  96. anchor patterns to the start (\(aq^\(aq), to the end (\(aq$\(aq) or both. Path
  97. separators (backslash for Windows and \(aq/\(aq on other systems) in paths are
  98. always normalized to a forward slash \(aq/\(aq before applying a pattern.
  99. .TP
  100. .B Path prefix, selector \fBpp:\fP
  101. This pattern style is useful to match whole sub\-directories. The pattern
  102. \fBpp:root/somedir\fP matches \fBroot/somedir\fP and everything therein.
  103. A leading path separator is always removed.
  104. .TP
  105. .B Path full\-match, selector \fBpf:\fP
  106. This pattern style is (only) useful to match full paths.
  107. This is kind of a pseudo pattern as it can not have any variable or
  108. unspecified parts \- the full path must be given. \fBpf:root/file.ext\fP
  109. matches \fBroot/file.ext\fP only. A leading path separator is always
  110. removed.
  111. .sp
  112. Implementation note: this is implemented via very time\-efficient O(1)
  113. hashtable lookups (this means you can have huge amounts of such patterns
  114. without impacting performance much).
  115. Due to that, this kind of pattern does not respect any context or order.
  116. If you use such a pattern to include a file, it will always be included
  117. (if the directory recursion encounters it).
  118. Other include/exclude patterns that would normally match will be ignored.
  119. Same logic applies for exclude.
  120. .UNINDENT
  121. .sp
  122. \fBNOTE:\fP
  123. .INDENT 0.0
  124. .INDENT 3.5
  125. \fBre:\fP, \fBsh:\fP and \fBfm:\fP patterns are all implemented on top of
  126. the Python SRE engine. It is very easy to formulate patterns for each
  127. of these types which requires an inordinate amount of time to match
  128. paths. If untrusted users are able to supply patterns, ensure they
  129. cannot supply \fBre:\fP patterns. Further, ensure that \fBsh:\fP and
  130. \fBfm:\fP patterns only contain a handful of wildcards at most.
  131. .UNINDENT
  132. .UNINDENT
  133. .sp
  134. Exclusions can be passed via the command line option \fB\-\-exclude\fP\&. When used
  135. from within a shell, the patterns should be quoted to protect them from
  136. expansion.
  137. .sp
  138. The \fB\-\-exclude\-from\fP option permits loading exclusion patterns from a text
  139. file with one pattern per line. Lines empty or starting with the hash sign
  140. \(aq#\(aq after removing whitespace on both ends are ignored. The optional style
  141. selector prefix is also supported for patterns loaded from a file. Due to
  142. whitespace removal, paths with whitespace at the beginning or end can only be
  143. excluded using regular expressions.
  144. .sp
  145. To test your exclusion patterns without performing an actual backup you can
  146. run \fBborg create \-\-list \-\-dry\-run ...\fP\&.
  147. .sp
  148. Examples:
  149. .INDENT 0.0
  150. .INDENT 3.5
  151. .sp
  152. .nf
  153. .ft C
  154. # Exclude \(aq/home/user/file.o\(aq but not \(aq/home/user/file.odt\(aq:
  155. $ borg create \-e \(aq*.o\(aq archive /
  156. # Exclude \(aq/home/user/junk\(aq and \(aq/home/user/subdir/junk\(aq but
  157. # not \(aq/home/user/importantjunk\(aq or \(aq/etc/junk\(aq:
  158. $ borg create \-e \(aqhome/*/junk\(aq archive /
  159. # Exclude the contents of \(aq/home/user/cache\(aq but not the directory itself:
  160. $ borg create \-e home/user/cache/ archive /
  161. # The file \(aq/home/user/cache/important\(aq is *not* backed up:
  162. $ borg create \-e home/user/cache/ archive / /home/user/cache/important
  163. # The contents of directories in \(aq/home\(aq are not backed up when their name
  164. # ends in \(aq.tmp\(aq
  165. $ borg create \-\-exclude \(aqre:^home/[^/]+\e.tmp/\(aq archive /
  166. # Load exclusions from file
  167. $ cat >exclude.txt <<EOF
  168. # Comment line
  169. home/*/junk
  170. *.tmp
  171. fm:aa:something/*
  172. re:^home/[^/]+\e.tmp/
  173. sh:home/*/.thumbnails
  174. # Example with spaces, no need to escape as it is processed by borg
  175. some file with spaces.txt
  176. EOF
  177. $ borg create \-\-exclude\-from exclude.txt archive /
  178. .ft P
  179. .fi
  180. .UNINDENT
  181. .UNINDENT
  182. .sp
  183. A more general and easier to use way to define filename matching patterns
  184. exists with the \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP options. Using
  185. these, you may specify the backup roots, default pattern styles and
  186. patterns for inclusion and exclusion.
  187. .INDENT 0.0
  188. .TP
  189. .B Root path prefix \fBR\fP
  190. A recursion root path starts with the prefix \fBR\fP, followed by a path
  191. (a plain path, not a file pattern). Use this prefix to have the root
  192. paths in the patterns file rather than as command line arguments.
  193. .TP
  194. .B Pattern style prefix \fBP\fP
  195. To change the default pattern style, use the \fBP\fP prefix, followed by
  196. the pattern style abbreviation (\fBfm\fP, \fBpf\fP, \fBpp\fP, \fBre\fP, \fBsh\fP).
  197. All patterns following this line will use this style until another style
  198. is specified.
  199. .TP
  200. .B Exclude pattern prefix \fB\-\fP
  201. Use the prefix \fB\-\fP, followed by a pattern, to define an exclusion.
  202. This has the same effect as the \fB\-\-exclude\fP option.
  203. .TP
  204. .B Exclude no\-recurse pattern prefix \fB!\fP
  205. Use the prefix \fB!\fP, followed by a pattern, to define an exclusion
  206. that does not recurse into subdirectories. This saves time, but
  207. prevents include patterns to match any files in subdirectories.
  208. .TP
  209. .B Include pattern prefix \fB+\fP
  210. Use the prefix \fB+\fP, followed by a pattern, to define inclusions.
  211. This is useful to include paths that are covered in an exclude
  212. pattern and would otherwise not be backed up.
  213. .UNINDENT
  214. .sp
  215. The first matching pattern is used, so if an include pattern matches
  216. before an exclude pattern, the file is backed up. Note that a no\-recurse
  217. exclude stops examination of subdirectories so that potential includes
  218. will not match \- use normal exludes for such use cases.
  219. .sp
  220. \fBTip: You can easily test your patterns with \-\-dry\-run and \-\-list\fP:
  221. .INDENT 0.0
  222. .INDENT 3.5
  223. .sp
  224. .nf
  225. .ft C
  226. $ borg create \-\-dry\-run \-\-list \-\-patterns\-from patterns.txt archive
  227. .ft P
  228. .fi
  229. .UNINDENT
  230. .UNINDENT
  231. .sp
  232. This will list the considered files one per line, prefixed with a
  233. character that indicates the action (e.g. \(aqx\(aq for excluding, see
  234. \fBItem flags\fP in \fIborg create\fP usage docs).
  235. .sp
  236. \fBNOTE:\fP
  237. .INDENT 0.0
  238. .INDENT 3.5
  239. It\(aqs possible that a sub\-directory/file is matched while parent
  240. directories are not. In that case, parent directories are not backed
  241. up and thus their user, group, permission, etc. cannot be restored.
  242. .UNINDENT
  243. .UNINDENT
  244. .sp
  245. Patterns (\fB\-\-pattern\fP) and excludes (\fB\-\-exclude\fP) from the command line are
  246. considered first (in the order of appearance). Then patterns from \fB\-\-patterns\-from\fP
  247. are added. Exclusion patterns from \fB\-\-exclude\-from\fP files are appended last.
  248. .sp
  249. Examples:
  250. .INDENT 0.0
  251. .INDENT 3.5
  252. .sp
  253. .nf
  254. .ft C
  255. # backup pics, but not the ones from 2018, except the good ones:
  256. # note: using = is essential to avoid cmdline argument parsing issues.
  257. borg create \-\-pattern=+pics/2018/good \-\-pattern=\-pics/2018 archive pics
  258. # backup only JPG/JPEG files (case insensitive) in all home directories:
  259. borg create \-\-pattern \(aq+ re:\e.jpe?g(?i)$\(aq archive /home
  260. # backup homes, but exclude big downloads (like .ISO files) or hidden files:
  261. borg create \-\-exclude \(aqre:\e.iso(?i)$\(aq \-\-exclude \(aqsh:home/**/.*\(aq archive /home
  262. # use a file with patterns (recursion root \(aq/\(aq via command line):
  263. borg create \-\-patterns\-from patterns.lst archive /
  264. .ft P
  265. .fi
  266. .UNINDENT
  267. .UNINDENT
  268. .sp
  269. The patterns.lst file could look like that:
  270. .INDENT 0.0
  271. .INDENT 3.5
  272. .sp
  273. .nf
  274. .ft C
  275. # "sh:" pattern style is the default
  276. # exclude caches
  277. \- home/*/.cache
  278. # include susans home
  279. + home/susan
  280. # also back up this exact file
  281. + pf:home/bobby/specialfile.txt
  282. # don\(aqt backup the other home directories
  283. \- home/*
  284. # don\(aqt even look in /dev, /proc, /run, /sys, /tmp (note: would exclude files like /device, too)
  285. ! re:^(dev|proc|run|sys|tmp)
  286. .ft P
  287. .fi
  288. .UNINDENT
  289. .UNINDENT
  290. .sp
  291. You can specify recursion roots either on the command line or in a patternfile:
  292. .INDENT 0.0
  293. .INDENT 3.5
  294. .sp
  295. .nf
  296. .ft C
  297. # these two commands do the same thing
  298. borg create \-\-exclude home/bobby/junk archive /home/bobby /home/susan
  299. borg create \-\-patterns\-from patternfile.lst archive
  300. .ft P
  301. .fi
  302. .UNINDENT
  303. .UNINDENT
  304. .sp
  305. patternfile.lst:
  306. .INDENT 0.0
  307. .INDENT 3.5
  308. .sp
  309. .nf
  310. .ft C
  311. # note that excludes use fm: by default and patternfiles use sh: by default.
  312. # therefore, we need to specify fm: to have the same exact behavior.
  313. P fm
  314. R /home/bobby
  315. R /home/susan
  316. \- home/bobby/junk
  317. .ft P
  318. .fi
  319. .UNINDENT
  320. .UNINDENT
  321. .sp
  322. This allows you to share the same patterns between multiple repositories
  323. without needing to specify them on the command line.
  324. .SH AUTHOR
  325. The Borg Collective
  326. .\" Generated by docutils manpage writer.
  327. .