borg 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 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 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"
  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 -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}"
  75. ;;
  76. *' extract '*)
  77. local opts="--list -n --dry-run --numeric-owner --nobsdflags --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 -P --prefix -a --glob-archives --sort-by --first --last ${common_opts}"
  81. ;;
  82. # rename
  83. # no specific options
  84. *" list "*)
  85. 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}"
  86. ;;
  87. *' diff '*)
  88. local opts="--numeric-owner --same-chunker-params --sort -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. *' info '*)
  97. local opts="--json -P --prefix -a --glob-archives --sort-by --first --last ${common_opts}"
  98. ;;
  99. *' mount '*)
  100. 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}"
  101. ;;
  102. # umount
  103. # no specific options
  104. # key change-passphrase
  105. # no specific options
  106. *' export '*)
  107. local opts="--paper --qr-html ${common_opts}"
  108. ;;
  109. *' import '*)
  110. local opts="--paper ${common_opts}"
  111. ;;
  112. *' upgrade '*)
  113. local opts="-n --dry-run --inplace --force --tam --disable-tam ${common_opts}"
  114. ;;
  115. *' recreate '*)
  116. 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}"
  117. ;;
  118. *' export-tar '*)
  119. local opts="--tar-filter --list -e exclude --exclude-from --pattern --patterns-from --strip-components ${common_opts}"
  120. ;;
  121. *' serve '*)
  122. local opts="--restrict-to-path --restrict-to-repository --append-only --storage-quota ${common_opts}"
  123. ;;
  124. *' config '*)
  125. local opts="-c --cache -d --delete --list ${common_opts}"
  126. ;;
  127. # with-lock
  128. # no specific options
  129. # break-lock
  130. # no specific options
  131. # benchmark crud
  132. # no specific options
  133. esac
  134. COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
  135. return 0
  136. fi
  137. # Listing archives.
  138. # Since "::" is treated as separate word in bash,
  139. # it is $cur when the cursor is right behind it
  140. # and $prev if the user has started to type an archive name.
  141. local repository_name="" # If set, we'll list the archives
  142. local typed_word=""
  143. if [[ ${cur} == "::" ]] ; then
  144. repository_name=${prev}
  145. fi
  146. if [[ ${prev} == "::" ]] ; then
  147. repository_name=${prevprev}
  148. typed_word=${cur}
  149. fi
  150. # Second archive listing for borg diff
  151. if [[ ${COMP_LINE} =~ ^.*\ diff\ .*::[^\ ]+\ ${cur}$ ]] ; then
  152. repository_name=`expr match "${COMP_LINE}" "\(.*\)::"`
  153. repository_name=${repository_name##* }
  154. typed_word=${cur}
  155. fi
  156. if [[ ${repository_name} != "" ]] ; then
  157. if [[ ${COMP_LINE} == *" ::"* ]] ; then
  158. # There is a space before the "::"
  159. # which means that no repository name was typed,
  160. # so probably $BORG_REPO is set.
  161. repository_name=""
  162. fi
  163. local archive_list=$(borg list --short "${repository_name}" 2>/dev/null)
  164. COMPREPLY=( $(compgen -W "${archive_list}" -- "${typed_word}" ) )
  165. return 0
  166. fi
  167. return 0
  168. }
  169. complete -F _borg borg