borg-patterns.1 12 KB

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