borg-patterns.1 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 "2024-01-20" "" "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 \fI\%Fnmatch\fP, selector \fIfm:\fP
  56. This is the default style for \fB\-\-exclude\fP and \fB\-\-exclude\-from\fP\&.
  57. These patterns use a variant of shell pattern syntax, with \(aq*\(aq matching
  58. any number of characters, \(aq?\(aq matching any single character, \(aq[...]\(aq
  59. matching any single character specified, including ranges, and \(aq[!...]\(aq
  60. matching any character not specified. For the purpose of these patterns,
  61. the path separator (backslash for Windows and \(aq/\(aq on other systems) is not
  62. treated specially. Wrap meta\-characters in brackets for a literal
  63. match (i.e. \fI[?]\fP to match the literal character \fI?\fP). For a path
  64. to match a pattern, the full path must match, or it must match
  65. from the start of the full path to just before a path separator. Except
  66. for the root path, paths will never end in the path separator when
  67. matching is attempted. Thus, if a given pattern ends in a path
  68. separator, a \(aq*\(aq is appended before matching is attempted. A leading
  69. path separator is always removed.
  70. .TP
  71. .B Shell\-style patterns, selector \fIsh:\fP
  72. This is the default style for \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP\&.
  73. Like fnmatch patterns these are similar to shell patterns. The difference
  74. is that the pattern may include \fI**/\fP for matching zero or more directory
  75. levels, \fI*\fP for matching zero or more arbitrary characters with the
  76. exception of any path separator. A leading path separator is always removed.
  77. .TP
  78. .B Regular expressions, selector \fIre:\fP
  79. Regular expressions similar to those found in Perl are supported. Unlike
  80. shell patterns regular expressions are not required to match the full
  81. path and any substring match is sufficient. It is strongly recommended to
  82. anchor patterns to the start (\(aq^\(aq), to the end (\(aq$\(aq) or both. Path
  83. separators (backslash for Windows and \(aq/\(aq on other systems) in paths are
  84. always normalized to a forward slash (\(aq/\(aq) before applying a pattern. The
  85. regular expression syntax is described in the \fI\%Python documentation for
  86. the re module\fP\&.
  87. .TP
  88. .B Path prefix, selector \fIpp:\fP
  89. This pattern style is useful to match whole sub\-directories. The pattern
  90. \fIpp:root/somedir\fP matches \fIroot/somedir\fP and everything therein. A leading
  91. path separator is always removed.
  92. .TP
  93. .B Path full\-match, selector \fIpf:\fP
  94. This pattern style is (only) useful to match full paths.
  95. This is kind of a pseudo pattern as it can not have any variable or
  96. unspecified parts \- the full path must be given. \fIpf:root/file.ext\fP matches
  97. \fIroot/file.ext\fP only. A leading path separator is always removed.
  98. .sp
  99. Implementation note: this is implemented via very time\-efficient O(1)
  100. hashtable lookups (this means you can have huge amounts of such patterns
  101. without impacting performance much).
  102. Due to that, this kind of pattern does not respect any context or order.
  103. If you use such a pattern to include a file, it will always be included
  104. (if the directory recursion encounters it).
  105. Other include/exclude patterns that would normally match will be ignored.
  106. Same logic applies for exclude.
  107. .UNINDENT
  108. .sp
  109. \fBNOTE:\fP
  110. .INDENT 0.0
  111. .INDENT 3.5
  112. \fIre:\fP, \fIsh:\fP and \fIfm:\fP patterns are all implemented on top of the Python SRE
  113. engine. It is very easy to formulate patterns for each of these types which
  114. requires an inordinate amount of time to match paths. If untrusted users
  115. are able to supply patterns, ensure they cannot supply \fIre:\fP patterns.
  116. Further, ensure that \fIsh:\fP and \fIfm:\fP patterns only contain a handful of
  117. wildcards at most.
  118. .UNINDENT
  119. .UNINDENT
  120. .sp
  121. Exclusions can be passed via the command line option \fB\-\-exclude\fP\&. When used
  122. from within a shell, the patterns should be quoted to protect them from
  123. expansion.
  124. .sp
  125. The \fB\-\-exclude\-from\fP option permits loading exclusion patterns from a text
  126. file with one pattern per line. Lines empty or starting with the number sign
  127. (\(aq#\(aq) after removing whitespace on both ends are ignored. The optional style
  128. selector prefix is also supported for patterns loaded from a file. Due to
  129. whitespace removal, paths with whitespace at the beginning or end can only be
  130. excluded using regular expressions.
  131. .sp
  132. To test your exclusion patterns without performing an actual backup you can
  133. run \fBborg create \-\-list \-\-dry\-run ...\fP\&.
  134. .sp
  135. Examples:
  136. .INDENT 0.0
  137. .INDENT 3.5
  138. .sp
  139. .nf
  140. .ft C
  141. # Exclude \(aq/home/user/file.o\(aq but not \(aq/home/user/file.odt\(aq:
  142. $ borg create \-e \(aq*.o\(aq backup /
  143. # Exclude \(aq/home/user/junk\(aq and \(aq/home/user/subdir/junk\(aq but
  144. # not \(aq/home/user/importantjunk\(aq or \(aq/etc/junk\(aq:
  145. $ borg create \-e \(aqhome/*/junk\(aq backup /
  146. # Exclude the contents of \(aq/home/user/cache\(aq but not the directory itself:
  147. $ borg create \-e home/user/cache/ backup /
  148. # The file \(aq/home/user/cache/important\(aq is *not* backed up:
  149. $ borg create \-e home/user/cache/ backup / /home/user/cache/important
  150. # The contents of directories in \(aq/home\(aq are not backed up when their name
  151. # ends in \(aq.tmp\(aq
  152. $ borg create \-\-exclude \(aqre:^home/[^/]+\e.tmp/\(aq backup /
  153. # Load exclusions from file
  154. $ cat >exclude.txt <<EOF
  155. # Comment line
  156. home/*/junk
  157. *.tmp
  158. fm:aa:something/*
  159. re:^home/[^/]+\e.tmp/
  160. sh:home/*/.thumbnails
  161. # Example with spaces, no need to escape as it is processed by borg
  162. some file with spaces.txt
  163. EOF
  164. $ borg create \-\-exclude\-from exclude.txt backup /
  165. .ft P
  166. .fi
  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. .nf
  221. .ft C
  222. # Define the recursion root
  223. R /
  224. # Exclude all iso files in any directory
  225. \- **/*.iso
  226. # Explicitly include all inside etc and root
  227. + etc/**
  228. + root/**
  229. # Exclude a specific directory under each user\(aqs home directories
  230. \- home/*/.cache
  231. # Explicitly include everything in /home
  232. + home/**
  233. # Explicitly exclude some directories without recursing into them
  234. ! re:^(dev|proc|run|sys|tmp)
  235. # Exclude all other files and directories
  236. # that are not specifically included earlier.
  237. \- **
  238. .ft P
  239. .fi
  240. .UNINDENT
  241. .UNINDENT
  242. .sp
  243. \fBNOTE:\fP
  244. .INDENT 0.0
  245. .INDENT 3.5
  246. It\(aqs possible that a sub\-directory/file is matched while parent directories are not.
  247. In that case, parent directories are not backed up thus their user, group, permission,
  248. etc. can not be restored.
  249. .UNINDENT
  250. .UNINDENT
  251. .sp
  252. Note that the default pattern style for \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP is
  253. shell style (\fIsh:\fP), so those patterns behave similar to rsync include/exclude
  254. patterns. The pattern style can be set via the \fIP\fP prefix.
  255. .sp
  256. Patterns (\fB\-\-pattern\fP) and excludes (\fB\-\-exclude\fP) from the command line are
  257. considered first (in the order of appearance). Then patterns from \fB\-\-patterns\-from\fP
  258. are added. Exclusion patterns from \fB\-\-exclude\-from\fP files are appended last.
  259. .sp
  260. Examples:
  261. .INDENT 0.0
  262. .INDENT 3.5
  263. .sp
  264. .nf
  265. .ft C
  266. # backup pics, but not the ones from 2018, except the good ones:
  267. # note: using = is essential to avoid cmdline argument parsing issues.
  268. borg create \-\-pattern=+pics/2018/good \-\-pattern=\-pics/2018 repo::arch pics
  269. # use a file with patterns:
  270. borg create \-\-patterns\-from patterns.lst repo::arch
  271. .ft P
  272. .fi
  273. .UNINDENT
  274. .UNINDENT
  275. .sp
  276. The patterns.lst file could look like that:
  277. .INDENT 0.0
  278. .INDENT 3.5
  279. .sp
  280. .nf
  281. .ft C
  282. # \(dqsh:\(dq pattern style is the default, so the following line is not needed:
  283. P sh
  284. R /
  285. # can be rebuild
  286. \- home/*/.cache
  287. # they\(aqre downloads for a reason
  288. \- home/*/Downloads
  289. # susan is a nice person
  290. # include susans home
  291. + home/susan
  292. # also back up this exact file
  293. + pf:home/bobby/specialfile.txt
  294. # don\(aqt backup the other home directories
  295. \- home/*
  296. # don\(aqt even look in /proc
  297. ! proc
  298. .ft P
  299. .fi
  300. .UNINDENT
  301. .UNINDENT
  302. .sp
  303. You can specify recursion roots either on the command line or in a patternfile:
  304. .INDENT 0.0
  305. .INDENT 3.5
  306. .sp
  307. .nf
  308. .ft C
  309. # these two commands do the same thing
  310. borg create \-\-exclude home/bobby/junk repo::arch /home/bobby /home/susan
  311. borg create \-\-patterns\-from patternfile.lst repo::arch
  312. .ft P
  313. .fi
  314. .UNINDENT
  315. .UNINDENT
  316. .sp
  317. The patternfile:
  318. .INDENT 0.0
  319. .INDENT 3.5
  320. .sp
  321. .nf
  322. .ft C
  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. .ft P
  330. .fi
  331. .UNINDENT
  332. .UNINDENT
  333. .sp
  334. This allows you to share the same patterns between multiple repositories
  335. without needing to specify them on the command line.
  336. .SH AUTHOR
  337. The Borg Collective
  338. .\" Generated by docutils manpage writer.
  339. .