_borg 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #compdef borg
  2. # -------
  3. #
  4. # For borg 1.1.1
  5. #
  6. # To Install:
  7. #
  8. # Copy this file to /usr/share/zsh/site-functions/
  9. #
  10. # -------
  11. # borgbackup ZSH completion
  12. # Kevin Gravier 2017 <kevin@mrkmg.com>
  13. # https://github.com/mrkmg/borgbackup-zsh-completion
  14. #
  15. # The MIT License (MIT)
  16. # Copyright (c) 2017 Kevin
  17. # Permission is hereby granted, free of charge, to any person obtaining a copy
  18. # of this software and associated documentation files (the "Software"), to deal
  19. # in the Software without restriction, including without limitation the rights
  20. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. # copies of the Software, and to permit persons to whom the Software is
  22. # furnished to do so, subject to the following conditions:
  23. # The above copyright notice and this permission notice shall be included in all
  24. # copies or substantial portions of the Software.
  25. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  26. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  28. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  30. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. # SOFTWARE.
  32. _borg() {
  33. typeset -A opt_args
  34. local -a borg_possible_commands borg_possible_key_commands commands keyCommands borg_standard_options
  35. local command keyCommand item subItem
  36. borg_standard_options=({-h,--help}'[show this help message and exit]'
  37. --critical'[work on log level CRITICAL]'
  38. --error'[work on log level ERROR]'
  39. --warning'[work on log level WARNING (default)]'
  40. {--info,-v,--verbose}'[work on log level INFO]'
  41. --debug'[work on log level DEBUG]'
  42. --default-topic'[enable TOPIC debugging (can be specified multiple times).]:TOPIC'
  43. {-p,--progress}'[show progress information]'
  44. --log-json'[Output one JSON object per log line instead of formatted text.]'
  45. --lock-wait'[wait at most SECONDS for acquiring a repository/cache lock (default: 1).]:SECONDS'
  46. --show-version'[show/log the borg version]'
  47. --show-rc'[show/log the return code (rc)]'
  48. --umask'[set umask to M (local and remote, default: 0077)]:umask'
  49. --remote-path'[set remote path to executable (default: "borg")]:_files'
  50. --remote-ratelimit'[set remote network upload rate limit in kiByte/s (default: 0=unlimited)]:RATE'
  51. --consider-part-files'[treat part files like normal files (e.g. to list/extract them)]'
  52. --debug-profile'[Write execution profile in Borg format into FILE.]:_files')
  53. borg_possible_commands=(serve init check key change-passphrase create extract export-tar diff rename delete list mount umount info break-lock prune upgrade with-lock benchmark)
  54. borg_possible_key_commands=(change-passphrase import export)
  55. command=""
  56. keyCommand=""
  57. for item in $words; do
  58. (( ${borg_possible_commands[(I)$item]} )) && command=$item
  59. (( ${borg_possible_key_commands[(I)$item]} )) && keyCommand=$item
  60. done
  61. case $command in
  62. (serve)
  63. _arguments \
  64. --restrict-to-path'[restrict repository access to PATH]:_files'\
  65. --restrict-to-repository'[restrict repository access]:_files'\
  66. --append-only'[only allow appending to repository segment files]'\
  67. --storage-quota'[Override storage quota of the repository]:QUOTA'\
  68. $borg_standard_options
  69. ;;
  70. (init)
  71. _arguments \
  72. '2:repo:_files'\
  73. {-e,--encryption}'[select encryption key mode]:MODE'\
  74. --append-only'[only allow appending to repository segment files]'\
  75. --storage-quota'[Override storage quota of the repository]:QUOTA'\
  76. $borg_standard_options
  77. ;;
  78. (check)
  79. _arguments \
  80. '2:archives:__borg_archive'\
  81. --repository-only'[only perform repository checks]'\
  82. --archives-only'[only perform archives checks]'\
  83. --verify-data'[perform cryptographic archive data integrity verification]'\
  84. --repair'[attempt to repair any inconsistencies found]'\
  85. --save-space'[work slower, but using less space]'\
  86. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  87. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  88. --sort-keys'[Comma-separated list of sorting keys]:KEYS'\
  89. --first'[consider first N archives after other filters were applied]:N'\
  90. --last'[consider last N archives after other filters were applied]:N'\
  91. $borg_standard_options
  92. ;;
  93. (key)
  94. case $keyCommand in
  95. (change-passphrase)
  96. _arguments \
  97. '2:subCommand:(change-passphrase import export)'\
  98. '3:archives:__borg_archive'\
  99. $borg_standard_options
  100. ;;
  101. (import)
  102. _arguments \
  103. '2:subCommand:(change-passphrase import export)'\
  104. '3:archives:__borg_archive'\
  105. '4:path:_files'\
  106. --paper'[interactively import from a backup done with --paper]'\
  107. $borg_standard_options
  108. ;;
  109. (export)
  110. _arguments \
  111. '2:subCommand:(change-passphrase import export)'\
  112. '3:archives:__borg_archive'\
  113. '4:path:_files'\
  114. --paper'[Create an export suitable for printing and later type-in]'\
  115. --qr-html'[Create an html file suitable for printing and later type-in or qr scan]'\
  116. $borg_standard_options
  117. ;;
  118. *)
  119. _arguments \
  120. '2:subCommand:(change-passphrase import export)'\
  121. $borg_standard_options
  122. ;;
  123. esac
  124. ;;
  125. (change-passphrase)
  126. _arguments \
  127. '2:archives:__borg_archive'\
  128. $borg_standard_options
  129. ;;
  130. (create)
  131. _arguments \
  132. '2:archives:__borg_archive'\
  133. '3:path:_files'\
  134. {-n,--dry-run}'[do not create a backup archive]'\
  135. {-s,--stats}'[print statistics for the created archive]'\
  136. --list'[output verbose list of items (files, dirs, ...)]'\
  137. --filter'[only display items with the given status characters]:STATUSCHARS'\
  138. --json'[output stats as JSON. Implies --stats.]'\
  139. --no-cache-sync'[experimental: do not synchronize the cache. Implies not using the files cache.]'\
  140. {-x,--one-file-system}'[stay in the same file system ]'\
  141. --numeric-owner'[only store numeric user and group identifiers]'\
  142. --noatime'[do not store atime into archive]'\
  143. --noctime'[do not store ctime into archive]'\
  144. --nobsdflags'[do not read and store bsdflags (e.g. NODUMP, IMMUTABLE) into archive]'\
  145. --ignore-inode'[gnore inode data in the file metadata cache used to detect unchanged files.]'\
  146. --files-cache'[operate files cache in MODE. default: ctime,size,inode]:MODE'\
  147. --read-special'[open and read block and char device files as well as FIFOs as if they were regular files.]'\
  148. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  149. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  150. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  151. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  152. --exclude-caches'[exclude directories that contain a CACHEDIR.TAG file ]'\
  153. --exclude-if-present'[exclude directories that are tagged by containing a filesystem object with the given NAME]:NAME'\
  154. {--keep-exclude-tags,--keep-tag-files}'[if tag objects are specified with --exclude-if-present, don’t omit the tag objects themselves]'\
  155. --exclude-nodump'[exclude files flagged NODUMP]'\
  156. --comment'[add a comment text to the archive]:COMMENT'\
  157. --timestamp'[manually specify the archive creation date/time]:TIMESTAMP'\
  158. {-c,--checkpoint-interval}'[write checkpoint every SECONDS seconds]:SECONDS'\
  159. --chunker-params'[pecify the chunker parameters]:PARAMS'\
  160. {-C,--compression}'[select compression algorithm]:COMPRESSION'\
  161. $borg_standard_options
  162. ;;
  163. (extract)
  164. _arguments \
  165. '2:archives:__borg_archive'\
  166. '3:path:_files'\
  167. --list'[output verbose list of items (files, dirs, ...)]'\
  168. {-n,--dry-run}'[do not actually change any files]'\
  169. --numeric-owner'[only obey numeric user and group identifiers]'\
  170. --nobsdflags'[do not extract/set bsdflags (e.g. NODUMP, IMMUTABLE)]'\
  171. --stdout'[write all extracted data to stdout]'\
  172. --sparse'[create holes in output sparse file from all-zero chunks]'\
  173. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  174. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  175. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  176. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  177. --strip-components'[Remove the specified number of leading path elements. Paths with fewer elements will be silently skipped.]:NUMBER'\
  178. $borg_standard_options
  179. ;;
  180. (export-tar)
  181. _arguments \
  182. '2:archives:__borg_archive'\
  183. '3:tar:_files'\
  184. '4:path:_files'\
  185. --tar-filter'[filter program to pipe data through]'\
  186. --list'[output verbose list of items (files, dirs, ...)]'\
  187. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  188. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  189. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  190. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  191. --strip-components'[Remove the specified number of leading path elements. Paths with fewer elements will be silently skipped.]:NUMBER'\
  192. $borg_standard_options
  193. ;;
  194. (diff)
  195. _arguments \
  196. '2:archives:__borg_archive'\
  197. '3:archives:__borg_archive'\
  198. '*:path:_files'\
  199. --numeric-owner'[only obey numeric user and group identifiers]'\
  200. --same-chunker-params'[Override check of chunker parameters.]'\
  201. --sort'[Sort the output lines by file path.]'\
  202. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  203. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  204. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  205. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  206. --exclude-caches'[exclude directories that contain a CACHEDIR.TAG file ]'\
  207. --exclude-if-present'[exclude directories that are tagged by containing a filesystem object with the given NAME]:NAME'\
  208. {--keep-exclude-tags,--keep-tag-files}'[if tag objects are specified with --exclude-if-present, don’t omit the tag objects themselves]'\
  209. $borg_standard_options
  210. ;;
  211. (rename)
  212. _arguments \
  213. '2:archives:__borg_archive'\
  214. '3:name:NAME'\
  215. $borg_standard_options
  216. ;;
  217. (delete)
  218. _arguments \
  219. '2:archives:__borg_archive'\
  220. '*:archives:archives'\
  221. {-s,--stats}'[print statistics for the deleted archive]'\
  222. --cache-only'[delete only the local cache for the given repository]'\
  223. --force'[force deletion of corrupted archives, use --force --force in case --force does not work.]'\
  224. --save-space'[work slower, but using less space]'\
  225. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  226. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  227. --sort-keys'[Comma-separated list of sorting keys]:KEYS'\
  228. --first'[consider first N archives after other filters were applied]:N'\
  229. --last'[consider last N archives after other filters were applied]:N'\
  230. $borg_standard_options
  231. ;;
  232. (list)
  233. _arguments \
  234. '2:archives:__borg_archive'\
  235. '*:path:_files'\
  236. --short'[only print file/directory names, nothing else]'\
  237. {--format,--list-format}'[specify format for file listing]:FORMAT'\
  238. --json'[Only valid for listing repository contents. Format output as JSON.]'\
  239. --json-lines'[Only valid for listing archive contents. Format output as JSON Lines. ]'\
  240. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  241. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  242. --sort-keys'[Comma-separated list of sorting keys]:KEYS'\
  243. --first'[consider first N archives after other filters were applied]:N'\
  244. --last'[consider last N archives after other filters were applied]:N'\
  245. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  246. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  247. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  248. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  249. --exclude-caches'[exclude directories that contain a CACHEDIR.TAG file ]'\
  250. --exclude-if-present'[exclude directories that are tagged by containing a filesystem object with the given NAME]:NAME'\
  251. {--keep-exclude-tags,--keep-tag-files}'[if tag objects are specified with --exclude-if-present, don’t omit the tag objects themselves]'\
  252. $borg_standard_options
  253. ;;
  254. (mount)
  255. _arguments \
  256. '2:archives:__borg_archive'\
  257. '3:mountpoint:_files'\
  258. {-f,--foreground}'[stay in foreground, do not daemonize]'\
  259. -o'[Extra mount options]:OPTS'\
  260. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  261. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  262. --sort-keys'[Comma-separated list of sorting keys]:KEYS'\
  263. --first'[consider first N archives after other filters were applied]:N'\
  264. --last'[consider last N archives after other filters were applied]:N'\
  265. $borg_standard_options
  266. ;;
  267. (umount)
  268. _arguments \
  269. '2:mountpoint:_files'\
  270. $borg_standard_options
  271. ;;
  272. (info)
  273. _arguments \
  274. '2:archives:__borg_archive'\
  275. --json'[format output as JSON]'\
  276. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  277. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  278. --sort-keys'[Comma-separated list of sorting keys]:KEYS'\
  279. --first'[consider first N archives after other filters were applied]:N'\
  280. --last'[consider last N archives after other filters were applied]:N'\
  281. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  282. $borg_standard_options
  283. ;;
  284. (break-lock)
  285. _arguments \
  286. '2:archives:__borg_archive'\
  287. $borg_standard_options
  288. ;;
  289. (prune)
  290. _arguments \
  291. '2:archives:__borg_archive'\
  292. {-n,--dry-run}'[do not change repository]'\
  293. --force'[]'\
  294. {-s,--stats}'[]'\
  295. --list'[]'\
  296. --keep-within'[keep all archives within this time interval]:INERVAL'\
  297. {--keep-last,--keep-secondly}'[number of secondly archives to keep]:N'\
  298. --keep-minutely'[number of minutely archives to keep]:N'\
  299. {-H,--keep-hourly}'[number of hourly archives to keep]:N'\
  300. {-d,--keep-daily}'[number of daily archives to keep]:N'\
  301. {-w,--keep-weekly}'[number of weekly archives to keep]:N'\
  302. {-m,--keep-monthly}'[number of monthly archives to keep]:N'\
  303. {-y,--keep-yearly}'[number of yearly archives to keep]:N'\
  304. --save-space'[work slower, but using less space]'\
  305. $borg_standard_options
  306. ;;
  307. (upgrade)
  308. _arguments \
  309. '2:archives:__borg_archive'\
  310. {-n,--dry-run}'[do not change repository]'\
  311. --inplace'[rewrite repository in place, with no chance of going back to older versions of the repository.]'\
  312. --force'[Force upgrade]'\
  313. --tam'[Enable manifest authentication (in key and cache) (Borg 1.0.9 and later).]'\
  314. --disable-tam'[Disable manifest authentication (in key and cache).]'\
  315. $borg_standard_options
  316. ;;
  317. (with-lock)
  318. _arguments \
  319. '(-)2:archives:__borg_archive'\
  320. $borg_standard_options
  321. #'3:command:_command_names -e'\
  322. #'4:arguments:_normal'
  323. #TODO Debug this, getting "_tags:comptags:36: nesting level too deep" error
  324. ;;
  325. (benchmark)
  326. _arguments \
  327. '2:type:(crud)'\
  328. '3:repo:_files'\
  329. '4:path:_files'\
  330. $borg_standard_options
  331. ;;
  332. *)
  333. commands=(
  334. 'serve:start repository server process'
  335. 'init:initialize empty repository'
  336. 'check:verify repository'
  337. 'key:manage repository key'
  338. 'change-passphrase:change repository passphrase'
  339. 'create:create backup'
  340. 'extract:extract archive contents'
  341. 'export-tar:create tarball from archive'
  342. 'diff:find differences in archive contents'
  343. 'rename:rename archive'
  344. 'delete:delete archive'
  345. 'list:list archive or repository contents'
  346. 'mount:mount repository'
  347. 'umount:umount repository'
  348. 'info:show repository or archive information'
  349. 'break-lock:break repository and cache locks'
  350. 'prune:prune archives'
  351. 'upgrade:upgrade repository format'
  352. 'with-lock:run user command with lock held'
  353. 'benchmark:benchmark command'
  354. )
  355. _describe 'values' commands
  356. _arguments $borg_standard_options
  357. ;;
  358. esac
  359. }
  360. __borg_archive() {
  361. local -a items
  362. if (($+BORG_REPO)); then
  363. items=("${(@f)$(borg list $cpath --short 2>/dev/null | sed "s#^#::#")}")
  364. else
  365. local cpath
  366. cpath=$words[-1]
  367. cpath=${cpath%::*}
  368. items=("${(@f)$(borg list $cpath --short 2>/dev/null | sed "s#^#${cpath}::#")}")
  369. fi
  370. if [[ $items[1] == "" ]]; then
  371. _files -/
  372. else
  373. _wanted archives expl 'archive' compadd $items
  374. fi
  375. }