borg 6.4 KB

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