_borg 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. --stdin-name'[use NAME in archive for stdin data]:NAME'\
  78. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  79. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  80. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  81. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  82. --exclude-caches'[exclude directories that contain a CACHEDIR.TAG file ]'\
  83. --exclude-if-present'[exclude directories that are tagged by containing a filesystem object with the given NAME]:NAME'\
  84. {--keep-exclude-tags,--keep-tag-files}'[if tag objects are specified with --exclude-if-present, don’t omit the tag objects themselves]'\
  85. {-x,--one-file-system}'[stay in the same file system ]'\
  86. --numeric-owner'[only store numeric user and group identifiers]'\
  87. --noatime'[do not store atime into archive]'\
  88. --nobirthtime'[do not store birthtime (creation date) into archive]'\
  89. --nobsdflags'[do not read and store bsdflags (e.g. NODUMP, IMMUTABLE) into archive]'\
  90. --ignore-inode'[ignore inode data in the file metadata cache used to detect unchanged files.]'\
  91. --files-cache'[operate files cache in MODE. default: ctime,size,inode]:MODE'\
  92. --read-special'[open and read block and char device files as well as FIFOs as if they were regular files.]'\
  93. --comment'[add a comment text to the archive]:COMMENT'\
  94. --timestamp'[manually specify the archive creation date/time]:TIMESTAMP'\
  95. --timestamp'[manually specify the archive creation date/time by a reference file/directory]:_files'\
  96. {-c,--checkpoint-interval}'[write checkpoint every SECONDS seconds]:SECONDS'\
  97. --chunker-params'[specify the chunker parameters]:PARAMS'\
  98. {-C,--compression}'[select compression algorithm]:COMPRESSION'\
  99. $borg_common_options
  100. ;;
  101. (extract)
  102. _arguments \
  103. '2:archives:__borg_archive'\
  104. '3:path:_files'\
  105. --list'[output verbose list of items (files, dirs, ...)]'\
  106. {-n,--dry-run}'[do not actually change any files]'\
  107. --numeric-owner'[only obey numeric user and group identifiers]'\
  108. --nobsdflags'[do not extract/set bsdflags (e.g. NODUMP, IMMUTABLE)]'\
  109. --stdout'[write all extracted data to stdout]'\
  110. --sparse'[create holes in output sparse file from all-zero chunks]'\
  111. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  112. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  113. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  114. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  115. --strip-components'[Remove the specified number of leading path elements. Paths with fewer elements will be silently skipped.]:NUMBER'\
  116. $borg_common_options
  117. ;;
  118. (check)
  119. _arguments \
  120. '2:archives:__borg_archive'\
  121. --repository-only'[only perform repository checks]'\
  122. --archives-only'[only perform archives checks]'\
  123. --verify-data'[perform cryptographic archive data integrity verification]'\
  124. --repair'[attempt to repair any inconsistencies found]'\
  125. --save-space'[work slower, but using less space]'\
  126. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  127. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  128. --sort-by'[Comma-separated list of sorting keys]:KEYS'\
  129. --first'[consider first N archives after other filters were applied]:N'\
  130. --last'[consider last N archives after other filters were applied]:N'\
  131. $borg_common_options
  132. ;;
  133. (rename)
  134. _arguments \
  135. '2:archives:__borg_archive'\
  136. '3:name:NAME'\
  137. $borg_common_options
  138. ;;
  139. (list)
  140. _arguments \
  141. '2:archives:__borg_archive'\
  142. '*:path:_files'\
  143. --short'[only print file/directory names, nothing else]'\
  144. {--format,--list-format}'[specify format for file listing]:FORMAT'\
  145. --json'[Only valid for listing repository contents. Format output as JSON.]'\
  146. --json-lines'[Only valid for listing archive contents. Format output as JSON Lines. ]'\
  147. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  148. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  149. --sort-by'[Comma-separated list of sorting keys]:KEYS'\
  150. --first'[consider first N archives after other filters were applied]:N'\
  151. --last'[consider last N archives after other filters were applied]:N'\
  152. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  153. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  154. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  155. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  156. $borg_common_options
  157. ;;
  158. (diff)
  159. _arguments \
  160. '2:archives:__borg_archive'\
  161. '3:archives:__borg_archive'\
  162. '*:path:_files'\
  163. --numeric-owner'[only obey numeric user and group identifiers]'\
  164. --same-chunker-params'[override check of chunker parameters.]'\
  165. --sort'[sort the output lines by file path.]'\
  166. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  167. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  168. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  169. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  170. $borg_common_options
  171. ;;
  172. (delete)
  173. _arguments \
  174. '2:archives:__borg_archive'\
  175. '*:archives:archives'\
  176. --dry-run'[do not change the repository]'\
  177. {-s,--stats}'[print statistics for the deleted archive]'\
  178. --cache-only'[delete only the local cache for the given repository]'\
  179. --force'[force deletion of corrupted archives, use --force --force in case --force does not work.]'\
  180. --save-space'[work slower, but using less space]'\
  181. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  182. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  183. --sort-by'[Comma-separated list of sorting keys]:KEYS'\
  184. --first'[consider first N archives after other filters were applied]:N'\
  185. --last'[consider last N archives after other filters were applied]:N'\
  186. $borg_common_options
  187. ;;
  188. (prune)
  189. _arguments \
  190. '2:archives:__borg_archive'\
  191. {-n,--dry-run}'[do not change repository]'\
  192. --force'[force pruning of corrupted archives]'\
  193. {-s,--stats}'[print statistics for the deleted archive]'\
  194. --list'[output verbose list of archives it keeps/prunes]'\
  195. --keep-within'[keep all archives within this time interval]:INTERVAL'\
  196. {--keep-last,--keep-secondly}'[number of secondly archives to keep]:N'\
  197. --keep-minutely'[number of minutely archives to keep]:N'\
  198. {-H,--keep-hourly}'[number of hourly archives to keep]:N'\
  199. {-d,--keep-daily}'[number of daily archives to keep]:N'\
  200. {-w,--keep-weekly}'[number of weekly archives to keep]:N'\
  201. {-m,--keep-monthly}'[number of monthly archives to keep]:N'\
  202. {-y,--keep-yearly}'[number of yearly archives to keep]:N'\
  203. --save-space'[work slower, but using less space]'\
  204. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  205. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  206. $borg_common_options
  207. ;;
  208. (info)
  209. _arguments \
  210. '2:archives:__borg_archive'\
  211. --json'[format output as JSON]'\
  212. {-P,--prefix}'[only consider archive names starting with this prefix.]:PREFIX'\
  213. {-a,--glob-archives}'[only consider archive names matching the glob]:GLOB'\
  214. --sort-by'[Comma-separated list of sorting keys]:KEYS'\
  215. --first'[consider first N archives after other filters were applied]:N'\
  216. --last'[consider last N archives after other filters were applied]:N'\
  217. $borg_common_options
  218. ;;
  219. (mount)
  220. _arguments \
  221. '2:archives:__borg_archive'\
  222. '3:mountpoint:_files'\
  223. {-f,--foreground}'[stay in foreground, do not daemonize]'\
  224. -o'[Extra mount options]:OPTS'\
  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-by'[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. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  231. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  232. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  233. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  234. --strip-components'[Remove the specified number of leading path elements. Paths with fewer elements will be silently skipped.]:NUMBER'\
  235. $borg_common_options
  236. ;;
  237. (umount)
  238. _arguments \
  239. '2:mountpoint:_files'\
  240. $borg_common_options
  241. ;;
  242. (key)
  243. case $keyCommand in
  244. (change-passphrase)
  245. _arguments \
  246. '2:subCommand:(change-passphrase import export)'\
  247. '3:archives:__borg_archive'\
  248. $borg_common_options
  249. ;;
  250. (export)
  251. _arguments \
  252. '2:subCommand:(change-passphrase import export)'\
  253. '3:archives:__borg_archive'\
  254. '4:path:_files'\
  255. --paper'[Create an export suitable for printing and later type-in]'\
  256. --qr-html'[Create an html file suitable for printing and later type-in or qr scan]'\
  257. $borg_common_options
  258. ;;
  259. (import)
  260. _arguments \
  261. '2:subCommand:(change-passphrase import export)'\
  262. '3:archives:__borg_archive'\
  263. '4:path:_files'\
  264. --paper'[interactively import from a backup done with --paper]'\
  265. $borg_common_options
  266. ;;
  267. *)
  268. _arguments \
  269. '2:subCommand:(change-passphrase import export)'\
  270. $borg_common_options
  271. ;;
  272. esac
  273. ;;
  274. (upgrade)
  275. _arguments \
  276. '2:archives:__borg_archive'\
  277. {-n,--dry-run}'[do not change repository]'\
  278. --inplace'[rewrite repository in place, with no chance of going back to older versions of the repository.]'\
  279. --force'[Force upgrade]'\
  280. --tam'[Enable manifest authentication (in key and cache).]'\
  281. --disable-tam'[Disable manifest authentication (in key and cache).]'\
  282. $borg_common_options
  283. ;;
  284. (recreate)
  285. _arguments \
  286. '2:archives:__borg_archive'\
  287. '3:path:_files'\
  288. --list'[output verbose list of items (files, dirs, ...)]'\
  289. --filter'[only display items with the given status characters]:STATUSCHARS'\
  290. {-n,--dry-run}'[do not create a backup archive]'\
  291. {-s,--stats}'[print statistics at end]'\
  292. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  293. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  294. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  295. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  296. --exclude-caches'[exclude directories that contain a CACHEDIR.TAG file ]'\
  297. --exclude-if-present'[exclude directories that are tagged by containing a filesystem object with the given NAME]:NAME'\
  298. {--keep-exclude-tags,--keep-tag-files}'[if tag objects are specified with --exclude-if-present, don’t omit the tag objects themselves]'\
  299. --target'[create a new archive with the name ARCHIVE]:ARCHIVE'\
  300. {-c,--checkpoint-interval}'[write checkpoint every SECONDS seconds]:SECONDS'\
  301. --comment'[add a comment text to the archive]:COMMENT'\
  302. --timestamp'[manually specify the archive creation date/time]:TIMESTAMP'\
  303. {-C,--compression}'[select compression algorithm]:COMPRESSION'\
  304. --recompress'[recompress data chunks according to --compression if if-different]:PARAMS'\
  305. --chunker-params'[pecify the chunker parameters]:PARAMS'\
  306. $borg_common_options
  307. ;;
  308. (export-tar)
  309. _arguments \
  310. '2:archives:__borg_archive'\
  311. '3:tar:_files'\
  312. '4:path:_files'\
  313. --tar-filter'[filter program to pipe data through]'\
  314. --list'[output verbose list of items (files, dirs, ...)]'\
  315. {-e,--exclude}'[exclude paths matching PATTERN]:PATTERN'\
  316. --exclude-from'[read exclude patterns from EXCLUDEFILE, one per line]:_files'\
  317. --pattern'[experimental: include/exclude paths matching PATTERN]:PATTERN'\
  318. --patterns-from'[experimental: read include/exclude patterns from PATTERNFILE, one per line]:_files'\
  319. --strip-components'[Remove the specified number of leading path elements. Paths with fewer elements will be silently skipped.]:NUMBER'\
  320. $borg_common_options
  321. ;;
  322. (serve)
  323. _arguments \
  324. --restrict-to-path'[restrict repository access to PATH]:_files'\
  325. --restrict-to-repository'[restrict repository access]:_files'\
  326. --append-only'[only allow appending to repository segment files]'\
  327. --storage-quota'[Override storage quota of the repository]:QUOTA'\
  328. $borg_common_options
  329. ;;
  330. (config)
  331. _arguments \
  332. '2:archives:__borg_archive'\
  333. '3:name:NAME'\
  334. '4:value:VALUE'\
  335. {-c,--cache}'[get and set values from the repo cache]'\
  336. {-d,--delete}'[delete the key from the config]'\
  337. --list'[list the configuration of the repo]'\
  338. $borg_common_options
  339. ;;
  340. (with-lock)
  341. _arguments \
  342. '(-)2:archives:__borg_archive'\
  343. $borg_common_options
  344. #'3:command:_command_names -e'\
  345. #'4:arguments:_normal'
  346. #TODO Debug this, getting "_tags:comptags:36: nesting level too deep" error
  347. ;;
  348. (break-lock)
  349. _arguments \
  350. '2:archives:__borg_archive'\
  351. $borg_common_options
  352. ;;
  353. (benchmark)
  354. _arguments \
  355. '2:type:(crud)'\
  356. '3:repo:_files'\
  357. '4:path:_files'\
  358. $borg_common_options
  359. ;;
  360. *)
  361. commands=(
  362. 'init:initialize empty repository'
  363. 'create:create backup'
  364. 'extract:extract archive contents'
  365. 'check:verify repository'
  366. 'rename:rename archive'
  367. 'list:list archive or repository contents'
  368. 'diff:find differences in archive contents'
  369. 'delete:delete archive'
  370. 'prune:prune archives'
  371. 'info:show repository or archive information'
  372. 'mount:mount repository'
  373. 'umount:umount repository'
  374. 'key:manage repository key'
  375. 'upgrade:upgrade repository format'
  376. 'recreate:recreate contents of existing archives'
  377. 'export-tar:create tarball from archive'
  378. 'serve:start repository server process'
  379. 'config:get/set options in repo/cache config'
  380. 'with-lock:run user command with lock held'
  381. 'break-lock:break repository and cache locks'
  382. 'benchmark:benchmark command'
  383. )
  384. _describe 'values' commands
  385. _arguments $borg_common_options
  386. ;;
  387. esac
  388. }
  389. __borg_archive() {
  390. local -a items
  391. if (($+BORG_REPO)); then
  392. items=("${(@f)$(borg list $cpath --short 2>/dev/null | sed "s#^#::#")}")
  393. else
  394. local cpath
  395. cpath=$words[-1]
  396. cpath=${cpath%::*}
  397. items=("${(@f)$(borg list $cpath --short 2>/dev/null | sed "s#^#${cpath}::#")}")
  398. fi
  399. if [[ $items[1] == "" ]]; then
  400. _files -/
  401. else
  402. _wanted archives expl 'archive' compadd $items
  403. fi
  404. }