borg-patterns.1 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "2022-08-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. If you give \fI/absolute/\fP as root, the paths going into the matcher will
  40. look relative like \fIabsolute/.../file.ext\fP, because file paths in Borg
  41. archives are always stored normalized and relative. This means that e.g.
  42. \fBborg create /path/to/repo ../some/path\fP will store all files as
  43. \fIsome/path/.../file.ext\fP and \fBborg create /path/to/repo /home/user\fP
  44. will store all files as \fIhome/user/.../file.ext\fP\&.
  45. .sp
  46. A directory exclusion pattern can end either with or without a slash (\(aq/\(aq).
  47. If it ends with a slash, such as \fIsome/path/\fP, the directory will be
  48. included but not its content. If it does not end with a slash, such as
  49. \fIsome/path\fP, both the directory and content will be excluded.
  50. .sp
  51. File patterns support these styles: fnmatch, shell, regular expressions,
  52. path prefixes and path full\-matches. By default, fnmatch is used for
  53. \fB\-\-exclude\fP patterns and shell\-style is used for the \fB\-\-pattern\fP
  54. option. For commands that support patterns in their \fBPATH\fP argument
  55. like (\fBborg list\fP), the default pattern is path prefix.
  56. .sp
  57. Starting with Borg 1.2, discovered fs paths are normalised, have leading
  58. slashes removed and then are matched against your patterns.
  59. Note: You need to review your include / exclude patterns and make
  60. sure they do not expect leading slashes. Borg can only deal with this
  61. for some very simple patterns by removing leading slashes there also.
  62. .sp
  63. If followed by a colon (\(aq:\(aq) the first two characters of a pattern are
  64. used as a style selector. Explicit style selection is necessary when a
  65. non\-default style is desired or when the desired pattern starts with
  66. two alphanumeric characters followed by a colon (i.e. \fIaa:something/*\fP).
  67. .INDENT 0.0
  68. .TP
  69. .B \fI\%Fnmatch\fP, 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 \fI\%Python documentation for
  100. the re module\fP\&.
  101. .TP
  102. .B Path prefix, selector \fIpp:\fP
  103. This pattern style is useful to match whole sub\-directories. The pattern
  104. \fIpp:root/somedir\fP matches \fIroot/somedir\fP and everything therein. A leading
  105. path separator is always removed.
  106. .TP
  107. .B Path full\-match, selector \fIpf:\fP
  108. This pattern style is (only) useful to match full paths.
  109. This is kind of a pseudo pattern as it can not have any variable or
  110. unspecified parts \- the full path must be given. \fIpf:root/file.ext\fP matches
  111. \fIroot/file.ext\fP only. A leading path separator is always removed.
  112. .sp
  113. Implementation note: this is implemented via very time\-efficient O(1)
  114. hashtable lookups (this means you can have huge amounts of such patterns
  115. without impacting performance much).
  116. Due to that, this kind of pattern does not respect any context or order.
  117. If you use such a pattern to include a file, it will always be included
  118. (if the directory recursion encounters it).
  119. Other include/exclude patterns that would normally match will be ignored.
  120. Same logic applies for exclude.
  121. .UNINDENT
  122. .sp
  123. \fBNOTE:\fP
  124. .INDENT 0.0
  125. .INDENT 3.5
  126. \fIre:\fP, \fIsh:\fP and \fIfm:\fP patterns are all implemented on top of the Python SRE
  127. engine. It is very easy to formulate patterns for each of these types which
  128. requires an inordinate amount of time to match paths. If untrusted users
  129. are able to supply patterns, ensure they cannot supply \fIre:\fP patterns.
  130. Further, ensure that \fIsh:\fP and \fIfm:\fP patterns only contain a handful of
  131. wildcards at most.
  132. .UNINDENT
  133. .UNINDENT
  134. .sp
  135. Exclusions can be passed via the command line option \fB\-\-exclude\fP\&. When used
  136. from within a shell, the patterns should be quoted to protect them from
  137. expansion.
  138. .sp
  139. The \fB\-\-exclude\-from\fP option permits loading exclusion patterns from a text
  140. file with one pattern per line. Lines empty or starting with the number sign
  141. (\(aq#\(aq) after removing whitespace on both ends are ignored. The optional style
  142. selector prefix is also supported for patterns loaded from a file. Due to
  143. whitespace removal, paths with whitespace at the beginning or end can only be
  144. excluded using regular expressions.
  145. .sp
  146. To test your exclusion patterns without performing an actual backup you can
  147. run \fBborg create \-\-list \-\-dry\-run ...\fP\&.
  148. .sp
  149. Examples:
  150. .INDENT 0.0
  151. .INDENT 3.5
  152. .sp
  153. .nf
  154. .ft C
  155. # Exclude \(aq/home/user/file.o\(aq but not \(aq/home/user/file.odt\(aq:
  156. $ borg create \-e \(aq*.o\(aq backup /
  157. # Exclude \(aq/home/user/junk\(aq and \(aq/home/user/subdir/junk\(aq but
  158. # not \(aq/home/user/importantjunk\(aq or \(aq/etc/junk\(aq:
  159. $ borg create \-e \(aqhome/*/junk\(aq backup /
  160. # Exclude the contents of \(aq/home/user/cache\(aq but not the directory itself:
  161. $ borg create \-e home/user/cache/ backup /
  162. # The file \(aq/home/user/cache/important\(aq is *not* backed up:
  163. $ borg create \-e home/user/cache/ backup / /home/user/cache/important
  164. # The contents of directories in \(aq/home\(aq are not backed up when their name
  165. # ends in \(aq.tmp\(aq
  166. $ borg create \-\-exclude \(aqre:^home/[^/]+\e.tmp/\(aq backup /
  167. # Load exclusions from file
  168. $ cat >exclude.txt <<EOF
  169. # Comment line
  170. home/*/junk
  171. *.tmp
  172. fm:aa:something/*
  173. re:^home/[^/]+\e.tmp/
  174. sh:home/*/.thumbnails
  175. # Example with spaces, no need to escape as it is processed by borg
  176. some file with spaces.txt
  177. EOF
  178. $ borg create \-\-exclude\-from exclude.txt backup /
  179. .ft P
  180. .fi
  181. .UNINDENT
  182. .UNINDENT
  183. .sp
  184. A more general and easier to use way to define filename matching patterns exists
  185. with the \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP options. Using these, you may
  186. specify the backup roots (starting points) and patterns for inclusion/exclusion.
  187. A root path starts with the prefix \fIR\fP, followed by a path (a plain path, not a
  188. file pattern). An include rule starts with the prefix +, an exclude rule starts
  189. with the prefix \-, an exclude\-norecurse rule starts with !, all followed by a pattern.
  190. .sp
  191. \fBNOTE:\fP
  192. .INDENT 0.0
  193. .INDENT 3.5
  194. Via \fB\-\-pattern\fP or \fB\-\-patterns\-from\fP you can define BOTH inclusion and exclusion
  195. of files using pattern prefixes \fB+\fP and \fB\-\fP\&. With \fB\-\-exclude\fP and
  196. \fB\-\-exclude\-from\fP ONLY excludes are defined.
  197. .UNINDENT
  198. .UNINDENT
  199. .sp
  200. Inclusion patterns are useful to include paths that are contained in an excluded
  201. path. The first matching pattern is used so if an include pattern matches before
  202. an exclude pattern, the file is backed up. If an exclude\-norecurse pattern matches
  203. a directory, it won\(aqt recurse into it and won\(aqt discover any potential matches for
  204. include rules below that directory.
  205. .sp
  206. \fBNOTE:\fP
  207. .INDENT 0.0
  208. .INDENT 3.5
  209. It\(aqs possible that a sub\-directory/file is matched while parent directories are not.
  210. In that case, parent directories are not backed up thus their user, group, permission,
  211. etc. can not be restored.
  212. .UNINDENT
  213. .UNINDENT
  214. .sp
  215. Note that the default pattern style for \fB\-\-pattern\fP and \fB\-\-patterns\-from\fP is
  216. shell style (\fIsh:\fP), so those patterns behave similar to rsync include/exclude
  217. patterns. The pattern style can be set via the \fIP\fP prefix.
  218. .sp
  219. Patterns (\fB\-\-pattern\fP) and excludes (\fB\-\-exclude\fP) from the command line are
  220. considered first (in the order of appearance). Then patterns from \fB\-\-patterns\-from\fP
  221. are added. Exclusion patterns from \fB\-\-exclude\-from\fP files are appended last.
  222. .sp
  223. Examples:
  224. .INDENT 0.0
  225. .INDENT 3.5
  226. .sp
  227. .nf
  228. .ft C
  229. # backup pics, but not the ones from 2018, except the good ones:
  230. # note: using = is essential to avoid cmdline argument parsing issues.
  231. borg create \-\-pattern=+pics/2018/good \-\-pattern=\-pics/2018 repo::arch pics
  232. # use a file with patterns:
  233. borg create \-\-patterns\-from patterns.lst repo::arch
  234. .ft P
  235. .fi
  236. .UNINDENT
  237. .UNINDENT
  238. .sp
  239. The patterns.lst file could look like that:
  240. .INDENT 0.0
  241. .INDENT 3.5
  242. .sp
  243. .nf
  244. .ft C
  245. # "sh:" pattern style is the default, so the following line is not needed:
  246. P sh
  247. R /
  248. # can be rebuild
  249. \- home/*/.cache
  250. # they\(aqre downloads for a reason
  251. \- home/*/Downloads
  252. # susan is a nice person
  253. # include susans home
  254. + home/susan
  255. # also back up this exact file
  256. + pf:home/bobby/specialfile.txt
  257. # don\(aqt backup the other home directories
  258. \- home/*
  259. # don\(aqt even look in /proc
  260. ! proc
  261. .ft P
  262. .fi
  263. .UNINDENT
  264. .UNINDENT
  265. .sp
  266. You can specify recursion roots either on the command line or in a patternfile:
  267. .INDENT 0.0
  268. .INDENT 3.5
  269. .sp
  270. .nf
  271. .ft C
  272. # these two commands do the same thing
  273. borg create \-\-exclude home/bobby/junk repo::arch /home/bobby /home/susan
  274. borg create \-\-patterns\-from patternfile.lst repo::arch
  275. .ft P
  276. .fi
  277. .UNINDENT
  278. .UNINDENT
  279. .sp
  280. The patternfile:
  281. .INDENT 0.0
  282. .INDENT 3.5
  283. .sp
  284. .nf
  285. .ft C
  286. # note that excludes use fm: by default and patternfiles use sh: by default.
  287. # therefore, we need to specify fm: to have the same exact behavior.
  288. P fm
  289. R /home/bobby
  290. R /home/susan
  291. \- home/bobby/junk
  292. .ft P
  293. .fi
  294. .UNINDENT
  295. .UNINDENT
  296. .sp
  297. This allows you to share the same patterns between multiple repositories
  298. without needing to specify them on the command line.
  299. .SH AUTHOR
  300. The Borg Collective
  301. .\" Generated by docutils manpage writer.
  302. .