borg-patterns.1 13 KB

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