_borg 22 KB

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