borg-patterns.1 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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" "2025-05-19" "" "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 start with \fBabsolute/\fP\&.
  60. If you give \fB\&../../relative\fP as root, the paths will be normalized
  61. as \fBrelative/\fP\&.
  62. .sp
  63. Borg supports different pattern styles. To define a non\-default
  64. style for a specific pattern, prefix it with two characters followed
  65. by a colon \(aq:\(aq (i.e. \fBfm:path/*\fP, \fBsh:path/**\fP).
  66. .INDENT 0.0
  67. .TP
  68. .B Fnmatch <https://docs.python.org/3/library/fnmatch.html>
  69. , selector \fBfm:\fP
  70. This is the default style for \fB\-\-exclude\fP and \fB\-\-exclude\-from\fP\&.
  71. These patterns use a variant of shell pattern syntax, with \(aq*\(aq matching
  72. any number of characters, \(aq?\(aq matching any single character, \(aq[...]\(aq
  73. matching any single character specified, including ranges, and \(aq[!...]\(aq
  74. matching any character not specified. For the purpose of these patterns,
  75. the path separator (backslash for Windows and \(aq/\(aq on other systems) is not
  76. treated specially. Wrap meta\-characters in brackets for a literal
  77. match (i.e. \fB[?]\fP to match the literal character \(aq?\(aq). For a path
  78. to match a pattern, the full path must match, or it must match
  79. from the start of the full path to just before a path separator. Except
  80. for the root path, paths will never end in the path separator when
  81. matching is attempted. Thus, if a given pattern ends in a path
  82. separator, a \(aq*\(aq is appended before matching is attempted. A leading
  83. path separator is always removed.
  84. .TP
  85. .B Shell\-style patterns, selector \fBsh:\fP
  86. This is the default style for \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP\&.
  87. Like fnmatch patterns these are similar to shell patterns. The difference
  88. is that the pattern may include \fB**/\fP for matching zero or more directory
  89. levels, \fB*\fP for matching zero or more arbitrary characters with the
  90. exception of any path separator, \fB{}\fP containing comma\-separated
  91. alternative patterns. A leading path separator is always removed.
  92. .TP
  93. .B Regular expressions <https://docs.python.org/3/library/re.html>
  94. , selector \fBre:\fP
  95. Unlike shell patterns, regular expressions are not required to match the full
  96. path and any substring match is sufficient. It is strongly recommended to
  97. anchor patterns to the start (\(aq^\(aq), to the end (\(aq$\(aq) or both. Path
  98. separators (backslash for Windows and \(aq/\(aq on other systems) in paths are
  99. always normalized to a forward slash \(aq/\(aq before applying a pattern.
  100. .TP
  101. .B Path prefix, selector \fBpp:\fP
  102. This pattern style is useful to match whole sub\-directories. The pattern
  103. \fBpp:root/somedir\fP matches \fBroot/somedir\fP and everything therein.
  104. A leading path separator is always removed.
  105. .TP
  106. .B Path full\-match, selector \fBpf:\fP
  107. This pattern style is (only) useful to match full paths.
  108. This is kind of a pseudo pattern as it can not have any variable or
  109. unspecified parts \- the full path must be given. \fBpf:root/file.ext\fP
  110. matches \fBroot/file.ext\fP only. A leading path separator is always
  111. removed.
  112. .sp
  113. Implementation note: this is implemented via very time\-efficient O(1)
  114. hashtable lookups (this means you can have huge amounts of such patterns
  115. without impacting performance much).
  116. Due to that, this kind of pattern does not respect any context or order.
  117. If you use such a pattern to include a file, it will always be included
  118. (if the directory recursion encounters it).
  119. Other include/exclude patterns that would normally match will be ignored.
  120. Same logic applies for exclude.
  121. .UNINDENT
  122. .sp
  123. \fBNOTE:\fP
  124. .INDENT 0.0
  125. .INDENT 3.5
  126. \fBre:\fP, \fBsh:\fP and \fBfm:\fP patterns are all implemented on top of
  127. the Python SRE engine. It is very easy to formulate patterns for each
  128. of these types which requires an inordinate amount of time to match
  129. paths. If untrusted users are able to supply patterns, ensure they
  130. cannot supply \fBre:\fP patterns. Further, ensure that \fBsh:\fP and
  131. \fBfm:\fP patterns only contain a handful of wildcards at most.
  132. .UNINDENT
  133. .UNINDENT
  134. .sp
  135. Exclusions can be passed via the command line option \fB\-\-exclude\fP\&. When used
  136. from within a shell, the patterns should be quoted to protect them from
  137. expansion.
  138. .sp
  139. Patterns matching special characters, e.g. white space, within a shell may
  140. require adjustments, such as putting quotation marks around the arguments.
  141. Example:
  142. Using bash, the following command line option would match and exclude \(dqitem name\(dq:
  143. \fB\-\-pattern=\(aq\-path/item name\(aq\fP
  144. Note that when patterns are used within a pattern file directly read by borg,
  145. e.g. when using \fB\-\-exclude\-from\fP or \fB\-\-patterns\-from\fP, there is no shell
  146. involved and thus no quotation marks are required.
  147. .sp
  148. The \fB\-\-exclude\-from\fP option permits loading exclusion patterns from a text
  149. file with one pattern per line. Lines empty or starting with the hash sign
  150. \(aq#\(aq after removing whitespace on both ends are ignored. The optional style
  151. selector prefix is also supported for patterns loaded from a file. Due to
  152. whitespace removal, paths with whitespace at the beginning or end can only be
  153. excluded using regular expressions.
  154. .sp
  155. To test your exclusion patterns without performing an actual backup you can
  156. run \fBborg create \-\-list \-\-dry\-run ...\fP\&.
  157. .sp
  158. Examples:
  159. .INDENT 0.0
  160. .INDENT 3.5
  161. .sp
  162. .EX
  163. # Exclude \(aq/home/user/file.o\(aq but not \(aq/home/user/file.odt\(aq:
  164. $ borg create \-e \(aq*.o\(aq archive /
  165. # Exclude \(aq/home/user/junk\(aq and \(aq/home/user/subdir/junk\(aq but
  166. # not \(aq/home/user/importantjunk\(aq or \(aq/etc/junk\(aq:
  167. $ borg create \-e \(aqhome/*/junk\(aq archive /
  168. # Exclude the contents of \(aq/home/user/cache\(aq but not the directory itself:
  169. $ borg create \-e home/user/cache/ archive /
  170. # The file \(aq/home/user/cache/important\(aq is *not* backed up:
  171. $ borg create \-e home/user/cache/ archive / /home/user/cache/important
  172. # The contents of directories in \(aq/home\(aq are not backed up when their name
  173. # ends in \(aq.tmp\(aq
  174. $ borg create \-\-exclude \(aqre:^home/[^/]+\e.tmp/\(aq archive /
  175. # Load exclusions from file
  176. $ cat >exclude.txt <<EOF
  177. # Comment line
  178. home/*/junk
  179. *.tmp
  180. fm:aa:something/*
  181. re:^home/[^/]+\e.tmp/
  182. sh:home/*/.thumbnails
  183. # Example with spaces, no need to escape as it is processed by borg
  184. some file with spaces.txt
  185. EOF
  186. $ borg create \-\-exclude\-from exclude.txt archive /
  187. .EE
  188. .UNINDENT
  189. .UNINDENT
  190. .sp
  191. A more general and easier to use way to define filename matching patterns
  192. exists with the \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP options. Using
  193. these, you may specify the backup roots, default pattern styles and
  194. patterns for inclusion and exclusion.
  195. .INDENT 0.0
  196. .TP
  197. .B Root path prefix \fBR\fP
  198. A recursion root path starts with the prefix \fBR\fP, followed by a path
  199. (a plain path, not a file pattern). Use this prefix to have the root
  200. paths in the patterns file rather than as command line arguments.
  201. .TP
  202. .B Pattern style prefix \fBP\fP
  203. To change the default pattern style, use the \fBP\fP prefix, followed by
  204. the pattern style abbreviation (\fBfm\fP, \fBpf\fP, \fBpp\fP, \fBre\fP, \fBsh\fP).
  205. All patterns following this line will use this style until another style
  206. is specified.
  207. .TP
  208. .B Exclude pattern prefix \fB\-\fP
  209. Use the prefix \fB\-\fP, followed by a pattern, to define an exclusion.
  210. This has the same effect as the \fB\-\-exclude\fP option.
  211. .TP
  212. .B Exclude no\-recurse pattern prefix \fB!\fP
  213. Use the prefix \fB!\fP, followed by a pattern, to define an exclusion
  214. that does not recurse into subdirectories. This saves time, but
  215. prevents include patterns to match any files in subdirectories.
  216. .TP
  217. .B Include pattern prefix \fB+\fP
  218. Use the prefix \fB+\fP, followed by a pattern, to define inclusions.
  219. This is useful to include paths that are covered in an exclude
  220. pattern and would otherwise not be backed up.
  221. .UNINDENT
  222. .sp
  223. The first matching pattern is used, so if an include pattern matches
  224. before an exclude pattern, the file is backed up. Note that a no\-recurse
  225. exclude stops examination of subdirectories so that potential includes
  226. will not match \- use normal excludes for such use cases.
  227. .sp
  228. Example:
  229. .INDENT 0.0
  230. .INDENT 3.5
  231. .sp
  232. .EX
  233. # Define the recursion root
  234. R /
  235. # Exclude all iso files in any directory
  236. \- **/*.iso
  237. # Explicitly include all inside etc and root
  238. + etc/**
  239. + root/**
  240. # Exclude a specific directory under each user\(aqs home directories
  241. \- home/*/.cache
  242. # Explicitly include everything in /home
  243. + home/**
  244. # Explicitly exclude some directories without recursing into them
  245. ! re:^(dev|proc|run|sys|tmp)
  246. # Exclude all other files and directories
  247. # that are not specifically included earlier.
  248. \- **
  249. .EE
  250. .UNINDENT
  251. .UNINDENT
  252. .sp
  253. \fBTip: You can easily test your patterns with \-\-dry\-run and \-\-list\fP:
  254. .INDENT 0.0
  255. .INDENT 3.5
  256. .sp
  257. .EX
  258. $ borg create \-\-dry\-run \-\-list \-\-patterns\-from patterns.txt archive
  259. .EE
  260. .UNINDENT
  261. .UNINDENT
  262. .sp
  263. This will list the considered files one per line, prefixed with a
  264. character that indicates the action (e.g. \(aqx\(aq for excluding, see
  265. \fBItem flags\fP in \fIborg create\fP usage docs).
  266. .sp
  267. \fBNOTE:\fP
  268. .INDENT 0.0
  269. .INDENT 3.5
  270. It\(aqs possible that a sub\-directory/file is matched while parent
  271. directories are not. In that case, parent directories are not backed
  272. up and thus their user, group, permission, etc. cannot be restored.
  273. .UNINDENT
  274. .UNINDENT
  275. .sp
  276. Patterns (\fB\-\-pattern\fP) and excludes (\fB\-\-exclude\fP) from the command line are
  277. considered first (in the order of appearance). Then patterns from \fB\-\-patterns\-from\fP
  278. are added. Exclusion patterns from \fB\-\-exclude\-from\fP files are appended last.
  279. .sp
  280. Examples:
  281. .INDENT 0.0
  282. .INDENT 3.5
  283. .sp
  284. .EX
  285. # back up pics, but not the ones from 2018, except the good ones:
  286. # note: using = is essential to avoid cmdline argument parsing issues.
  287. borg create \-\-pattern=+pics/2018/good \-\-pattern=\-pics/2018 archive pics
  288. # back up only JPG/JPEG files (case insensitive) in all home directories:
  289. borg create \-\-pattern \(aq+ re:\e.jpe?g(?i)$\(aq archive /home
  290. # back up homes, but exclude big downloads (like .ISO files) or hidden files:
  291. borg create \-\-exclude \(aqre:\e.iso(?i)$\(aq \-\-exclude \(aqsh:home/**/.*\(aq archive /home
  292. # use a file with patterns (recursion root \(aq/\(aq via command line):
  293. borg create \-\-patterns\-from patterns.lst archive /
  294. .EE
  295. .UNINDENT
  296. .UNINDENT
  297. .sp
  298. The patterns.lst file could look like that:
  299. .INDENT 0.0
  300. .INDENT 3.5
  301. .sp
  302. .EX
  303. # \(dqsh:\(dq pattern style is the default
  304. # exclude caches
  305. \- home/*/.cache
  306. # include susans home
  307. + home/susan
  308. # also back up this exact file
  309. + pf:home/bobby/specialfile.txt
  310. # don\(aqt back up the other home directories
  311. \- home/*
  312. # don\(aqt even look in /dev, /proc, /run, /sys, /tmp (note: would exclude files like /device, too)
  313. ! re:^(dev|proc|run|sys|tmp)
  314. .EE
  315. .UNINDENT
  316. .UNINDENT
  317. .sp
  318. You can specify recursion roots either on the command line or in a patternfile:
  319. .INDENT 0.0
  320. .INDENT 3.5
  321. .sp
  322. .EX
  323. # these two commands do the same thing
  324. borg create \-\-exclude home/bobby/junk archive /home/bobby /home/susan
  325. borg create \-\-patterns\-from patternfile.lst archive
  326. .EE
  327. .UNINDENT
  328. .UNINDENT
  329. .sp
  330. patternfile.lst:
  331. .INDENT 0.0
  332. .INDENT 3.5
  333. .sp
  334. .EX
  335. # note that excludes use fm: by default and patternfiles use sh: by default.
  336. # therefore, we need to specify fm: to have the same exact behavior.
  337. P fm
  338. R /home/bobby
  339. R /home/susan
  340. \- home/bobby/junk
  341. .EE
  342. .UNINDENT
  343. .UNINDENT
  344. .sp
  345. This allows you to share the same patterns between multiple repositories
  346. without needing to specify them on the command line.
  347. .SH AUTHOR
  348. The Borg Collective
  349. .\" Generated by docutils manpage writer.
  350. .