borg 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 --version --critical --error --warning --info -v --verbose --debug --debug-topic -p --progress --log-json --lock-wait --show-version --show-rc --umask --remote-path --remote-ratelimit --consider-part-files --debug-profile"
  15. local opts="${common_opts}"
  16. # Commands
  17. if [[ ${COMP_CWORD} == 1 ]] ; then
  18. local borg_commands="init create extract check rename list diff delete prune info mount umount key serve upgrade recreate export-tar with-lock break-lock benchmark config"
  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 "import export change-passphrase" -- ${cur}) )
  26. return 0
  27. ;;
  28. 'benchmark')
  29. COMPREPLY=( $(compgen -W "crud" -- ${cur}) )
  30. return 0
  31. ;;
  32. '--encryption' | '-e')
  33. local encryption_modes="none keyfile keyfile-blake2 repokey repokey-blake2 authenticated authenticated-blake2"
  34. COMPREPLY=( $(compgen -W "${encryption_modes}" -- ${cur}) )
  35. return 0
  36. ;;
  37. '--files-cache')
  38. local files_cache_mode="ctime,size,inode mtime,size,inode ctime,size mtime,size rechunk,ctime rechunk,mtime disabled"
  39. COMPREPLY=( $(compgen -W "${files_cache_mode}" -- ${cur}) )
  40. return 0
  41. ;;
  42. '--compression' | '-C')
  43. 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"
  44. COMPREPLY=( $(compgen -W "${compression_methods}" -- ${cur}) )
  45. return 0
  46. ;;
  47. '--sort-by')
  48. local sort_keys="timestamp name id"
  49. COMPREPLY=( $(compgen -W "${sort_keys}" -- ${cur}) )
  50. return 0
  51. ;;
  52. '-o')
  53. # FIXME there are lot more options, but not all are applicable:
  54. local fuse_options="allow_other allow_root versions allow_damaged_files"
  55. COMPREPLY=( $(compgen -W "${fuse_options}" -- ${cur}) )
  56. return 0
  57. ;;
  58. '--recompress')
  59. local recompress_when="if-different always"
  60. COMPREPLY=( $(compgen -W "${recompress_when}" -- ${cur}) )
  61. return 0
  62. ;;
  63. esac
  64. if [[ ${cur} == -* ]] ; then
  65. case "${COMP_LINE}" in
  66. *' init '*)
  67. local opts="-e --encryption --append-only --storage-quota ${common_opts}"
  68. ;;
  69. *' create '*)
  70. local opts="-n --dry-run -s --stats --list --filter --json --no-cache-sync -e --exclude --exclude-from --pattern --patterns-from --exclude-caches --exclude-if-present --keep-exclude-tags --keep-tag-files --exclude-nodump -x --one-file-system --numeric-owner --noatime --noctime --nobirthtime --nobsdflags --ignore-inode --files-cache --read-special --comment --timestamp -c --checkpoint-interval --chunker-params -C --compression ${common_opts}"
  71. ;;
  72. *' extract '*)
  73. local opts="--list -n --dry-run --numeric-owner --nobsdflags --stdout --sparse -e --exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
  74. ;;
  75. *' check '*)
  76. local opts="--repository-only --archives-only --verify-data --repair --save-space -P --prefix -a --glob-archives --sort-by --first --last ${common_opts}"
  77. ;;
  78. # rename
  79. # no specific options
  80. *" list "*)
  81. local opts="--short --list-format --format --json --json-lines -P --prefix -a --glob-archives --sort-by --first --last -e --exclude --exclude-from --pattern --patterns-from ${common_opts}"
  82. ;;
  83. *' diff '*)
  84. local opts="--numeric-owner --same-chunker-params --sort -e --exclude --exclude-from --pattern --patterns-from ${common_opts}"
  85. ;;
  86. *' delete '*)
  87. local opts="-s --stats --cache-only --force --save-space -P --prefix -a --glob-archives --sort-by --first --last ${common_opts}"
  88. ;;
  89. *' prune '*)
  90. 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 --save-space -P --prefix -a --glob-archives ${common_opts}"
  91. ;;
  92. *' info '*)
  93. local opts="--json -P --prefix -a --glob-archives --sort-by --first --last ${common_opts}"
  94. ;;
  95. *' mount '*)
  96. local opts="-f --foreground -o -P --prefix -a --glob-archives --sort-by --first --last ${common_opts}"
  97. ;;
  98. # umount
  99. # no specific options
  100. # key change-passphrase
  101. # no specific options
  102. *' export '*)
  103. local opts="--paper --qr-html ${common_opts}"
  104. ;;
  105. *' import '*)
  106. local opts="--paper ${common_opts}"
  107. ;;
  108. *' upgrade '*)
  109. local opts="-n --dry-run --inplace --force --tam --disable-tam ${common_opts}"
  110. ;;
  111. *' recreate '*)
  112. local opts="--list --filter -n dry-run -s stats -e exclude --exclude-from --pattern --patterns-from --exclude-caches --exclude-if-present --keep-exclude-tags --keep-tag-files --target -c checkpoint-interval --comment --timestamp --timestamp -C compression --recompress --chunker-params ${common_opts}"
  113. ;;
  114. *' export-tar '*)
  115. local opts="--tar-filter --list -e exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
  116. ;;
  117. *' serve '*)
  118. local opts="--restrict-to-path --restrict-to-repository --append-only --storage-quota ${common_opts}"
  119. ;;
  120. # with-lock
  121. # no specific options
  122. # break-lock
  123. # no specific options
  124. # benchmark crud
  125. # no specific options
  126. *' config '*)
  127. local opts="-c --cache -d --delete ${common_opts}"
  128. ;;
  129. esac
  130. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  131. return 0
  132. fi
  133. # Listing archives.
  134. # Since "::" is treated as separate word in bash,
  135. # it is $cur when the cursor is right behind it
  136. # and $prev if the user has started to type an archive name.
  137. local repository_name="" # If set, we'll list the archives
  138. local typed_word=""
  139. if [[ ${cur} == "::" ]] ; then
  140. repository_name=${prev}
  141. fi
  142. if [[ ${prev} == "::" ]] ; then
  143. repository_name=${prevprev}
  144. typed_word=${cur}
  145. fi
  146. if [[ ${repository_name} != "" ]] ; then
  147. if [[ ${COMP_LINE} == *" ::"* ]] ; then
  148. # There is a space before the "::"
  149. # which means that no repository name was typed,
  150. # so probably $BORG_REPO is set.
  151. repository_name=""
  152. fi
  153. local archive_list=$(borg list --short "${repository_name}" 2>/dev/null)
  154. COMPREPLY=( $(compgen -W "${archive_list}" -- "${typed_word}" ) )
  155. return 0
  156. fi
  157. return 0
  158. }
  159. complete -F _borg borg