_borg 24 KB

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