_borg 20 KB

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