borg 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 --rsh"
  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 compact info mount umount key serve upgrade recreate export-tar with-lock break-lock config benchmark help"
  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. 'help')
  33. COMPREPLY=( $(compgen -W "patterns placeholders compression" -- ${cur}) )
  34. return 0
  35. ;;
  36. '--encryption' | '-e')
  37. local encryption_modes="none keyfile keyfile-blake2 repokey repokey-blake2 authenticated authenticated-blake2"
  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. esac
  68. if [[ ${cur} == -* ]] ; then
  69. case "${COMP_LINE}" in
  70. *' init '*)
  71. local opts="-e --encryption --append-only --storage-quota --make-parent-dirs ${common_opts}"
  72. ;;
  73. *' create '*)
  74. local opts="-n --dry-run -s --stats --list --filter --json --no-cache-sync --stdin-name --content-from-command -e --exclude --exclude-from --pattern --patterns-from --exclude-caches --exclude-if-present --keep-exclude-tags --exclude-nodump -x --one-file-system --numeric-owner --noatime --noctime --nobirthtime --nobsdflags --noacls --noxattrs --noflags --files-cache --read-special --comment --timestamp -c --checkpoint-interval --chunker-params -C --compression ${common_opts}"
  75. ;;
  76. *' extract '*)
  77. local opts="--list -n --dry-run --numeric-owner --nobsdflags --noacls --noxattrs --stdout --sparse -e --exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
  78. ;;
  79. *' check '*)
  80. local opts="--repository-only --archives-only --verify-data --repair --save-space --max-duration -P --prefix -a --glob-archives --sort-by --first --last ${common_opts}"
  81. ;;
  82. # rename
  83. # no specific options
  84. *" list "*)
  85. local opts="--short --format --json --json-lines -P --prefix -a --glob-archives --sort-by --first --last -e --exclude --exclude-from --pattern --patterns-from ${common_opts}"
  86. ;;
  87. *' diff '*)
  88. local opts="--numeric-owner --same-chunker-params --sort --json-lines -e --exclude --exclude-from --pattern --patterns-from ${common_opts}"
  89. ;;
  90. *' delete '*)
  91. local opts="-n --dry-run -s --stats --cache-only --force --save-space -P --prefix -a --glob-archives --sort-by --first --last ${common_opts}"
  92. ;;
  93. *' prune '*)
  94. 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}"
  95. ;;
  96. *' compact '*)
  97. local opts="--cleanup-commits ${common_opts}"
  98. ;;
  99. *' info '*)
  100. local opts="--json -P --prefix -a --glob-archives --sort-by --first --last ${common_opts}"
  101. ;;
  102. *' mount '*)
  103. local opts="-f --foreground -o -P --prefix -a --glob-archives --sort-by --first --last -e --exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
  104. ;;
  105. # umount
  106. # no specific options
  107. # key change-passphrase
  108. # no specific options
  109. *' export '*)
  110. local opts="--paper --qr-html ${common_opts}"
  111. ;;
  112. *' import '*)
  113. local opts="--paper ${common_opts}"
  114. ;;
  115. *' upgrade '*)
  116. local opts="-n --dry-run --inplace --force --tam --disable-tam ${common_opts}"
  117. ;;
  118. *' recreate '*)
  119. local opts="--list --filter -n dry-run -s stats -e exclude --exclude-from --pattern --patterns-from --exclude-caches --exclude-if-present --keep-exclude-tags --target -c checkpoint-interval --comment --timestamp --timestamp -C compression --recompress --chunker-params ${common_opts}"
  120. ;;
  121. *' export-tar '*)
  122. local opts="--tar-filter --list -e exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
  123. ;;
  124. *' serve '*)
  125. local opts="--restrict-to-path --restrict-to-repository --append-only --storage-quota ${common_opts}"
  126. ;;
  127. *' config '*)
  128. local opts="-c --cache -d --delete --list ${common_opts}"
  129. ;;
  130. # with-lock
  131. # no specific options
  132. # break-lock
  133. # no specific options
  134. # benchmark crud
  135. # no specific options
  136. esac
  137. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  138. return 0
  139. fi
  140. # Get the repository name if available
  141. # If there is a space before the "::" it means that no repository name was typed,
  142. # so probably $BORG_REPO was set and we can still list the archives.
  143. local repository_name="${COMP_LINE%%::*}"
  144. repository_name=${repository_name##* }
  145. # Listing archives.
  146. # Since "::" is treated as separate word in Bash,
  147. # it is $cur when the cursor is right behind it
  148. # and $prev if the user has started to type an archive name.
  149. local typed_word=${cur}
  150. local -i list_archives=0
  151. if [[ ${cur} == "::" ]] ; then
  152. list_archives=1
  153. typed_word=""
  154. fi
  155. if [[ ${prev} == "::" ]] ; then
  156. list_archives=1
  157. fi
  158. # Second archive listing for borg diff
  159. if [[ ${COMP_LINE} =~ ^.*\ diff\ .*::[^\ ]+\ ${cur}$ ]] ; then
  160. list_archives=1
  161. fi
  162. # Additional archive listing for borg delete
  163. if [[ ${COMP_LINE} =~ ^.*\ delete\ .*::[^\ ]+.*${cur}$ ]] ; then
  164. list_archives=1
  165. fi
  166. if (( $list_archives )) ; then
  167. local archives=$(borg list --short "${repository_name}" 2>/dev/null)
  168. COMPREPLY=( $(compgen -W "${archives}" -- "${typed_word}" ) )
  169. return 0
  170. fi
  171. return 0
  172. }
  173. complete -F _borg borg