borg-patterns.1 12 KB

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