borg-patterns.1 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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-19" "" "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. The \fB\-\-exclude\-from\fP option permits loading exclusion patterns from a text
  128. file with one pattern per line. Lines empty or starting with the number sign
  129. (\(aq#\(aq) after removing whitespace on both ends are ignored. The optional style
  130. selector prefix is also supported for patterns loaded from a file. Due to
  131. whitespace removal, paths with whitespace at the beginning or end can only be
  132. excluded using regular expressions.
  133. .sp
  134. To test your exclusion patterns without performing an actual backup you can
  135. run \fBborg create \-\-list \-\-dry\-run ...\fP\&.
  136. .sp
  137. Examples:
  138. .INDENT 0.0
  139. .INDENT 3.5
  140. .sp
  141. .EX
  142. # Exclude \(aq/home/user/file.o\(aq but not \(aq/home/user/file.odt\(aq:
  143. $ borg create \-e \(aq*.o\(aq backup /
  144. # Exclude \(aq/home/user/junk\(aq and \(aq/home/user/subdir/junk\(aq but
  145. # not \(aq/home/user/importantjunk\(aq or \(aq/etc/junk\(aq:
  146. $ borg create \-e \(aqhome/*/junk\(aq backup /
  147. # Exclude the contents of \(aq/home/user/cache\(aq but not the directory itself:
  148. $ borg create \-e home/user/cache/ backup /
  149. # The file \(aq/home/user/cache/important\(aq is *not* backed up:
  150. $ borg create \-e home/user/cache/ backup / /home/user/cache/important
  151. # The contents of directories in \(aq/home\(aq are not backed up when their name
  152. # ends in \(aq.tmp\(aq
  153. $ borg create \-\-exclude \(aqre:^home/[^/]+\e.tmp/\(aq backup /
  154. # Load exclusions from file
  155. $ cat >exclude.txt <<EOF
  156. # Comment line
  157. home/*/junk
  158. *.tmp
  159. fm:aa:something/*
  160. re:^home/[^/]+\e.tmp/
  161. sh:home/*/.thumbnails
  162. # Example with spaces, no need to escape as it is processed by borg
  163. some file with spaces.txt
  164. EOF
  165. $ borg create \-\-exclude\-from exclude.txt backup /
  166. .EE
  167. .UNINDENT
  168. .UNINDENT
  169. .sp
  170. A more general and easier to use way to define filename matching patterns
  171. exists with the \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP options. Using
  172. these, you may specify the backup roots, default pattern styles and
  173. patterns for inclusion and exclusion.
  174. .INDENT 0.0
  175. .TP
  176. .B Root path prefix \fBR\fP
  177. A recursion root path starts with the prefix \fBR\fP, followed by a path
  178. (a plain path, not a file pattern). Use this prefix to have the root
  179. paths in the patterns file rather than as command line arguments.
  180. .TP
  181. .B Pattern style prefix \fBP\fP
  182. To change the default pattern style, use the \fBP\fP prefix, followed by
  183. the pattern style abbreviation (\fBfm\fP, \fBpf\fP, \fBpp\fP, \fBre\fP, \fBsh\fP).
  184. All patterns following this line will use this style until another style
  185. is specified.
  186. .TP
  187. .B Exclude pattern prefix \fB\-\fP
  188. Use the prefix \fB\-\fP, followed by a pattern, to define an exclusion.
  189. This has the same effect as the \fB\-\-exclude\fP option.
  190. .TP
  191. .B Exclude no\-recurse pattern prefix \fB!\fP
  192. Use the prefix \fB!\fP, followed by a pattern, to define an exclusion
  193. that does not recurse into subdirectories. This saves time, but
  194. prevents include patterns to match any files in subdirectories.
  195. .TP
  196. .B Include pattern prefix \fB+\fP
  197. Use the prefix \fB+\fP, followed by a pattern, to define inclusions.
  198. This is useful to include paths that are covered in an exclude
  199. pattern and would otherwise not be backed up.
  200. .UNINDENT
  201. .sp
  202. \fBNOTE:\fP
  203. .INDENT 0.0
  204. .INDENT 3.5
  205. Via \fB\-\-pattern\fP or \fB\-\-patterns\-from\fP you can define BOTH inclusion and exclusion
  206. of files using pattern prefixes \fB+\fP and \fB\-\fP\&. With \fB\-\-exclude\fP and
  207. \fB\-\-exclude\-from\fP ONLY excludes are defined.
  208. .UNINDENT
  209. .UNINDENT
  210. .sp
  211. The first matching pattern is used, so if an include pattern matches
  212. before an exclude pattern, the file is backed up. Note that a no\-recurse
  213. exclude stops examination of subdirectories so that potential includes
  214. will not match \- use normal excludes for such use cases.
  215. .sp
  216. Example:
  217. .INDENT 0.0
  218. .INDENT 3.5
  219. .sp
  220. .EX
  221. # Define the recursion root
  222. R /
  223. # Exclude all iso files in any directory
  224. \- **/*.iso
  225. # Explicitly include all inside etc and root
  226. + etc/**
  227. + root/**
  228. # Exclude a specific directory under each user\(aqs home directories
  229. \- home/*/.cache
  230. # Explicitly include everything in /home
  231. + home/**
  232. # Explicitly exclude some directories without recursing into them
  233. ! re:^(dev|proc|run|sys|tmp)
  234. # Exclude all other files and directories
  235. # that are not specifically included earlier.
  236. \- **
  237. .EE
  238. .UNINDENT
  239. .UNINDENT
  240. .sp
  241. \fBNOTE:\fP
  242. .INDENT 0.0
  243. .INDENT 3.5
  244. It\(aqs possible that a sub\-directory/file is matched while parent directories are not.
  245. In that case, parent directories are not backed up thus their user, group, permission,
  246. etc. can not be restored.
  247. .UNINDENT
  248. .UNINDENT
  249. .sp
  250. Note that the default pattern style for \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP is
  251. shell style (\fIsh:\fP), so those patterns behave similar to rsync include/exclude
  252. patterns. The pattern style can be set via the \fIP\fP prefix.
  253. .sp
  254. Patterns (\fB\-\-pattern\fP) and excludes (\fB\-\-exclude\fP) from the command line are
  255. considered first (in the order of appearance). Then patterns from \fB\-\-patterns\-from\fP
  256. are added. Exclusion patterns from \fB\-\-exclude\-from\fP files are appended last.
  257. .sp
  258. Examples:
  259. .INDENT 0.0
  260. .INDENT 3.5
  261. .sp
  262. .EX
  263. # backup pics, but not the ones from 2018, except the good ones:
  264. # note: using = is essential to avoid cmdline argument parsing issues.
  265. borg create \-\-pattern=+pics/2018/good \-\-pattern=\-pics/2018 repo::arch pics
  266. # use a file with patterns:
  267. borg create \-\-patterns\-from patterns.lst repo::arch
  268. .EE
  269. .UNINDENT
  270. .UNINDENT
  271. .sp
  272. The patterns.lst file could look like that:
  273. .INDENT 0.0
  274. .INDENT 3.5
  275. .sp
  276. .EX
  277. # \(dqsh:\(dq pattern style is the default, so the following line is not needed:
  278. P sh
  279. R /
  280. # can be rebuild
  281. \- home/*/.cache
  282. # they\(aqre downloads for a reason
  283. \- home/*/Downloads
  284. # susan is a nice person
  285. # include susans home
  286. + home/susan
  287. # also back up this exact file
  288. + pf:home/bobby/specialfile.txt
  289. # don\(aqt backup the other home directories
  290. \- home/*
  291. # don\(aqt even look in /proc
  292. ! proc
  293. .EE
  294. .UNINDENT
  295. .UNINDENT
  296. .sp
  297. You can specify recursion roots either on the command line or in a patternfile:
  298. .INDENT 0.0
  299. .INDENT 3.5
  300. .sp
  301. .EX
  302. # these two commands do the same thing
  303. borg create \-\-exclude home/bobby/junk repo::arch /home/bobby /home/susan
  304. borg create \-\-patterns\-from patternfile.lst repo::arch
  305. .EE
  306. .UNINDENT
  307. .UNINDENT
  308. .sp
  309. The patternfile:
  310. .INDENT 0.0
  311. .INDENT 3.5
  312. .sp
  313. .EX
  314. # note that excludes use fm: by default and patternfiles use sh: by default.
  315. # therefore, we need to specify fm: to have the same exact behavior.
  316. P fm
  317. R /home/bobby
  318. R /home/susan
  319. \- home/bobby/junk
  320. .EE
  321. .UNINDENT
  322. .UNINDENT
  323. .sp
  324. This allows you to share the same patterns between multiple repositories
  325. without needing to specify them on the command line.
  326. .SH AUTHOR
  327. The Borg Collective
  328. .\" Generated by docutils manpage writer.
  329. .