borg 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Completions for borg
  2. # https://www.borgbackup.org/
  3. # Note:
  4. # Listing archives works on password protected repositories only if $BORG_PASSPHRASE is set.
  5. # Install:
  6. # Copy this file to /usr/share/bash-completion/completions/ or /etc/bash_completion.d/
  7. _borg()
  8. {
  9. compopt -o default
  10. COMPREPLY=()
  11. local cur="${COMP_WORDS[COMP_CWORD]}"
  12. local prev="${COMP_WORDS[COMP_CWORD-1]}"
  13. local prevprev="${COMP_WORDS[COMP_CWORD-2]}"
  14. local common_opts="-h --help --critical --error --warning --info -v --verbose --debug --debug-topic -p --progress --iec --log-json --lock-wait --bypass-lock --show-version --show-rc --umask --remote-path --upload-ratelimit --upload-buffer --consider-part-files --debug-profile --rsh -r --repo"
  15. local opts="${common_opts}"
  16. # Commands
  17. if [[ ${COMP_CWORD} == 1 ]] ; then
  18. local borg_commands="benchmark break-lock check compact config create delete diff export-tar extract help import-tar info key list mount prune rcompress rcreate rdelete recreate rename rinfo rlist serve transfer umount with-lock"
  19. COMPREPLY=( $(compgen -W "${borg_commands}" -- ${cur}) )
  20. compopt +o default
  21. return 0
  22. fi
  23. case "${prev}" in
  24. 'key')
  25. COMPREPLY=( $(compgen -W "change-location change-passphrase export import" -- ${cur}) )
  26. return 0
  27. ;;
  28. 'benchmark')
  29. COMPREPLY=( $(compgen -W "cpu crud" -- ${cur}) )
  30. return 0
  31. ;;
  32. 'help')
  33. COMPREPLY=( $(compgen -W "patterns placeholders compression" -- ${cur}) )
  34. return 0
  35. ;;
  36. '--encryption' | '-e')
  37. local encryption_modes="authenticated authenticated-blake2 keyfile-aes-ocb keyfile-blake2-aes-ocb keyfile-blake2-chacha20-poly1305 keyfile-chacha20-poly1305 none repokey-aes-ocb repokey-blake2-aes-ocb repokey-blake2-chacha20-poly1305 repokey-chacha20-poly1305"
  38. COMPREPLY=( $(compgen -W "${encryption_modes}" -- ${cur}) )
  39. return 0
  40. ;;
  41. '--files-cache')
  42. local files_cache_mode="ctime,size,inode mtime,size,inode ctime,size mtime,size rechunk,ctime rechunk,mtime size disabled"
  43. COMPREPLY=( $(compgen -W "${files_cache_mode}" -- ${cur}) )
  44. return 0
  45. ;;
  46. '--compression' | '-C')
  47. local compression_methods="none auto lz4 zstd,1 zstd,2 zstd,3 zstd,4 zstd,5 zstd,6 zstd,7 zstd,8 zstd,9 zstd,10 zstd,11 zstd,12 zstd,13 zstd,14 zstd,15 zstd,16 zstd,17 zstd,18 zstd,19 zstd,20 zstd,21 zstd,22 zlib,1 zlib,2 zlib,3 zlib,4 zlib,5 zlib,6 zlib,7 zlib,8 zlib,9 lzma,0 lzma,1 lzma,2 lzma,3 lzma,4 lzma,5 lzma,6 lzma,7 lzma,8 lzma,9"
  48. COMPREPLY=( $(compgen -W "${compression_methods}" -- ${cur}) )
  49. return 0
  50. ;;
  51. '--sort-by')
  52. local sort_keys="timestamp name id"
  53. COMPREPLY=( $(compgen -W "${sort_keys}" -- ${cur}) )
  54. return 0
  55. ;;
  56. '-o')
  57. # FIXME This list is probably not full, but I tried to pick only those that are relevant to borg mount -o:
  58. local fuse_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"
  59. COMPREPLY=( $(compgen -W "${fuse_options}" -- ${cur}) )
  60. return 0
  61. ;;
  62. '--recompress')
  63. local recompress_when="if-different always never"
  64. COMPREPLY=( $(compgen -W "${recompress_when}" -- ${cur}) )
  65. return 0
  66. ;;
  67. '--upgrader')
  68. local upgraders="From12To20 NoOp"
  69. COMPREPLY=( $(compgren -W "${upgraders}" -- ${cur}) )
  70. return 0
  71. ;;
  72. esac
  73. if [[ ${cur} == -* ]] ; then
  74. case "${COMP_LINE}" in
  75. *' rcreate '*)
  76. local opts="--other-repo -e --encryption --append-only --storage-quota --make-parent-dirs --copy-crypt-key ${common_opts}"
  77. ;;
  78. *' rlist '*)
  79. local opts="--consider-checkpoints --short --format --json ${common_opts} -a --match-archives --sort-by --first --last"
  80. ;;
  81. *' rinfo '*)
  82. local opts="--json ${common_opts}"
  83. ;;
  84. *' rcompress '*)
  85. local opts="-C --compression -s --stats -c --checkpoint-interval ${common_opts}"
  86. ;;
  87. *' rdelete '*)
  88. local opts="-n --dry-run --list --force --cache-only --keep-security-info ${common_opts}"
  89. ;;
  90. *' create '*)
  91. local opts="-n --dry-run -s --stats --list --filter --json --no-cache-sync --stdin-name --stdin-user --stdin-group --stdin-mode --content-from-command --paths-from-stdin --paths-from-command --paths-delimiter -e --exclude --exclude-from --pattern --patterns-from --exclude-caches --exclude-if-present --keep-exclude-tags --exclude-nodump -x --one-file-system --numeric-ids --atime --noctime --nobirthtime --noflags --noacls --noxattrs --sparse --files-cache --read-special --comment --timestamp -c --checkpoint-interval --chunker-params -C --compression ${common_opts}"
  92. ;;
  93. *' extract '*)
  94. local opts="--list -n --dry-run --numeric-ids --noflags --noacls --noxattrs --stdout --sparse -e --exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
  95. ;;
  96. *' check '*)
  97. local opts="--repository-only --archives-only --verify-data --repair --max-duration -a --match-archives --sort-by --first --last ${common_opts}"
  98. ;;
  99. # rename
  100. # no specific options
  101. *" list "*)
  102. local opts="--short --format --json-lines -e --exclude --exclude-from --pattern --patterns-from ${common_opts}"
  103. ;;
  104. *' diff '*)
  105. local opts="--numeric-ids --same-chunker-params --sort --json-lines -e --exclude --exclude-from --pattern --patterns-from ${common_opts}"
  106. ;;
  107. *' delete '*)
  108. local opts="-n --dry-run --list --consider-checkpoints -s --stats --cache-only --force -c --checkpoint-interval -a --match-archives --sort-by --first --last ${common_opts}"
  109. ;;
  110. *' prune '*)
  111. local opts="-n --dry-run --force -s --stats --list --keep-within --keep-last --keep-secondly --keep-minutely -H --keep-hourly -d --keep-daily -w --keep-weekly -m --keep-monthly -y --keep-yearly -c --checkpoint-interval -a --match-archives ${common_opts}"
  112. ;;
  113. *' compact '*)
  114. local opts="--threshold ${common_opts}"
  115. ;;
  116. *' info '*)
  117. local opts="--json -a --match-archives --sort-by --first --last ${common_opts}"
  118. ;;
  119. *' mount '*)
  120. local opts="--consider-checkpoints -f --foreground -o --numeric-ids -a --match-archives --sort-by --first --last -e --exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
  121. ;;
  122. # umount
  123. # no specific options
  124. # key change-passphrase
  125. # no specific options
  126. *' change-location '*)
  127. local opts="${common_opts} keyfile repokey --keep"
  128. ;;
  129. *' export '*)
  130. local opts="--paper --qr-html ${common_opts}"
  131. ;;
  132. *' import '*)
  133. local opts="--paper ${common_opts}"
  134. ;;
  135. *' recreate '*)
  136. local opts="--list --filter -n dry-run -s stats -e exclude --exclude-from --pattern --patterns-from --exclude-caches --exclude-if-present --keep-exclude-tags -a --match-archives --sort-by --first --last --target -c --checkpoint-interval --comment --timestamp -C --compression --recompress --chunker-params ${common_opts}"
  137. ;;
  138. *' export-tar '*)
  139. local opts="--tar-filter --list --tar-format -e exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
  140. ;;
  141. *' import-tar '*)
  142. local opts="--tar-filter -s --stats --list --filter --json ${common_opts} --comment --timestamp -c --checkpoint-interval --chunker-params -C --compression"
  143. ;;
  144. *' transfer '*)
  145. local opts="-n --dry-run --other-repo --upgrader ${common_opts} -a --match-archives --sort-by --first --last"
  146. ;;
  147. *' serve '*)
  148. local opts="--restrict-to-path --restrict-to-repository --append-only --storage-quota ${common_opts}"
  149. ;;
  150. *' config '*)
  151. local opts="-c --cache -d --delete --list ${common_opts}"
  152. ;;
  153. # with-lock
  154. # no specific options
  155. # break-lock
  156. # no specific options
  157. # benchmark crud
  158. # no specific options
  159. esac
  160. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  161. return 0
  162. fi
  163. # Get the repository name if available
  164. # If there is a space before the "::" it means that no repository name was typed,
  165. # so probably $BORG_REPO was set and we can still list the archives.
  166. local repository_name="${COMP_LINE%%::*}"
  167. repository_name=${repository_name##* }
  168. # Listing archives.
  169. # Since "::" is treated as separate word in Bash,
  170. # it is $cur when the cursor is right behind it
  171. # and $prev if the user has started to type an archive name.
  172. local typed_word=${cur}
  173. local -i list_archives=0
  174. if [[ ${cur} == "::" ]] ; then
  175. list_archives=1
  176. typed_word=""
  177. fi
  178. if [[ ${prev} == "::" ]] ; then
  179. list_archives=1
  180. fi
  181. # Second archive listing for borg diff
  182. if [[ ${COMP_LINE} =~ ^.*\ diff\ .*::[^\ ]+\ ${cur}$ ]] ; then
  183. list_archives=1
  184. fi
  185. # Additional archive listing for borg delete
  186. if [[ ${COMP_LINE} =~ ^.*\ delete\ .*::[^\ ]+.*${cur}$ ]] ; then
  187. list_archives=1
  188. fi
  189. if (( $list_archives )) ; then
  190. local archives=$(borg list --short "${repository_name}" 2>/dev/null)
  191. COMPREPLY=( $(compgen -W "${archives}" -- "${typed_word}" ) )
  192. return 0
  193. fi
  194. return 0
  195. }
  196. complete -F _borg borg