borg-patterns.1 14 KB

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