borg-patterns.1 12 KB

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